Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将.png嵌入HTML电子邮件?_Python_Html_Email_Smtp - Fatal编程技术网

Python 如何将.png嵌入HTML电子邮件?

Python 如何将.png嵌入HTML电子邮件?,python,html,email,smtp,Python,Html,Email,Smtp,我目前使用以下代码每天向用户发送3次电子邮件(包含报告)。我想在这封邮件中添加一个图表,但似乎不知道如何添加 def HTML_Email(subject, to, html, files, filename): import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from e

我目前使用以下代码每天向用户发送3次电子邮件(包含报告)。我想在这封邮件中添加一个图表,但似乎不知道如何添加

def HTML_Email(subject, to, html, files, filename):
    import smtplib  
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText                    
    from email.mime.application import MIMEApplication
    from os.path import basename
    import email
    import email.mime.application

    # Create message container - the correct MIME type is 
multipart/alternative.
    msg = MIMEMultipart('mixed')
    msg['Subject'] = subject
    msg['From'] = "ChicagoGISScripts@mobilitie.com"
    msg['To'] = ", ".join(to)

    # Record the MIME types of both parts - text/plain and text/html
    part2 = MIMEText(html, 'html')

    # create PDF attachment
    fp=open(files,'rb')
    att = email.mime.application.MIMEApplication(fp.read(),_subtype="xlsx")
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)

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

# Send the message via local SMTP server.
user = 'ausername'
pwd = 'apassword'
s = smtplib.SMTP('smtp.office365.com',587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(user,pwd)
s.sendmail(msg['From'], to, msg.as_string())
s.quit()
通常情况下,我会使用类似这样的东西来处理它,但我正在尝试包含一个存储在本地计算机上的.png。如果我不想在邮件正文中嵌入图片,我还缺少什么

html = """\
<html>
  <head></head>
  <body>
    <p><font face ="Gotham, monospace">Some HTML,<br><br>
       <img src="C:\\Users\\Me\\Desktop\\graphs.png"></img></font>
    </p>
  </body>
</html>
"""
html=”“”\
一些HTML,

"""
由于您没有在服务器上托管图像,因此无法使用普通链接将其嵌入电子邮件中。尝试将.png文件编码为,并将其设置为
src

编辑

请参阅,了解如何在python中执行此操作

编辑2

生成的html应该如下所示

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />

这正是雷迪尔巴所说的。您必须在服务器上托管电子邮件,并且在电子邮件正文中具有绝对src

而不是:

<img src="C:\\Users\\Me\\Desktop\\graphs.png">

你需要:

<img src="http://www.somedomain.com/images/graphs.png" alt="Name' />


所以如果我有
encoded=base64.b64encode(open(“filename.png”,“rb”).read())
我会做
?似乎不起作用,因为图像只是在电子邮件中显示为错误。
encoded
是base64编码字符串,因此您只需将该字符串打印到
src=“…”
部分。看看我链接的wikipedia文章,看看如果你做得正确(在“用法示例”一节下),它应该是什么样子。嗯,我已经尝试了多种方法,但仍然没有正确显示。我甚至把这张照片放在一个网站上,并通过网站给它打电话。我的代码中是否有什么东西阻止它正确解析?我需要MIMEImage吗?编辑我甚至尝试过从其他网站链接,所以我认为这一定是我的代码中的内容…尝试复制生成的html并粘贴到这里。也许还有别的事情发生了。
“\测试