Browse Source

add: image as dict work without maintype specified
It's redundant to specify maintype as it should be image when we embed
such to the body. Also slightly reworded the exception message to
prevent printing image data.

Mikael Koli 4 years ago
parent
commit
80a62226a5
1 changed files with 9 additions and 3 deletions
  1. 9 3
      redmail/email/body.py

+ 9 - 3
redmail/email/body.py

@@ -192,11 +192,17 @@ class HTMLBody(Body):
 
             elif isinstance(img, dict):
                 # Expecting dict explanation of bytes
-                # ie. {"maintype": "image", "subtype": "png", "content"}
-                required_keys = ("content", "maintype", "subtype")
+                # ie. {"maintype": "image", "subtype": "png", "content": b'...'}
+
+                # Setting defaults
+                img['maintype'] = img.get('maintype', 'image')
+
+                # Validation
+                required_keys = ("content", "subtype")
                 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)
-                    raise KeyError(f"Image {repr(img)} missing keys: {missing_keys}")
+                    raise KeyError(f"Dict representation of an image missing keys: {missing_keys}")
+                
                 img_content = img.pop("content")
                 kwds = img