0

I am testing the sending of an email with an embeded png file. I use the code:

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

template ="""
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Image Test</h2>
<img src="/home/depot/wintergreen/mplplots/01.png">
</body>
</html>"""

MAIL_HOST = 'smtp.gmail.com:587'
MAIL_USER = 'xxx@gmail.com'
MAIL_PASSWORD = 'xxx'
MAIL_REPICIENTS = ['vincent.konate@gmail.com']        

message = MIMEMultipart('alternative')
message['Subject'] = "Test Image"

html = MIMEText(template, 'html', "utf-8")
message.attach(html)

smtp = SMTP(MAIL_HOST)
smtp.ehlo()
smtp.starttls()
smtp.login(MAIL_USER, MAIL_PASSWORD)
smtp.sendmail(MAIL_USER, MAIL_REPICIENTS, message.as_string())
smtp.close()

The sending works, however the image doesnt show up, instead this is what I see: enter image description here

I am testing with the Jupyter notebook; When i type

from IPython.display import Image
Image("/home/depot/wintergreen/mplplots/01.png")

The image shows, so the path is correct.

What could be wrong here?

jim jarnac
  • 4,804
  • 11
  • 51
  • 88
  • 2
    How do you think Gmail would access an image on your local hard drive?! You haven't embedded anything. – jonrsharpe Jun 04 '17 at 21:56
  • @jonrsharpe How do i attach the image to the email then? – jim jarnac Jun 04 '17 at 21:57
  • 2
    Did you try literally just searching for that? E.g. https://stackoverflow.com/q/7755501/3001761, https://stackoverflow.com/q/920910/3001761, https://stackoverflow.com/q/19171742/3001761, ... – jonrsharpe Jun 04 '17 at 21:58
  • @jonrsharpe Yes I looked up all those, but im not familiar with the concept so wasnt able to adapt it to my example. so i understand I should use content id? – jim jarnac Jun 04 '17 at 22:02
  • Then post a [mcve] of your attempt that explains where you got stuck. Given the accepted answers, that seems like it would be a good idea. – jonrsharpe Jun 04 '17 at 22:10

2 Answers2

0

You need to use email.utils.make_msgid to create an id an use it in your image tag srcattribute. You'll also need to embed the image into the message.

Check python's 3.5 email example 19.1.14 which shows how to embed files in and HTML.

Note that for 3.7 the API changed.

Grasshopper
  • 1,749
  • 1
  • 14
  • 30
-1

Your image URL needs to include the hostname. You have only:

<img src="/home/depot/wintergreen/mplplots/01.png">

You need something like:

<img src="http://myhostname.com/home/depot/wintergreen/mplplots/01.png">

Because when your user gets the email, there needs to be the domain.

Now, if the image isn't hosted somewhere, but is available on a filesystem accessible by all of your recipients (say, while reading on a corporate intranet) you can use a file URI rather than http. For example

<img src="file:///home/depot/wintergreen/mplplots/01.png">

Which will work for those recipients whose device can access the local directory for the file.

Finally, there is the option include the image itself within the email (rather than just a link to it). To actually include the image, you'll need to send a MIME message which, itself, includes the HTML, the Image and a reference from one to the other (the "link" within the HTML part would be like: <img src="cid:XXX"> where the cid refers to the Content-ID of the image. See Embed picture in email for details.

(You could use a data-uri directly in the HTML, but this has spotty support in email clients. See, for example, What is Data URI support like in major email client software?. You'd need to investigate if that's appropriate for your user base.)

pbuck
  • 4,291
  • 2
  • 24
  • 36
  • I believe that's the local directory `/home`, not something that's publicly hosted. – jonrsharpe Jun 04 '17 at 22:48
  • @jonrsharpe, that won't work either. If it's a local file it would need to have a file-type URL: `file:///mydir/filename.png`. As the OP indicates it's email, opening a local directory would be problematic unless all receivers of the email have the same local directory (maybe good assumption for internal corporate email, but seems a stretch given most email is now ready via mobile.) – pbuck Jun 05 '17 at 14:50
  • Well yes, exactly, which is why this answer doesn't help them at all. If the image was already public (or available to recipients at least) they could just put the right link in, but I don't think it is. – jonrsharpe Jun 05 '17 at 14:58