【发布时间】:2019-08-29 03:23:44
【问题描述】:
我只能附加图像文件,但嵌入的图像文件不会出现在邮件中。它说链接图像无法显示文件可能被删除、删除或重命名。验证链接点到正确的图像文件和位置,并且附加的文件大小相同。下面是我使用的代码
from requests_toolbelt import MultipartEncoder
import requests
import smtplib
import base64
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from os.path import basename
from jinja2 import Template
def send_mail(send_from: str, subject: str, text: str,send_to: list, files= None):
send_to= default_address if not send_to else send_to
main = Template('''
<html><body>
{% for image in pictures %}<img src="cid:{{image}}">{% endfor %}
</body></html>''')
msg = MIMEMultipart()
html = main.render(pictures=files)
part2 = MIMEText(html, 'html')
msg.attach(part2)
msg['From'] = send_from
msg['To'] = ', '.join(send_to)
msg['Subject'] = subject
for f in files or []:
with open(f, "rb") as fil:
msgImage = MIMEImage(fil.read())
ext = f.split('.')[-1:]
attachedfile = MIMEApplication(fil.read(), _subtype = ext)
fil.close()
msgImage.add_header('Content-ID', '<{}>'.format(f))
msgImage.add_header('content-Disposition','inline',filename=f)
msg.attach(msgImage)
attachedfile.add_header(
'content-disposition', 'attachment', filename=basename(f) )
msg.attach(msgImage)
msg.attach(attachedfile)
smtp = smtplib.SMTP(host="smtp-mail.outlook.com", port= 25)
smtp.starttls()
smtp.login(usr,pwd)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
send_mail(send_from= frommail,
subject="Daily backup Testing",
text='files added: ',
send_to= tomail,
files= files_list)
我收到的邮件是。图像路径文件正确。当我打印时,我得到了这个files ['check123\\Screenshot (161).png', 'check123\\Screenshot (163).png', 'check123\\Screenshot (164).png']
我该如何解决这个问题?
【问题讨论】:
标签: python python-3.x email jinja2