要使用Python3和电子邮件库发送带有图片的超文本标记语言(HTML)电子邮件,可以按照以下步骤进行:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
msg = MIMEMultipart()
msg['Subject'] = '邮件主题'
msg['From'] = '发件人邮箱'
msg['To'] = '收件人邮箱'
html = """
<html>
<body>
<p>这是一封带有图片的HTML邮件:</p>
<img src="cid:image1">
</body>
</html>
"""
msg.attach(MIMEText(html, 'html'))
with open('图片文件路径', 'rb') as f:
image_data = f.read()
image = MIMEImage(image_data)
image.add_header('Content-ID', '<image1>')
msg.attach(image)
smtp_server = 'SMTP服务器地址'
smtp_port = 'SMTP服务器端口'
smtp_username = 'SMTP用户名'
smtp_password = 'SMTP密码'
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.login(smtp_username, smtp_password)
server.send_message(msg)
注意:需要将代码中的以下内容替换为实际的信息:
推荐的腾讯云相关产品:腾讯云邮件推送(https://cloud.tencent.com/product/ses)
以上是使用Python3和电子邮件库发送带有图片的超文本标记语言电子邮件的完整步骤。
领取专属 10元无门槛券
手把手带您无忧上云