【发布时间】:2020-10-01 14:57:17
【问题描述】:
我是 python 的新手,我想构建一些东西来使用 python smtp 发送电子邮件,我想 还从全局路径嵌入图像,同时我还在 html 中传递参数
到目前为止,这是成功的。发送电子邮件。但图像没有嵌入邮件正文中,而是作为单独的附件发送我如何将所有内容嵌入一个正文中
msg = MIMEMultipart('related')
msg['Subject'] = "My text dated"
msg['From'] = MY_ADDRESS
msg['To'] = MY_ADDRESS
email = "someome@gmail.com"
name = "someome"
这是我试图传递 thress 参数以进行格式化的 html 标记
image_cid = make_msgid()
html = """\
<html>
<head></head>
<body>
<img src="cid:{image_cid}" alt="Logo" style="width:250px;height:50px;"><br>
<p><h4 style="font-size:15px;">email.{email}</h4></p>
<p><h4 style="font-size:15px;">Name.{name}</h4></p>
</body>
</html>
""".format(image_cid = image_cid[1:-1] ,email =email,name = name )
# Record the MIME types of text/html.
from glob import glob
list_of_images = glob('*.jpg')
这里循环遍历当前目录中所有格式的 jpg 图像
for filename in list_of_images:
fp = open(filename, 'rb')
msg_img = MIMEImage(fp.read())
fp.close()
msg_img.add_header('Content-ID', '<{}>'.format(filename))
msg_img.add_header('Content-Disposition', 'inline', filename=filename)
msg.attach(msg_img)
part2 = MIMEText(html, 'html')
# Attach parts into message container.
msg.attach(part2)
# Send the message via local SMTP server.
s = smtplib.SMTP(host='rtrt.rty.com', port=567)
s.starttls()
s.login(USERNAME, PASSWORD)
s.send_message(msg)
del msg
# Terminate the SMTP session and close the connection
s.quit()
【问题讨论】:
标签: python python-3.x python-2.7