Search code examples
pythonhtmlemailjinja2mime

Python: Multipart html email is sent both with embedded image and attachment


I have checked many posts about it, but still cant find a solution. I am able to send email with embedded pictures, but the email also includes those pictures as attachments, and I need only embedded pictures. I have tried many variations, with 'related' type, 'mixed'. Also with html code inside Python program (not in Jinja2 template), but i cant make it work.

list_of_images = get_graphs() #list with file names

# here if I put "related" - images are sent ONLY as attachments 
mail = MIMEMultipart() 
for filename in list_of_images:
    fp = open(filename, 'rb')
    msg_img = MIMEImage(fp.read())
    fp.close()
    msg_img.add_header('Content-ID', '<{}>'.format(filename))
    msg_img.add_header('Content-Disposition', 'inline', filename=filename)
    mail.attach(msg_img)
#Jinja2 for html template
env = Environment(loader=FileSystemLoader('.'))
main = env.get_template('images.tpl')
html = main.render(pictures=list_of_images)
msgHtml = MIMEText(html, 'html')
mail.attach(msgHtml)

mail['Subject'] = "TEST"
mail['From'] = "email@addr"
mail['To'] = "email@addr"
s = smtplib.SMTP("localhost")
s.sendmail(mail['From'], "email@addr", mail.as_string())
s.quit()

jinja template:

<html>
<body>
{% for image in pictures %}
<img src="cid:{{image}}">
{% endfor %}
</body>
</html>

Solution

  • Attach the HTML first or specify a "start" parameter to the multipart/related content type.

    Quoting RFC2387:

    The start parameter, if given, is the content-ID of the compound object's "root". If not present the "root" is the first body part in the Multipart/Related entity. The "root" is the element the applications processes first.

    So, in your example, you might make these changes to mark the root element:

    mail = MIMEMultipart("related", start="<HTML>", type="text/html") 
    ...
    msgHtml.add_header('Content-ID', '<HTML>')
    

    At least in Google Mail, either placing the HTML first or adding the "start" parameter allows the images to be displayed inline.

    Complete example:

    from jinja2 import Template
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.mime.image import MIMEImage
    from glob import glob
    from getpass import getpass
    import smtplib
    
    me = 'example@gmail.com'
    you= 'example@gmail.com'
    auth = ('example@gmail.com', getpass())
    mx=  ('smtp.gmail.com', 465)
    
    list_of_images = glob('*.jpg')
    
    mail = MIMEMultipart("related")
    #Jinja2 for html template
    main = Template('''
        <html><body>
        {% for image in pictures %}<img src="cid:{{image}}">{% endfor %}
        </body></html>''')
    html = main.render(pictures=list_of_images)
    msgHtml = MIMEText(html, 'html')
    mail.attach(msgHtml)
    
    for filename in list_of_images:
        fp = open(filename, 'rb')
        msg_img = MIMEImage(fp.read())
        fp.close()
        msg_img.add_header('Content-ID', '<{}>'.format(filename))
        msg_img.add_header('Content-Disposition', 'inline', filename=filename)
        mail.attach(msg_img)
    
    mail['Subject'] = "TEST"
    mail['From'] = me
    mail['To'] = you
    
    s = smtplib.SMTP_SSL(*mx)
    s.login(*auth)
    s.sendmail(me, you, mail.as_string())
    s.quit()