Created
August 26, 2012 17:20
-
-
Save mitchtech/3481781 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 = "username@gmail.com" | |
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( ["mailto@gmail.com"], | |
"this is the subject", | |
"this is the body text of the email", | |
["photo.jpg","text_file.txt"] ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
really really thank you for your guide