Jelajahi Sumber

Merge pull request #43 from Miksus/issue/42

Use defined Jinja env with text and html
Mikael Koli 3 tahun lalu
induk
melakukan
230034cfb7
3 mengubah file dengan 53 tambahan dan 3 penghapusan
  1. 3 2
      redmail/email/body.py
  2. 2 0
      redmail/email/sender.py
  3. 48 1
      redmail/test/email/test_template.py

+ 3 - 2
redmail/email/body.py

@@ -39,9 +39,10 @@ class BodyImage:
 
 class Body:
 
-    def __init__(self, template:Template=None, table_template:Template=None, use_jinja=True):
+    def __init__(self, jinja_env:Environment, template:Template=None, table_template:Template=None, use_jinja=True):
         self.template = template
         self.table_template = table_template
+        self.jinja_env = jinja_env
         self.use_jinja = use_jinja
 
     def render_body(self, body:str, jinja_params:dict):
@@ -49,7 +50,7 @@ class Body:
             raise ValueError("Either body or template must be specified but not both.")
             
         if body is not None:
-            template = Environment().from_string(body)
+            template = self.jinja_env.from_string(body)
         else:
             template = self.template
         return template.render(**jinja_params)

+ 2 - 0
redmail/email/sender.py

@@ -338,6 +338,7 @@ class EmailSender:
             body = TextBody(
                 template=self.get_text_template(text_template),
                 table_template=self.get_text_table_template(),
+                jinja_env=self.templates_text,
                 use_jinja=use_jinja
             )
             body.attach(
@@ -351,6 +352,7 @@ class EmailSender:
             body = HTMLBody(
                 template=self.get_html_template(html_template),
                 table_template=self.get_html_table_template(),
+                jinja_env=self.templates_html,
                 use_jinja=use_jinja
             )
             body.attach(

+ 48 - 1
redmail/test/email/test_template.py

@@ -1,8 +1,10 @@
+from textwrap import dedent
+from jinja2 import Environment
 from redmail import EmailSender
 
 import pytest
 
-from convert import remove_email_extra
+from convert import remove_email_extra, remove_email_content_id
 from getpass import getpass, getuser
 from platform import node
 
@@ -49,6 +51,51 @@ def test_template(tmpdir):
     assert expected_html == html
     assert expected_text == text
 
+def test_jinja_env(tmpdir):
+
+    sender = EmailSender(host=None, port=1234)
+    env = Environment()
+    env.globals["my_param"] = "<a value>"
+    sender.templates_text = env
+    sender.templates_html = env
+    msg = sender.get_message(
+        sender="me@example.com",
+        receivers=["you@example.com"],
+        subject="Some news",
+        text="A param: {{ my_param }}",
+        html="<h1>A param: {{ my_param }}</h1>"
+    )
+    content = str(msg)
+    content = remove_email_content_id(content)
+    assert content == dedent("""
+    from: me@example.com
+    subject: Some news
+    to: you@example.com
+    MIME-Version: 1.0
+    Content-Type: multipart/mixed; boundary="===============<ID>=="
+
+    --===============<ID>==
+    Content-Type: multipart/alternative;
+     boundary="===============<ID>=="
+
+    --===============<ID>==
+    Content-Type: text/plain; charset="utf-8"
+    Content-Transfer-Encoding: 7bit
+
+    A param: <a value>
+
+    --===============<ID>==
+    Content-Type: text/html; charset="utf-8"
+    Content-Transfer-Encoding: 7bit
+    MIME-Version: 1.0
+
+    <h1>A param: <a value></h1>
+
+    --===============<ID>==--
+
+    --===============<ID>==--
+    """)[1:]
+
 def test_body_and_template_error(tmpdir):
 
     html_templates = tmpdir.mkdir("html_tmpl")