templating.rst 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. .. meta::
  2. :description: Send templated email in Python using Jinja.
  3. :keywords: send, email, Python, jinja, environment
  4. .. _templating:
  5. Using Templates
  6. ===============
  7. As templating relies on Jinja, you can set the
  8. template path to a custom folder and
  9. .. code-block:: python
  10. from redmail import EmailSender
  11. email = EmailSender(host="localhost", port=0)
  12. email.set_template_paths(
  13. html="path/html/templates",
  14. text="path/text/templates",
  15. )
  16. .. note::
  17. If you are dissatisfied with default HTML and text
  18. table templates, you can also pass ``html_table``
  19. and ``text_table`` to specify the templates used
  20. to render embedded tables:
  21. .. code-block:: python
  22. email.set_template_paths(
  23. html_table="path/html/tables",
  24. text_table="path/text/tables",
  25. )
  26. Next we will make a simple template, let's call it
  27. ``event_card.html``:
  28. .. code-block:: html
  29. <h1>Hi {{ participant }}!</h1>
  30. <p>
  31. Thank you for being a valuable member of our
  32. community! We are organizing an event
  33. {{ event_name }} and we would like to invite
  34. you.
  35. </p>
  36. <p>Kind regards,<br>
  37. <em>{{ organizer }} </em>
  38. </p>
  39. Then we can use this template:
  40. .. code-block:: python
  41. email.send(
  42. subject='email subject',
  43. receivers=['first.last@example.com'],
  44. html_template='event_card.html',
  45. body_params={
  46. 'participant': 'Jack',
  47. 'event_name': 'Open data',
  48. 'organizer': 'Organization.org'
  49. }
  50. )