Why does it work ?
Michel Combe
combe.michel at 9online.fr
Tue Jul 1 10:01:13 EDT 2003
I'm trying to send a mail with an attached file.
I use the following code and *it works* but I don't understand why and I
hate that.
for filename in os.listdir(dir):
path = os.path.join(dir, filename)
if not os.path.isfile(path):
continue
ctype, encoding = mimetypes.guess_type(path)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
if maintype == 'text':
fp = open(path)
msg = MIMEText(fp.read(), _subtype=subtype)
fp.close()
msg.add_header('Content-Disposition', 'attachment', filename=filename)
outer.attach(msg)
In the "dir" directory, I have 2 text files :
- "texte.txt" which is the text I want inside the body of the message and
- "fichier.txt" which is the file I want attached.
Why is "texte.txt" the only file that I find in the body of the message.
Both files are text, so the MIMEText instruction shoud be executed for both
files, No ?
Also add_header should be executed for both files resulting in 2 attached
files ?
Which instruction says what is included in the body and what is attached to
the mail ?
I must say I'm not sure of what MIMEText and add_header exactly do. I read
the doc but it was not too clear for me.
Thanks for your help
Michel Combe
More information about the Python-list
mailing list