Jump to content

Python Email Scripts


P@c_M@n
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...