Skip to content

Instantly share code, notes, and snippets.

@samueltc
Created July 22, 2020 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samueltc/970d0aa8d69df4a29685693249faf2fc to your computer and use it in GitHub Desktop.
Save samueltc/970d0aa8d69df4a29685693249faf2fc to your computer and use it in GitHub Desktop.
simple e-mail sender using AWS SES
#!/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