faqts : Computers : Programming : Languages : Python : Common Problems : Email

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 19 people (63%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

I can send mail via the smtplib but how do I attach files to the email?

Jun 25th, 2000 20:44
unknown unknown, Jürgen Hermann, Grant Edwards


Just did this today (you're lucky), you need a combination of cStringIO
(create file objects from your text, optional), MimeWriter (create the 
MIME msg), mimetools (encode the parts, f.x. in base64), and smtplib 
(send the mail).

----------------

Take a look at <ftp://ftp.visi.com/users/grante/stuff/mfetch.py>.

There's a class called SmtpWriter that has a "Message" method that 
probably does what you want...

def Message(self,sender="", subject="", recipients=[], body="", 
attachments=[]):
    if self.__debugLevel > 2:
        sys.stderr.write("SmtpWriter: Building RFC822 message From: %s; 
Subject: %s; (Length=%d with %d
attachments)\n" % 
                          (sender, subject, len(body),len(attachments)))
[...]

The MimeWriter library module is where most of the magic happens...