Hi All,<div><br></div><div>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.</div>
<div><br></div><div>Any problem with this code? any insight would be greatly appreciated.</div><div><br></div><div><div>import smtplib</div><div><br></div><div>from email.mime.multipart import MIMEMultipart</div><div>from email.mime.text import MIMEText</div>
<div><br></div><div>testfile = 'test.txt'</div><div><br></div><div>msg = MIMEMultipart()</div><div>msg['Subject'] = 'Email with Attachment'</div><div>msg['From'] = '<a href="mailto:swavijay@gmail.com">swavijay@gmail.com</a>'</div>
<div>msg['To'] = '<a href="mailto:swavijay@gmail.com">swavijay@gmail.com</a>'</div><div><br></div><div>with open(testfile,'rb') as f:</div><div> # Open the files in binary mode. Let the MIMEImage class automatically</div>
<div> # guess the specific image type.</div><div> img = MIMEText(f.read())</div><div> f.close()</div><div> msg.attach(img)</div><div> </div><div>try:</div><div> s = smtplib.SMTP('localhost')</div>
<div> s.sendmail(msg['From'],[msg['To']],msg.as_string())</div><div> print 'Email with attachment sent successfully'</div><div> s.quit()</div><div>except:</div><div> print 'Email with attachment could not be sent !!'</div>
<br>-- <br>Vijay Swaminathan<br>
</div>