在使用Python发送HTML内容的电子邮件时,可以通过以下步骤来保留HTML内部链接:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg['From'] = 'sender@example.com'
msg['To'] = 'recipient@example.com'
msg['Subject'] = 'HTML Email with Internal Links'
html_content = """
<html>
<body>
<h1>Welcome to my website!</h1>
<p>Click <a href="https://www.example.com">here</a> to visit my website.</p>
</body>
</html>
"""
msg.attach(MIMEText(html_content, 'html'))
# 配置SMTP服务器
smtp_server = 'smtp.example.com'
smtp_port = 587
smtp_username = 'your_username'
smtp_password = 'your_password'
# 创建SMTP连接
smtp_conn = smtplib.SMTP(smtp_server, smtp_port)
smtp_conn.starttls()
smtp_conn.login(smtp_username, smtp_password)
# 发送邮件
smtp_conn.sendmail(msg['From'], msg['To'], msg.as_string())
# 关闭SMTP连接
smtp_conn.quit()
这样,使用Python发送的电子邮件将保留HTML内部链接。请注意,确保替换示例中的发送者、接收者、SMTP服务器和凭据信息为实际值。
推荐的腾讯云相关产品:腾讯企业邮件(https://cloud.tencent.com/product/exmail)
领取专属 10元无门槛券
手把手带您无忧上云