email_utils.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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, plain_content=None):
  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. if not plain_content:
  15. plain_content = subject
  16. message = Mail(
  17. from_email=SUPPORT_EMAIL,
  18. to_emails=to_email,
  19. subject=subject,
  20. html_content=html_content,
  21. plain_text_content=plain_content,
  22. )
  23. sg = SendGridAPIClient(SENDGRID_API_KEY)
  24. response = sg.send(message)
  25. LOG.d("sendgrid res:%s, email:%s", response.status_code, to_email)
  26. def notify_admin(subject, html_content):
  27. send(
  28. SUPPORT_EMAIL,
  29. subject,
  30. f"""
  31. <html><body>
  32. {html_content}
  33. </body></html>""",
  34. )