templating.rst 1.3 KB

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