代码无法在邮件中显示图像

时间:2017-03-07 12:25:35

标签: python html

此处代码未获取图片的来源,只是在html alt=标记中显示img

图片未显示在邮件中 一切都正常,但我在gmail中看不到任何图像。

我已将图片和代码保存在同一个文件夹中

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()

1 个答案:

答案 0 :(得分:0)

如果所有代码都没问题,但您仍无法在电子邮件中看到该图片,请记住: 是否显示图像也是由您的电子邮件服务决定的,一些电子邮件服务将隐藏图像以解决安全问题。您可以尝试使用具有不同电子邮件服务的其他电子邮件地址。 PS:如果你想使用CSS调整你的html布局,“忘记它”。