Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
1,278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import smtplib
  2. import os
  3.  
  4. def send_email(host, port, username, password, mail_from, mail_to, subject, body):
  5. message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (mail_from, mail_to, subject, body)
  6. print (message)
  7. try:
  8. server = smtplib.SMTP(host, port)
  9. server.ehlo()
  10. server.starttls()
  11. server.login(username, password)
  12. server.sendmail(mail_from, mail_to, message)
  13. server.close()
  14. return True
  15. except Exception as ex:
  16. print (ex)
  17. return False
  18.  
  19. def get_recipient():
  20. # separate multiple recipient by comma. eg: "abc@gmail.com, xyz@gmail.com"
  21. return os.environ['MAIL_TO']
  22.  
  23. def lambda_handler(event, context):
  24. username = os.environ['USERNAME']
  25. password = os.environ['PASSWORD']
  26. host = os.environ['SMTPHOST']
  27. port = os.environ['SMTPPORT']
  28.  
  29. response = { "isBase64Encoded": False }
  30. to = get_recipient()
  31. subject = event['queryStringParameters']['subject']
  32. body = event['body']
  33.  
  34. success = send_email(host, port, username, password, username, to, subject, body)
  35. if success:
  36. response["statusCode"] = 200
  37. response["body"] = "message sent"
  38. else:
  39. response["statusCode"] = 400
  40. response["body"] = "message sending failed"
  41. return response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement