You are here: Home Plone My Python Scripts Sendmail with attachments
Search
Advanced Search…
E-Mail

Webmail: webmail.wyden.com

E-Mail Preferences: postfix.wyden.com/users

E-Mail Administration: postfix.wyden.com

Statistics
Total: 463
Total Pages: 284
Total Folders: 87
Total Files: 18
Total Links: 26
Last modification: 03.02.2012 16:00
 

Sendmail with attachments

by Wyden Silvan last modified 11.01.2010 16:01

import smtplib
import os
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

def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
          assert type(send_to)==list
          assert type(files)==list

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

          msg.attach( MIMEText(text) )

          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="%s"' % os.path.basename(f))
            msg.attach(part)

          smtp = smtplib.SMTP(server)
          smtp.sendmail(send_from, send_to, msg.as_string())
          smtp.close()