Created
January 3, 2016 11:17
-
-
Save nejdetckenobi/36b932db119baa527cc7 to your computer and use it in GitHub Desktop.
sending mail via postfix, 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
# Source: http://masnun.com/2010/01/01/sending-mail-via-postfix-a-perfect-python-example.html | |
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 | |
def sendMail(to, fro, subject, text, files=[],server="localhost"): | |
assert type(to)==list | |
assert type(files)==list | |
msg = MIMEMultipart() | |
msg['From'] = fro | |
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) | |
smtp = smtplib.SMTP(server) | |
smtp.sendmail(fro, to, msg.as_string() ) | |
smtp.close() | |
# Example: | |
sendMail(['maSnun <masnun@gmail.com>'],'phpGeek <masnun@leevio.com>','Hello Python!','Heya buddy! Say hello to Python! :)',['masnun.py','masnun.php']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment