瀏覽代碼

fix: email structure
Now multipart/mixed is used always if possible as top level
content-type.

Mikael Koli 3 年之前
父節點
當前提交
845aae994c
共有 2 個文件被更改,包括 12 次插入6 次删除
  1. 0 6
      redmail/email/attachment.py
  2. 12 0
      redmail/email/sender.py

+ 0 - 6
redmail/email/attachment.py

@@ -18,12 +18,6 @@ class Attachments:
         self.encoding = encoding
 
     def attach(self, msg:EmailMessage):
-        if msg.get_content_type() == "text/plain":
-            # We need to change the content type
-            # This occurs if no body is defined or only text is defined
-            # The content type is therefore changed to multipart/mixed
-            # See issue #23
-            msg.make_mixed()
         for part in self._get_parts():
             msg.attach(part)
 

+ 12 - 0
redmail/email/sender.py

@@ -338,6 +338,18 @@ class EmailSender:
                 tables=body_tables,
                 jinja_params=self.get_html_params(extra=body_params, sender=sender)
             )
+
+        # Change the structure to multipart/mixed if possible.
+        # This seems to be the most versatile and most unproblematic top level content-type
+        # as otherwise content may be missing or it may be misrendered.
+        # See: https://stackoverflow.com/a/23853079/13696660
+        # See issues: #23, #37
+        try:
+            msg.make_mixed()
+        except ValueError:
+            # Cannot convert to mixed
+            pass
+        
         if attachments:
             att = Attachments(attachments, encoding=self.attachment_encoding)
             att.attach(msg)