P@c_M@n Posted October 1, 2010 Posted October 1, 2010 Hey guys i have been playing around with python and email. I am using python 2.6. Anyway, i have noticed that there are some big differences between my script that just sends an email and the script that sends an email with a file attachment. Both scripts work, but i would like to know if i am doing anything wrong and/or if i can improve anything within these scripts. Heres emailer.py: import smtplib from email.mime.text import MIMEText server=smtplib.SMTP() server.connect("smtp.mail.yahoo.com", 25) server.login("*************", "*************") msg = MIMEText("Hello! This message was sent by the python emailer!") msg['Subject'] = 'hi!' msg['From'] = "**************" msg['To'] = "*****************" server.sendmail("***************", "**************", msg.as_string()) server.quit() And heres attachment.py: import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email import Encoders msg = MIMEMultipart() msg['From'] = "*******************" msg['To'] = "****************" msg['Subject'] = "Hello" msg.attach( MIMEText("HIII!!!! This was sent by the python emailer!!!")) files=["games.txt"] for f in files: part = MIMEBase('application', "octet-stream") part.set_payload( open(f,"rb").read() ) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="games.txt"') msg.attach(part) s = smtplib.SMTP() s.connect("smtp.mail.yahoo.com", 25) s.login("*****************", "********") s.sendmail("******************", "***********", msg.as_string()) s.close() I have starred out my emails and passwords as you can see, but anyway, I notice how you have to import MIMEMultipart for the email in attachment.py, but in emailer.py, I dont have to do this. Are these normal email scripts and can i do these any better? Thanks in advance for any input. Quote
Mr-Protocol Posted October 1, 2010 Posted October 1, 2010 I don't understand what your question is. Did you code both of those? As far as editing them, they both seem robust and do not require anything changed. Quote
P@c_M@n Posted October 1, 2010 Author Posted October 1, 2010 Sorry about that. Yeah i kind of pieced them together through snippets i found online. I just wanted to make sure i did everything right. I didn't want to miss some really really important step lol. Thanks mr.protocol. Quote
Mr-Protocol Posted October 1, 2010 Posted October 1, 2010 (edited) The only thing I would suggest to change would be to add variables to take inputs. email.py [to] [from] [message] -f attachment.txt so you dont have to manually edit the script each time. Edited October 1, 2010 by Mr-Protocol Quote
P@c_M@n Posted October 1, 2010 Author Posted October 1, 2010 Add variables to take inputs? Like organize the script into functions? I was going to do that, but i just wanted to get down and dirty with the code and know that everything worked. And ill work into incorporating both scripts into just one. I was confused because in the first script, the only package you need to import from email is MIMEText and in the attachment script, you need to import MIMEMultipart as well. Because i tried coding like this for the attachment script: import smtplib from email.mime.text import MIMEText from email.MIMEBase import MIMEBase from email import Encoders server=smtplib.SMTP() server.connect("smtp.mail.yahoo.com", 25) server.login("*************", "*************") msg = MIMEText("Hello! This message was sent by the python emailer!") msg['Subject'] = 'hi!' msg['From'] = "**************" msg['To'] = "*****************" files=["games.txt"] for f in files: part = MIMEBase('application', "octet-stream") part.set_payload( open(f,"rb").read() ) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="games.txt"') msg.attach(part) server.sendmail("***************", "**************", msg.as_string()) server.quit() But the python interpreter said something about "MultipartConversionError: Cannot attach additional subparts to non-multipart/*" so i guess that means the script needs MIMEMultipart to sent attachments. Quote
Zimmer Posted October 2, 2010 Posted October 2, 2010 That is because email attachments are different encodings then just text in the email body. To do that you need MimeMultiPart to encode the attachments properly. Quote
P@c_M@n Posted October 5, 2010 Author Posted October 5, 2010 I see. That makes sense. Thanks for the info guys. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.