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/