Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. import smtplib
  2. import json
  3.  
  4.  
  5. from email.mime.multipart import MIMEMultipart
  6. from email.mime.text import MIMEText
  7.  
  8. args = []
  9. msg = MIMEMultipart('alternative')
  10. msg['From'] = 'noreply@example.com'
  11. html = open('mail.html').read()
  12.  
  13.  
  14. EMAIL_HOST = 'email-smtp...'
  15. EMAIL_HOST_USER = 'sss'
  16. EMAIL_HOST_PASSWORD = 'ssssss'
  17. EMAIL_PORT = 587
  18.  
  19. def lambda_handler(event, context):
  20. body = event['Records'][0]['Sns']['Message']
  21. global args
  22. args = json.loads(body)['args']
  23. set_worker(json.loads(body)['method'])()
  24.  
  25. return 'success'
  26.  
  27.  
  28. def set_worker(method):
  29. return {
  30. 'email' : email
  31. }.get(method, 'Not found')
  32.  
  33.  
  34. def email():
  35. global msg, html
  36.  
  37. name = args[0]
  38. title = args[1]
  39. msg_body = args[2]
  40. email = args[3]
  41. url = args[4]
  42. subject = "Test"
  43.  
  44. msg['Subject'] = subject
  45. msg['To'] = email
  46.  
  47. html = html.format(title, community_name, title, msg_body, community_name)
  48. mime_text = MIMEText(html, 'html')
  49. msg.attach(mime_text)
  50. send_message()
  51.  
  52. def send_message():
  53. mail = smtplib.SMTP(EMAIL_HOST, EMAIL_PORT)
  54. mail.ehlo()
  55. mail.starttls()
  56. mail.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
  57. mail.sendmail(msg['From'], msg['To'], msg.as_string())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement