Просмотр исходного кода

fix: support non-PNG embedded images as dict

Mikael Koli 4 лет назад
Родитель
Сommit
53e5b97d70
1 измененных файлов с 20 добавлено и 12 удалено
  1. 20 12
      redmail/email/body.py

+ 20 - 12
redmail/email/body.py

@@ -185,8 +185,10 @@ class HTMLBody(Body):
             if is_bytes(img) or isinstance(img, BytesIO):
             if is_bytes(img) or isinstance(img, BytesIO):
                 # We just assume the user meant PNG. If not, it should have been specified
                 # We just assume the user meant PNG. If not, it should have been specified
                 img_content = img.read() if hasattr(img, "read") else img
                 img_content = img.read() if hasattr(img, "read") else img
-                maintype = "image"
-                subtype  = "png"
+                kwds = {
+                    'maintype': 'image',
+                    'subtype': 'png',
+                }
 
 
             elif isinstance(img, dict):
             elif isinstance(img, dict):
                 # Expecting dict explanation of bytes
                 # Expecting dict explanation of bytes
@@ -195,9 +197,8 @@ class HTMLBody(Body):
                 if any(key not in img for key in required_keys):
                 if any(key not in img for key in required_keys):
                     missing_keys = tuple(key for key in required_keys if key not in img)
                     missing_keys = tuple(key for key in required_keys if key not in img)
                     raise KeyError(f"Image {repr(img)} missing keys: {missing_keys}")
                     raise KeyError(f"Image {repr(img)} missing keys: {missing_keys}")
-                img_content = img["content"]
-                maintype = "image"
-                subtype = "png"
+                img_content = img.pop("content")
+                kwds = img
 
 
             elif isinstance(img, Path) or (isinstance(img, str) and Path(img).is_file()):
             elif isinstance(img, Path) or (isinstance(img, str) and Path(img).is_file()):
                 path = img
                 path = img
@@ -205,20 +206,28 @@ class HTMLBody(Body):
                 
                 
                 with open(path, "rb") as img:
                 with open(path, "rb") as img:
                     img_content = img.read()
                     img_content = img.read()
+                kwds = {
+                    'maintype': maintype,
+                    'subtype': subtype,
+                }
             elif plt is not None and isinstance(img, plt.Figure):
             elif plt is not None and isinstance(img, plt.Figure):
                 buf = BytesIO()
                 buf = BytesIO()
                 img.savefig(buf, format='png')
                 img.savefig(buf, format='png')
                 buf.seek(0)
                 buf.seek(0)
                 img_content = buf.read()
                 img_content = buf.read()
-                maintype = "image"
-                subtype  = "png"
+                kwds = {
+                    'maintype': 'image',
+                    'subtype': 'png',
+                }
             elif PIL is not None and isinstance(img, PIL.Image.Image):
             elif PIL is not None and isinstance(img, PIL.Image.Image):
                 buf = BytesIO()
                 buf = BytesIO()
                 img.save(buf, format='PNG')
                 img.save(buf, format='PNG')
                 buf.seek(0)
                 buf.seek(0)
                 img_content = buf.read()
                 img_content = buf.read()
-                maintype = "image"
-                subtype  = "png"
+                kwds = {
+                    'maintype': 'image',
+                    'subtype': 'png',
+                }
             else:
             else:
                 # Cannot be figured out
                 # Cannot be figured out
                 if isinstance(img, str):
                 if isinstance(img, str):
@@ -227,7 +236,6 @@ class HTMLBody(Body):
 
 
             msg_body.add_related(
             msg_body.add_related(
                 img_content,
                 img_content,
-                maintype=maintype,
-                subtype=subtype,
-                cid=cid
+                cid=cid,
+                **kwds
             )
             )