Sending email in python
swavijay at gmail.com
Mon Jul 4 12:06:14 CEST 2011
I read through the smtplib and email modules of python and have come up with
a simple program to send a email as an attachment. This module sends out an
email but not as an attachment but in the body of the email.
Any problem with this code? any insight would be greatly appreciated.
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
testfile = 'test.txt'
msg = MIMEMultipart()
msg['Subject'] = 'Email with Attachment'
msg['From'] = 'swavijay at gmail.com'
msg['To'] = 'swavijay at gmail.com'
with open(testfile,'rb') as f:
# Open the files in binary mode. Let the MIMEImage class automatically
# guess the specific image type.
img = MIMEText(f.read())
s = smtplib.SMTP('localhost')
print 'Email with attachment sent successfully'
print 'Email with attachment could not be sent !!'
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list