Created
September 28, 2013 19:30
-
-
Save madmas/6745618 to your computer and use it in GitHub Desktop.
send a mail (including attachments) with python
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
import smtplib, 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() | |
send_mail("[email protected]",["[email protected]"],"testMail","A Little Txt",["mail.py"],"localhost") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment