jinja_support.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. .. _jinja-support:
  2. Jinja Support
  3. =============
  4. Red Mail uses Jinja for templating the HTML and text
  5. bodies. This enables a lot of features out-of-the box.
  6. See `Jinja documentation <https://jinja.palletsprojects.com/>`_
  7. for more details of the templating.
  8. Parametrizing Emails
  9. --------------------
  10. You can also parametrize an email using Jinja
  11. parameters:
  12. .. code-block:: python
  13. email.send(
  14. subject='email subject',
  15. receivers=['first.last@example.com'],
  16. html="""
  17. <h1>Hi {{ client }},</h1>
  18. <p>we are opening the source of {{ project_name }}.</p>
  19. <p>Kind regards
  20. <br>{{ company }}</p>
  21. """,
  22. text="""
  23. Hi {{ client }},
  24. we are opening the source of {{ project_name }}.
  25. Kind regards,
  26. {{ company }}
  27. """,
  28. body_params={
  29. 'client': 'Customer LTD',
  30. 'project_name': 'Red Mail',
  31. 'company': 'Company LTD',
  32. }
  33. )
  34. Both text and HTML body support Jinja parametrization.
  35. Default Parameters
  36. ^^^^^^^^^^^^^^^^^^
  37. There are also some parameters passed automatically for convenience.
  38. You can always override these if you wish. Here is a quick example of
  39. some of them:
  40. .. code-block:: python
  41. email.send(
  42. subject='email subject',
  43. receivers=['first.last@example.com'],
  44. html="""
  45. <h1>Hi,</h1>
  46. <p>nice to meet you</p>
  47. <p>Kind regards
  48. <br>{{ sender.full_name }}</p>
  49. """,
  50. )
  51. Here is a list of default parameters:
  52. ================ ==================================== =========================================================
  53. Parameter Name Type Description
  54. ================ ==================================== =========================================================
  55. sender :class:`redmail.models.EmailAddress` Format class of the email sender
  56. error :class:`redmail.models.Error` Format class of the current exception (if any)
  57. node str Computer’s network name (if can be determined)
  58. user str Name of the current user logged on the computer
  59. now datetime.datetime Current date and time
  60. ================ ==================================== =========================================================
  61. Including Loops and Control Flow
  62. --------------------------------
  63. As the bodies use Jinja in the background, you can use various additional features such
  64. as if statements, for loops, macros etc. Here is a quick illustration:
  65. .. code-block:: python
  66. email.send(
  67. subject='email subject',
  68. receivers=['first.last@example.com'],
  69. html="""
  70. <h1>Hi!</h1>
  71. <p>
  72. Soon you will meet my team.
  73. Here is a quick introduction:
  74. </p>
  75. <ul>
  76. {% for colleague in colleagues.items() %}
  77. <li>{{ colleague }}: {{ description }}</li>
  78. {% endfor %}
  79. </ul>
  80. {% if confidential %}
  81. <p>
  82. This message is confidential.
  83. </p>
  84. {% endif %}
  85. <p>Kind regards
  86. <br>{{ sender.full_name }}</p>
  87. """,
  88. body_params={
  89. 'colleagues': {'Jack': 'Developer', 'John': 'CEO'},
  90. 'confidential': False
  91. }
  92. )
  93. Please see `Jinja documentation <https://jinja.palletsprojects.com/>`_
  94. for more.