【发布时间】:2022-01-23 08:54:03
【问题描述】:
使用以下代码发送电子邮件时,结果与预期不符。缺少图像,并且模板未按应有的方式居中。我提供存在的内容与预期的内容。我不确定问题出在HTML 还是模板的阅读上。
python 代码:
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class Mail:
def __init__(self):
self.sender_mail = 'someone@gmail.com'
self.password = 'my_pw'
self.smtp_server_domain_name = "smtp.gmail.com"
self.port = 465
def send(self, emails):
ssl_context = ssl.create_default_context()
service = smtplib.SMTP_SSL(self.smtp_server_domain_name, self.port, context=ssl_context)
service.login(self.sender_mail, self.password)
for email in emails:
mail = MIMEMultipart('mixed')
mail['Subject'] = 'Celebrations'
mail['From'] = self.sender_mail
mail['To'] = email
html_file = open('./templates/index.html')
html_content = MIMEText(html_file.read(), 'html')
mail.attach(html_content)
service.sendmail(self.sender_mail, email, mail.as_string())
service.quit()
if __name__=='__main__':
mail = Mail()
mail.send(emails=['you@gmail.com'])
【问题讨论】:
-
问题可能是 HTML。如果 Outlook 接收到它,那么您的一半代码正在运行。对于渲染,请尝试内联样式。
标签: python html email email-templates