python - 如何使用 smtplib 查看电子邮件中的文本文件和图像文件

标签 python python-3.x python-2.7

html = html_table + html_table1

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email import encoders
import datetime

me = "sender@gmail.com"
you = ['receiver@gmail.com']

subject = datetime.datetime.now().strftime("[%A %I:%M %P]")

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = me
msg['To'] = ",".join(you)

msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html')

filename = ("/home/EA.png")
attachment = open(filename,"rb")
msgImage = MIMEImage(attachment.read())

part1 = MIMEText(html, msgText, 'html')
html = html_table + html_table1
part = MIMEBase("application","octet-stream")
part.set_payload((attachment).read())
encoders.encode_base64(part)
encoders.encode_base64(msgImage)
part.add_header('Content-Disposition','attachment; filename= '+filename)
msgImage.add_header('Content-ID', '<image1>')

msg.attach(part1)
msg.attach(msgImage)

s = smtplib.SMTP('smtp.gmail.com',587)
s.sendmail(me, you, msg.as_string())
s.quit()

如何使用 smtplib 查看电子邮件中的文本文件和图像文件 -> 这里第一个文件嵌入在电子邮件中,第二个文件作为附件发送

例如,在上面的代码中,msg.attach(part1) -> 嵌入在电子邮件中,而 msg.attach(msgImage) -> 这是作为附件。 我想将这两个附件嵌入到电子邮件中。有人可以告诉我我在这里缺少什么吗

最佳答案

msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html')

We can edit it here as per the requirement

关于python - 如何使用 smtplib 查看电子邮件中的文本文件和图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52773871/

相关文章:

windows - 在 Windows 平台中将 Ghostscript 链接到 pypdfocr

python TCP套接字在recv方法上阻塞

python - 有没有办法在 plotly express 中实现堆叠或分组条形图

python - 从 ZMQ 子队列获取特定消息

python - 使用 BeautifulSoup 提取 &lt;script&gt; 的内容

python - 维基百科 AttributeError 直接运行

python - django.db.migrations.RenameModel 和 AutoField 序列名称

python - os.open vs open,使用什么

python - AttributeError : 'str' object has no attribute 'view' in Seaborn , 散点图

python - Flask:在网站而不是控制台上显示打印?