jinja_support.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. .. meta::
  2. :description: Send email in Python using Jinja.
  3. :keywords: send, email, Python, jinja
  4. .. _jinja-support:
  5. Jinja Support
  6. =============
  7. Red Mail uses Jinja for templating the HTML and text
  8. bodies. This enables a lot of features out-of-the box.
  9. See `Jinja documentation <https://jinja.palletsprojects.com/>`_
  10. for more details of the templating.
  11. Parametrizing Emails
  12. --------------------
  13. You can also parametrize an email using Jinja
  14. parameters:
  15. .. code-block:: python
  16. email.send(
  17. subject='email subject',
  18. receivers=['first.last@example.com'],
  19. html="""
  20. <h1>Hi {{ client }},</h1>
  21. <p>we are opening the source of {{ project_name }}.</p>
  22. <p>Kind regards
  23. <br>{{ company }}</p>
  24. """,
  25. text="""
  26. Hi {{ client }},
  27. we are opening the source of {{ project_name }}.
  28. Kind regards,
  29. {{ company }}
  30. """,
  31. body_params={
  32. 'client': 'Customer LTD',
  33. 'project_name': 'Red Mail',
  34. 'company': 'Company LTD',
  35. }
  36. )
  37. Both text and HTML body support Jinja parametrization.
  38. Default Parameters
  39. ^^^^^^^^^^^^^^^^^^
  40. There are also some parameters passed automatically for convenience.
  41. You can always override these if you wish. Here is a quick example of
  42. some of them:
  43. .. code-block:: python
  44. email.send(
  45. subject='email subject',
  46. receivers=['first.last@example.com'],
  47. html="""
  48. <h1>Hi,</h1>
  49. <p>nice to meet you</p>
  50. <p>Kind regards
  51. <br>{{ sender.full_name }}</p>
  52. """,
  53. )
  54. Here is a list of default parameters:
  55. ================ ==================================== =========================================================
  56. Parameter Name Type Description
  57. ================ ==================================== =========================================================
  58. sender :class:`redmail.models.EmailAddress` Format class of the email sender
  59. error :class:`redmail.models.Error` Format class of the current exception (if any)
  60. node str Computer’s network name (if can be determined)
  61. user str Name of the current user logged on the computer
  62. now datetime.datetime Current date and time
  63. ================ ==================================== =========================================================
  64. Including Loops and Control Flow
  65. --------------------------------
  66. As the bodies use Jinja in the background, you can use various additional features such
  67. as if statements, for loops, macros etc. Here is a quick illustration:
  68. .. code-block:: python
  69. email.send(
  70. subject='email subject',
  71. receivers=['first.last@example.com'],
  72. html="""
  73. <h1>Hi!</h1>
  74. <p>
  75. Soon you will meet my team.
  76. Here is a quick introduction:
  77. </p>
  78. <ul>
  79. {% for colleague in colleagues.items() %}
  80. <li>{{ colleague }}: {{ description }}</li>
  81. {% endfor %}
  82. </ul>
  83. {% if confidential %}
  84. <p>
  85. This message is confidential.
  86. </p>
  87. {% endif %}
  88. <p>Kind regards
  89. <br>{{ sender.full_name }}</p>
  90. """,
  91. body_params={
  92. 'colleagues': {'Jack': 'Developer', 'John': 'CEO'},
  93. 'confidential': False
  94. }
  95. )
  96. Please see `Jinja documentation <https://jinja.palletsprojects.com/>`_
  97. for more.
  98. Pass Unescaped Content
  99. ----------------------
  100. In case you need to include parts that should not be processed by
  101. Jinja, you may pass them using `markupsafe.Markup <https://markupsafe.palletsprojects.com/en/2.1.x/escaping/#markupsafe.Markup>`_:
  102. .. code-block:: python
  103. from markupsafe import Markup
  104. email.send(
  105. subject='email subject',
  106. receivers=['first.last@example.com'],
  107. html="""
  108. <h1>Hi,</h1>
  109. <p>{{ raw_content }}</p>
  110. <p>Kind regards
  111. <br>{{ sender.full_name }}</p>
  112. """,
  113. body_params={
  114. 'raw_content': Markup("<strong>this text is passed unescaped as is</strong>")
  115. }
  116. )
  117. .. warning::
  118. For HTML, content only from trusted sources should be left unescaped.
  119. Disabling Jinja
  120. ---------------
  121. In case you wish to pass raw text/HTML and don't want to use Jinja
  122. to render the bodies, you may also disable it:
  123. .. code-block:: python
  124. email.send(
  125. subject='email subject',
  126. receivers=['first.last@example.com'],
  127. text="""
  128. Hi,
  129. {{ these brackets are not processed }}
  130. """,
  131. html="""
  132. <h1>Hi!</h1>
  133. <p>
  134. {{ these brackets are not processed }}
  135. </p>
  136. """,
  137. use_jinja=False
  138. )
  139. You may also disable Jinja for all sent emails without passing the argument:
  140. .. code-block:: python
  141. email.use_jinja = False