I realized how painful some of the things are with SMTP and email libraries and I thought I have to do something with it. I made a library that makes embedding images to HTML way easier:
from redmail import EmailSender
email = EmailSender(host="<SMTP HOST>", port=0)
email.send(
sender="[email protected]",
receivers=["[email protected]"]
subject="An email with image",
html="""
<h1>Look at this:</h1>
{{ my_image }}
""",
body_images={
"my_image": "path/to/image.png"
}
)
Sorry for promotion but I think it's pretty awesome. You can supply the image as Matplotlib Figure
, Pillow Image
or just as bytes
if your image is in those formats. It uses Jinja for templating.
If you need to control the size of the image, you can also do this:
email.send(
sender="[email protected]",
receivers=["[email protected]"]
subject="An email with image",
html="""
<h1>Look at this:</h1>
<img src="{{ my_image.src }}" width=200 height=300>
""",
body_images={
"my_image": "path/to/image.png"
}
)
You can just pip install it:
pip install redmail
It's (hopefully) all you need for email sending (has a lot more) and it is well tested. I also wrote extensive documentation: https://red-mail.readthedocs.io/en/latest/ and source code is found here.