Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #!/usr/bin/python
  2. # install boto using pip
  3.  
  4. import types
  5. from threading import Thread
  6. from boto import (
  7. ses,
  8. connect_s3
  9. )
  10.  
  11. # Amazon AWS config and credentials
  12. DEFAULT_AWS_REGION = "eu-west-1"
  13. AWS_ACCESS_KEY = "<Your-AWS-access-key>"
  14. AWS_SECRET_KEY = "<Your-AWS-secret-key>"
  15.  
  16. # create an connection for SES AWS service with default region
  17. ses_conn = ses.connect_to_region(
  18. DEFAULT_AWS_REGION,
  19. aws_access_key_id=AWS_ACCESS_KEY,
  20. aws_secret_access_key=AWS_SECRET_KEY
  21. )
  22.  
  23. def async(f):
  24. def wrapper(*args, **kwargs):
  25. thr = Thread(target=f, args=args, kwargs=kwargs)
  26. thr.start()
  27. return wrapper
  28.  
  29.  
  30. @async
  31. def send_email(_from, to, subject, body):
  32. """Function to send email through Amazon AWS SES service
  33. :param _from: a source email
  34. :param to: a list of recipients or a single email of recipient
  35. :param subject: a subject text
  36. :param body: a body text or html
  37. :return: None
  38. """
  39. recipients = []
  40.  
  41. # if a single recipient
  42. if isinstance(to, types.StringTypes):
  43. recipients.append(to)
  44. else:
  45. recipients = to
  46.  
  47. try:
  48. # send mail
  49. ses_conn.send_email(
  50. source=_from,
  51. subject=subject,
  52. body=body,
  53. to_addresses=recipients,
  54. format='html'
  55. )
  56. print "Email sent successfully to %s" % recipients
  57.  
  58. except Exception as e:
  59. raise Exception("Problem in sending email :: %s" % str(e))
  60.  
  61.  
  62. if __name__ == '__main__':
  63. # to send email one recipient
  64. send_email("noreply@testing.com",
  65. "user@testing.com",
  66. "Test Async Mail",
  67. "Hi, I am tesing this gist.")
  68.  
  69. # to send email multiple recipients
  70. send_email("noreply@testing.com",
  71. ["user1@testing.com", "user2@testing.com"],
  72. "Test Async Mail",
  73. "Hi, I am tesing this gist.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement