Last active
January 31, 2019 10:07
-
-
Save samuelantonioli/3612cec5392afb81ac6e276c7590b426 to your computer and use it in GitHub Desktop.
Python Send Mail with Attachments
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
###### | |
# | |
# short snippet to send a mail with python | |
# with mail attachments | |
# | |
# - always handy to have such a code snippet | |
# - should work on python 2.x and 3.x | |
# - supports multiple recipients | |
# - based on https://docs.python.org/3.1/library/email-examples.html | |
# - please do proper error handling in production | |
# - some details on msg['To'] and sendmail: https://stackoverflow.com/a/8861795 | |
# | |
###### | |
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.mime.application import MIMEApplication | |
def sendmail(recv, subject, plain, html = None, attachments = []): | |
''' | |
simple function which sends a html mail with some attachments. | |
make sure that all attachments can be read. | |
usage: | |
sendmail('you@email.com', 'Mail', 'Some Text', None, [ | |
{ | |
'name': 'filename.pdf', | |
'file': '/path/to/a/file', | |
}, | |
]) | |
or | |
sendmail(['one@email.com', 'two@email.com'], 'subject line', 'some plain text') | |
''' | |
# me == my email address, recv == recipient's email address | |
me = 'me@email.com' | |
password = 'password' | |
server = 'smtp.email.com' | |
port = 587 | |
if isinstance(recv, str): | |
recv = [recv] | |
try: | |
# make sure that some assumptions are correct | |
assert isinstance(recv, (list, tuple)) | |
assert plain or html | |
assert isinstance(attachments, (list, tuple)) | |
# create message container - the correct MIME type is multipart/alternative. | |
msg = MIMEMultipart() # 'alternative' - not doing it | |
msg['Subject'] = '{}'.format(subject) | |
msg['From'] = me | |
msg['To'] = ', '.join(recv) | |
# add main content | |
if not plain is None: | |
part = MIMEText(plain, 'plain') | |
msg.attach(part) | |
if not html is None: | |
part = MIMEText(html, 'html') | |
msg.attach(part) | |
# According to RFC 2046, the last part of a multipart message, in this case | |
# the HTML message, is best and preferred. | |
# add attachment files | |
for attachment in attachments: | |
name = attachment['name'] | |
with open(attachment['file'], 'rb') as f: | |
content = f.read() | |
part = MIMEApplication(content, Name=name) | |
part['Content-Disposition'] = 'attachment; filename="{}"'.format(name) | |
msg.attach(part) | |
# Send the message via SMTP server. | |
mail = smtplib.SMTP(server, port) | |
mail.ehlo() | |
mail.starttls() | |
mail.login(me, password) | |
mail.sendmail(me, recv, msg.as_string()) | |
mail.quit() | |
return True | |
except Exception: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment