Skip to content

Instantly share code, notes, and snippets.

@danigm
Created March 4, 2010 07:44
Show Gist options
  • Save danigm/321511 to your computer and use it in GitHub Desktop.
Save danigm/321511 to your computer and use it in GitHub Desktop.
Send mail with attachment in python
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
import time
import random
def generate_message_id(msg_from):
domain = msg_from.split("@")[1]
r = "%s.%s" % (time.time(), random.randint(0, 100))
mid = "<%s@%s>" % (r, domain)
return mid
def send_mail(msg_from, to, subject, text,
files=[],server="localhost", debug=False):
assert type(to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = msg_from
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
text = text.encode("utf-8")
text = MIMEText(text, 'plain', "utf-8")
msg.attach(text)
msg.add_header('Message-ID', generate_message_id(msg_from))
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)
if not debug:
smtp = smtplib.SMTP(server)
smtp.sendmail(msg_from, to, msg.as_string())
smtp.close()
return msg
#sendMail(["[email protected]"], "hello","cheers", ["photo.jpg","memo.sxw"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment