Created
July 22, 2020 13:40
-
-
Save samueltc/970d0aa8d69df4a29685693249faf2fc to your computer and use it in GitHub Desktop.
simple e-mail sender using AWS SES
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 python | |
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.mime.base import MIMEBase | |
from email import encoders | |
import sys | |
import glob | |
import os | |
SMTP_USER = os.environ['SMTP_USER'] | |
SMTP_PASS = os.environ['SMTP_PASS'] | |
def part(filename): | |
basename = os.path.basename(filename) | |
attachment = open(filename, "rb") | |
p = MIMEBase('application', 'octet-stream') | |
p.set_payload((attachment).read()) | |
encoders.encode_base64(p) | |
p.add_header('Content-Disposition', "attachment; filename= %s" % basename) | |
return p | |
def publish(source, destinations, subject, attachments): | |
msg = MIMEMultipart() | |
msg['From'] = source | |
msg['To'] = ', '.join(destinations) | |
msg['Subject'] = subject | |
body = "Relevant information is in the attached file." | |
msg.attach(MIMEText(body, 'plain')) | |
for filename in attachments: | |
msg.attach(part(filename)) | |
smtp = smtplib.SMTP("email-smtp.us-east-1.amazonaws.com", 587) | |
#smtp.set_debuglevel(1) | |
print (smtp.starttls()) | |
print (SMTP_USER, SMTP_PASS) | |
print (smtp.login(SMTP_USER, SMTP_PASS)) | |
print (smtp.sendmail(source, destinations, msg.as_string())) | |
if __name__=='__main__': | |
source = sys.argv[1] | |
destinations = sys.argv[2].split(';') | |
subject = sys.argv[3] | |
attachments = sys.argv[4].split(';') | |
publish( | |
source, | |
destinations, | |
subject, | |
attachments | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment