Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.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脚本发送带有嵌入图像的html电子邮件_Python_Email - Fatal编程技术网

使用python脚本发送带有嵌入图像的html电子邮件

使用python脚本发送带有嵌入图像的html电子邮件,python,email,Python,Email,我是Python的新手。我想发送基于html的电子邮件,在邮件正文的左上角嵌入公司徽标 使用以下代码,电子邮件绝对可以工作,但不再附加嵌入的图像。我不知道我哪里弄错了。谁能帮帮我吗 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.MIMEImage import MIMEImage msg = MIMEMultipart('

我是Python的新手。我想发送基于html的电子邮件,在邮件正文的左上角嵌入公司徽标

使用以下代码,电子邮件绝对可以工作,但不再附加嵌入的图像。我不知道我哪里弄错了。谁能帮帮我吗

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()
导入smtplib
从email.mime.multipart导入MIMEMultipart
从email.mime.text导入MIMEText
从email.MIMEImage导入MIMEImage
msg=MIMEMultipart('alternative')
msg['Subject']=“我的文字日期为%s”%(今天)
msg['From']=发件人
msg['To']=接收器
html=”“”\

一些文本

""" #图像文件与脚本位于同一目录中 fp=open('logo.png','rb') msgImage=MIMEImage(fp.read()) fp.close() msgImage.add_头('Content-ID','') msg.attach(msgImage) part2=MIMEText(html,'html') 附加信息(第2部分) mailsrv=smtplib.SMTP('localhost')) mailsrv.sendmail(发送方、接收方、msg.as_string()) mailsrv.quit()
我解决了这个问题。这是更新后的代码供您参考

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()
导入smtplib
从email.mime.multipart导入MIMEMultipart
从email.mime.text导入MIMEText
从email.MIMEImage导入MIMEImage
msg=MIMEMultipart('相关')
msg['Subject']=“我的文字日期为%s”%(今天)
msg['From']=发件人
msg['To']=接收器
html=”“”\

一些文字。

""" #记录text/html的MIME类型。 part2=MIMEText(html,'html') #将部件附加到消息容器中。 附加信息(第2部分) #本例假定映像位于当前目录中 fp=open('logo.png','rb') msgImage=MIMEImage(fp.read()) fp.close() #如上所述定义图像的ID msgImage.add_头('Content-ID','') msg.attach(msgImage) #通过本地SMTP服务器发送邮件。 mailsrv=smtplib.SMTP('localhost')) mailsrv.sendmail(发送方、接收方、msg.as_string()) mailsrv.quit()
效果很好。。但是对于最新的python 3版本,MIMEIMAGE位于email.mime.image import MIMEIMAGE的
中。这个答案还正确地嵌入了图像,以便使用iOS邮件查看-我遇到了一个问题。谢谢。这太棒了。正是我需要的。好极了!