1

Here the code is not taking the source of the image it is just displaying the alt= in the html img tag

Image not displayed in the mail Every thing is working correctly but I could not see any image in the gmail.

I have kept the image and code in the same folder

import smtplib

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

# me == my email address
# you == recipient's email address
me = "my_gmail_address@gmail.com"
you = "sender_gmail_address@gmail.com"


msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow r you?\nHere is the link you wanted:\nhttps://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>An image that is a link:<br>
        <a href="https://www.w3schools.com">
        <img src="yy.jpg" alt="Go to W3Schools!" width="400" height="100" border="0">
    </p>
  </body>
</html>
"""
# Record the MIME types of both parts - text/plain and text/html.

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)


s = smtplib.SMTP('smtp.gmail.com',587)
s.ehlo()
s.starttls()
password=input("Enter your password")
s.login('my_gmail_address@gmail.com',password)
# sendmail function takes 3 arguments: sender's address, recipient's #address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()
Donald Duck
  • 8,409
  • 22
  • 75
  • 99

1 Answers1

0

If everything of the codes is fine, but you still cannot see the image in your email, please remember that: Whether to show an image is also detemined by your email service, some email services will just hide the image for security issue. You can try to use another email address with diferent email service. PS: if you want to use CSS to adjust the layout of your html, "FORGET IT".

Menglong Li
  • 2,177
  • 14
  • 19