Skip to content

Instantly share code, notes, and snippets.

@rafalf
Last active September 9, 2016 19:26
Show Gist options
  • Save rafalf/5067eedbf93721064a7ac55297142f5b to your computer and use it in GitHub Desktop.
Save rafalf/5067eedbf93721064a7ac55297142f5b to your computer and use it in GitHub Desktop.
Send Email - Amazon SES SMTP Endpoint (TLS)
#
# to send emails via Amazon SES SMTP (TLS)
#
def sendMail(self, subject, email_content):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
receipients = ['[email protected]','[email protected]']
smtp_server = 'email-smtp.us-east-1.amazonaws.com'
from_email = '[email protected]'
smtp_port = '587'
smtp_username = USER_NAME
smtp_password = PASSWORD
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = COMMASPACE.join(receipients)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(email_content))
#for file in files:
# part = MIMEBase('application', "octet-stream")
# part.set_payload(open(file, "rb").read())
# Encoders.encode_base64(part)
# part.add_header('Content-Disposition', 'attachment; filename="%s"'
# % os.path.basename(file))
# msg.attach(part)
smtp =smtplib.SMTP(
host = smtp_server,
port = smtp_port,
timeout = 10
)
smtp.set_debuglevel(10)
smtp.starttls()
smtp.ehlo()
smtp.login(smtp_username, smtp_password)
msg = msg.as_string()
msg = msg.replace("text/plain", "text/html")
smtp.sendmail(from_email, receipients, msg)
smtp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment