cookbook.rst 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. .. _cookbook:
  2. Cook Book
  3. =========
  4. This section provides various handy tips.
  5. .. _config-gmail:
  6. Gmail
  7. -----
  8. You need to make an app password `see this Google's answer <https://support.google.com/accounts/answer/185833>`_.
  9. When that is done you can use Red Mail's gmail object that has the Gmail
  10. server pre-configured:
  11. .. code-block:: python
  12. from redmail import gmail
  13. gmail.user_name = 'example@gmail.com' # Your Gmail user
  14. gmail.password = '<APP PASSWORD>'
  15. # And then you can send emails
  16. gmail.send(
  17. subject="Example email",
  18. receivers=['example@gmail.com']
  19. text="Hi, this is an email."
  20. )
  21. .. note::
  22. You can only send emails using your Gmail email address. Changing ``sender`` has no effect.
  23. Error Alerts
  24. ------------
  25. If you are building long running program (ie. web app) you can make a
  26. templated error alerts that include the full traceback:
  27. .. code-block:: python
  28. from redmail import EmailSender
  29. error_email = EmailSender(...)
  30. error_email.receivers = ['me@example.com']
  31. error_email.html = """<h2>An error encountered</h2>{{ error }}"""
  32. try:
  33. raise RuntimeError("Oops")
  34. except:
  35. # Send an email including the traceback
  36. error_email.send(subject="Fail: doing stuff failed")