DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Python : Send A Mail (text), With Attachments

09.21.2005
| 59068 views |
  • submit to reddit
        
import smtplib
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
import os

def sendMail(to, subject, text, files=[],server="localhost"):
    assert type(to)==list
    assert type(files)==list
    fro = "Expediteur <expediteur@mail.com>"

    msg = MIMEMultipart()
    msg['From'] = fro
    msg['To'] = COMMASPACE.join(to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )

    for file 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(file))
        msg.attach(part)

    smtp = smtplib.SMTP(server)
    smtp.sendmail(fro, to, msg.as_string() )
    smtp.close()


sendMail(
        ["destination@dest.kio"],
        "hello","cheers",
        ["photo.jpg","memo.sxw"]
    )

    

Comments

Snippets Manager replied on Sat, 2008/08/23 - 5:48am

Thanks a lot. This worked instantly - luckily it was the first snippet I tried. I think it should be pointed out that this works on a local SMTP server, such as postfix. I just got postfix to use gmail as a relay host *hurray*. Otherwise, one will have to do something like: server = smtplib.SMTP('smtp.gmail.com', 587) server.set_debuglevel(1) server.ehlo() server.starttls() server.ehlo() # assume I am john smith, sending to jane smith server.login("johnsmith@gmail.com", "xxxx") server.sendmail("john.smith@gmail.com", "jane.smith@gmail.com", msg) server.quit()

Snippets Manager replied on Fri, 2006/01/06 - 2:53am

I try the following before & success to send an e-mail using python.exe to compile the .py, but when I use py2exe to compile .py to .exe and run .exe some error exist, why?? error after run .exe: the following is my .py: import email import smtplib 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 import os def sendMail(to, fm, subject, text, files=[], server="192.168.0.99"): text=str(text) assert type(to)==list assert type(files)==list fro = fm msg = MIMEMultipart() msg['From'] = fro msg['To'] = COMMASPACE.join(to) msg['Date'] = formatdate(localtime=True) msg['Subject'] = subject msg.attach( MIMEText(text) ) for file 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(file)) msg.attach(part) smtp = smtplib.SMTP(server) smtp.sendmail(fro, to, msg.as_string() ) smtp.close()