Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import smtplib
- import os
- def send_email(host, port, username, password, mail_from, mail_to, subject, body):
- message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (mail_from, mail_to, subject, body)
- print (message)
- try:
- server = smtplib.SMTP(host, port)
- server.ehlo()
- server.starttls()
- server.login(username, password)
- server.sendmail(mail_from, mail_to, message)
- server.close()
- return True
- except Exception as ex:
- print (ex)
- return False
- def get_recipient():
- # separate multiple recipient by comma. eg: "abc@gmail.com, xyz@gmail.com"
- return os.environ['MAIL_TO']
- def lambda_handler(event, context):
- username = os.environ['USERNAME']
- password = os.environ['PASSWORD']
- host = os.environ['SMTPHOST']
- port = os.environ['SMTPPORT']
- response = { "isBase64Encoded": False }
- to = get_recipient()
- subject = event['queryStringParameters']['subject']
- body = event['body']
- success = send_email(host, port, username, password, username, to, subject, body)
- if success:
- response["statusCode"] = 200
- response["body"] = "message sent"
- else:
- response["statusCode"] = 400
- response["body"] = "message sending failed"
- return response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement