Guest User

Untitled

a guest
Dec 6th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding=utf8
  3. """
  4. Send email with SMTP over STARTTLS
  5.  
  6. Add env vars in your ~/.bashrc and source it:
  7.  
  8. export EMAIL_HOST=smtp.example.com
  9. export EMAIL_PORT=587
  10. export EMAIL_USERNAME=sender@example.com
  11. export EMAIL_PASSWORD=PASSWORD
  12. export EMAIL_TO_LIST=foo@gmail.com,bar@example.com
  13.  
  14. """
  15. import os
  16. import smtplib
  17.  
  18. EMAIL_HOST = os.environ['EMAIL_HOST']
  19. EMAIL_PORT = int(os.environ['EMAIL_PORT'])
  20.  
  21. EMAIL_USERNAME = os.environ['EMAIL_USERNAME']
  22. EMAIL_PASSWORD = os.environ['EMAIL_PASSWORD']
  23.  
  24. # comma separated email list
  25. EMAIL_TO_LIST = os.environ['EMAIL_TO_LIST'].split(',')
  26.  
  27. EMAIL_MESSAGE = "From: {}\r\nTo: {}\r\nSubject:{}\r\n\r\n{}".format(
  28. EMAIL_USERNAME,
  29. ','.join(EMAIL_TO_LIST),
  30. 'this is email subject',
  31. 'this is email body')
  32.  
  33. if EMAIL_PORT == 25:
  34. # smtp default, may be deprecated
  35. server = smtplib.SMTP(EMAIL_HOST, EMAIL_PORT)
  36. elif EMAIL_PORT == 587:
  37. # smtp over starttls, a improved solution
  38. server = smtplib.SMTP(EMAIL_HOST, EMAIL_PORT)
  39. server.starttls()
  40. elif EMAIL_PORT == 465:
  41. # smtp over ssl, a final solution
  42. server = smtplib.SMTP_SSL(EMAIL_HOST, EMAIL_PORT)
  43.  
  44. server.set_debuglevel(1)
  45. server.login(EMAIL_USERNAME, EMAIL_PASSWORD)
  46. server.sendmail(EMAIL_USERNAME, EMAIL_TO_LIST, EMAIL_MESSAGE)
  47. server.quit()
Add Comment
Please, Sign In to add comment