I am having problems understanding how to email an attachment using python. I have successfully emailed simple messages with the smtplib. Could someone please explain how to send an attachment in an email. I know there are other posts online but as a python beginner I find them hard to understand.
A:
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib
msg = MIMEMultipart()
msg.attach(MIMEText(file("text.txt").read()))
msg.attach(MIMEImage(file("image.png").read()))
# to send
mailer = smtplib.SMTP()
mailer.connect()
mailer.sendmail(from_, to, msg.as_string())
mailer.close()
Adapted from here.
Oli
2010-07-29 12:59:19
Not quite what I am looking for. The file was sent as the body of an email. There is also missing brackets on line 6 and 7. I feel that we are getting closer though
Richard
2010-07-29 13:23:22
Emails are plain text, and that's what `smtplib` supports. To send attachments, you encode them as a MIME message and send them in a plaintext email.There's a new python email module, though: http://docs.python.org/library/email.mime.html
katrielalex
2010-07-29 13:33:19
@katrienlalex a working example would go a long way to help my understanding
Richard
2010-07-29 13:52:25
I guess I should add that I am using python 2.4
Richard
2010-07-29 13:52:45
Are you sure the above example doesn't work? I don't have a SMTP server handy, but I looked at `msg.as_string()` and it certainly looks like the body of a MIME multipart email. Wikipedia explains MIME: http://en.wikipedia.org/wiki/MIME
katrielalex
2010-07-29 14:04:09
@katrielalex Thanks for the resource. Even though I have a working snip of code, I still am not sure whats happening here. This should help my understanding a bit.
Richard
2010-07-29 14:34:44
+1
A:
Here's another snip from here:
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
assert type(send_to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
It's much the same as the first example... But it should be easier to drop in.
Oli
2010-07-29 14:00:15
Thanks for you help. I just started to post my own answer when I saw yours. It ended up to be pretty much the same
Richard
2010-07-29 14:31:54
+1
A:
this is the code I ended up using:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
SUBJECT = "Email Data"
msg = MIMEMultipart()
msg['Subject'] = SUBJECT
msg['From'] = self.EMAIL_FROM
msg['To'] = ', '.join(self.EMAIL_TO)
part = MIMEBase('application', "octet-stream")
part.set_payload(open("text.txt", "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="text.txt"')
msg.attach(part)
server = smtplib.SMTP(self.EMAIL_SERVER)
server.sendmail(self.EMAIL_FROM, self.EMAIL_TO, msg.as_string())
Code is much the same as Oli's post. Thanks all
Code based from http://stackoverflow.com/questions/2798470/binary-file-email-attachment-problem post.
Richard
2010-07-29 14:29:52