4

I am newbie to Python. I wanted to send html based email with company logo embedded on top left to the email body.

With the following code the email is absolutely working but not attaching the embedded image anymore. Not sure where i did mistake. Can anyone please help me out here.

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.MIMEImage import MIMEImage

msg = MIMEMultipart('alternative')
msg['Subject'] = "My text dated %s" % (today)
            msg['From'] = sender
            msg['To'] = receiver

html = """\
<html>
<head></head>
<body>
  <img src="cid:image1" alt="Logo" style="width:250px;height:50px;"><br>
  <p><h4 style="font-size:15px;">Some Text.</h4></p>
</body>
</html>
"""

# The image file is in the same directory as the script
fp = open('logo.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

msgImage.add_header('Content-ID', '<image1>')
msg.attach(msgImage)

part2 = MIMEText(html, 'html')
msg.attach(part2)

mailsrv = smtplib.SMTP('localhost')
mailsrv.sendmail(sender, receiver, msg.as_string())
mailsrv.quit()

3 Answers 3

18

I figured out the issue. Here is the updated code for your referance.

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.MIMEImage import MIMEImage

msg = MIMEMultipart('related')
msg['Subject'] = "My text dated %s" % (today)
msg['From'] = sender
msg['To'] = receiver

html = """\
<html>
  <head></head>
    <body>
      <img src="cid:image1" alt="Logo" style="width:250px;height:50px;"><br>
       <p><h4 style="font-size:15px;">Some Text.</h4></p>           
    </body>
</html>
"""
# Record the MIME types of text/html.
part2 = MIMEText(html, 'html')

# Attach parts into message container.
msg.attach(part2)

# This example assumes the image is in the current directory
fp = open('logo.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msg.attach(msgImage)

# Send the message via local SMTP server.
mailsrv = smtplib.SMTP('localhost')
mailsrv.sendmail(sender, receiver, msg.as_string())
mailsrv.quit()
Sign up to request clarification or add additional context in comments.

2 Comments

Works great.. But for recent python 3 versions, the MIMEIMAGE was in from email.mime.image import MIMEImage
This answer also properly embeds the image for viewing with iOS Mail - a problem I was having. Thanks.
1

In case you want something simple:

from redmail import EmailSender
email = EmailSender(host="smtp.myhost.com", port=1)

email.send(
    sender="[email protected]",
    subject="Example email",
    receivers=["[email protected]"],
    html="""
        <h1>Hi, take a look at this image:</h1>
        {{ my_image }}
    """,
    body_images={"my_image": "path/to/image.png"}
)

The image is embedded to the HTML using Jinja and it creates the img tag automatically. You may also directly pass bytes, matplotlib.Figure or PIL.Image.Image to body_images as well if you prefer those formats.

In case you want more control on the image's properties, like change the width and height:

email.send(
    sender="[email protected]",
    subject="Example email",
    receivers=["[email protected]"],
    html="""
        <h1>Hi, take a look at this image:</h1>
        <img src="{{ my_image.src }}" width=500 height=350>
    """,
    body_images={"my_image": "path/to/image.png"}
)

Pip install it from PyPI:

pip install redmail

There is a lot the library can do: include attachments from various forms, prettify and embed tables to HTML body, load templates from disk, parametrize etc. It is also well tested (100 % test coverage) and documented. I'm the author of the library and sorry for the self-promotion. I think it's awesome and worth sharing.

Documentation: https://red-mail.readthedocs.io/en/latest/index.html

Source code: https://github.com/Miksus/red-mail

1 Comment

I tried redmail and it worked superbly until recently when for some reason which I am unable to figure out - the embedded png image is being displayed as X in the outlook message. Can you please let me know a way around this?
0

use as "from email.mime.image import MIMEImage" instead of "from email.MIMEImage import MIMEImage" - Otherwise it works fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.