Skip to content

Instantly share code, notes, and snippets.

@mitchtech
Created August 26, 2012 17:20
Show Gist options
  • Save mitchtech/3481781 to your computer and use it in GitHub Desktop.
Save mitchtech/3481781 to your computer and use it in GitHub Desktop.
#!/usr/bin/env 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
USERNAME = "[email protected]"
PASSWORD = "password"
def sendMail(to, subject, text, files=[]):
assert type(to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = USERNAME
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)
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo_or_helo_if_needed()
server.starttls()
server.ehlo_or_helo_if_needed()
server.login(USERNAME,PASSWORD)
server.sendmail(USERNAME, to, msg.as_string())
server.quit()
sendMail( ["[email protected]"],
"this is the subject",
"this is the body text of the email",
["photo.jpg","text_file.txt"] )
@saghaei20
Copy link

really really thank you for your guide

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment