Last active
March 3, 2017 13:49
-
-
Save d-schmidt/4cd317107e6080d829f5e65be42b91b8 to your computer and use it in GitHub Desktop.
sending text mails from python with text attachments
This file contains hidden or 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 python3 | |
# this is python 2 and 3 compatible | |
import smtplib | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
import datetime | |
COMMASPACE = ', ' | |
sender = '[email protected]' | |
recipients = ['[email protected]', '[email protected]'] | |
subject = 'Hello World' | |
body = """Hello Users, | |
your csv file""" | |
msg = MIMEMultipart() | |
msg['Subject'] = subject | |
msg['From'] = sender | |
msg['To'] = COMMASPACE.join(recipients) | |
msg.preamble = subject # for clients without multipart support | |
# attachment file(s) | |
filename = "important.csv" | |
with open(filename, "r") as text_file: | |
attachment = MIMEText(text_file.read()) | |
attachment.add_header('Content-Disposition', 'attachment', filename=filename) | |
msg.attach(attachment) | |
#body | |
msg.attach(MIMEText(body, 'plain')) | |
s = smtplib.SMTP('localhost') | |
s.sendmail(sender, recipients, msg.as_string()) | |
s.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment