email_utils.py 955 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # using SendGrid's Python Library
  2. # https://github.com/sendgrid/sendgrid-python
  3. from sendgrid import SendGridAPIClient
  4. from sendgrid.helpers.mail import Mail
  5. from app.config import SUPPORT_EMAIL, SENDGRID_API_KEY, ENV
  6. from app.log import LOG
  7. def send(to_email, subject, html_content):
  8. # On local only print out email content
  9. if ENV == "local":
  10. LOG.d(
  11. "send mail to %s, subject:%s, content:%s", to_email, subject, html_content
  12. )
  13. return
  14. message = Mail(
  15. from_email=SUPPORT_EMAIL,
  16. to_emails=to_email,
  17. subject=subject,
  18. html_content=html_content,
  19. )
  20. sg = SendGridAPIClient(SENDGRID_API_KEY)
  21. response = sg.send(message)
  22. LOG.d("sendgrid res:%s, email:%s", response.status_code, to_email)
  23. def notify_admin(subject, html_content):
  24. send(
  25. SUPPORT_EMAIL,
  26. subject,
  27. f"""
  28. <html><body>
  29. {html_content}
  30. </body></html>""",
  31. )