Skip to content

Instantly share code, notes, and snippets.

@samueltc
Created Jul 22, 2020
Embed
What would you like to do?
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