HTML格式的Python电子邮件mimelib
我试图在一封从Python脚本发送的电子邮件中以html格式发送用Python创建的两个数据帧 我想写一个文本和表,并重复两个以上的数据帧,但脚本不能附加多个html块。 代码如下:HTML格式的Python电子邮件mimelib,python,email,pandas,mime,Python,Email,Pandas,Mime,我试图在一封从Python脚本发送的电子邮件中以html格式发送用Python创建的两个数据帧 我想写一个文本和表,并重复两个以上的数据帧,但脚本不能附加多个html块。 代码如下: import numpy as np import pandas as pd import smtplib import time from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText sender
import numpy as np
import pandas as pd
import smtplib
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender = "blabla@gmail.com"
recipients = ['albalb@gmail.com']
msg = MIMEMultipart('alternative')
msg['Subject'] = "This a reminder call " + time.strftime("%c")
msg['From'] = sender
msg['To'] = ", ".join(recipients)
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"
html = df[['SYMBOL','ARBITRAGE BASIS %']].to_html()
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
username = 'blabla@gmail.com'
password = 'blahblah'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(sender, recipients, msg.as_string())
server.quit()
print("Success")
我收到一封邮件,邮件正文中最后一部分是格式化的html表格。第1部分文本未出现。怎么了?问题是,您正在将这些部分标记为
多部分/备选方案
——这意味着,“我有多个渲染中的信息;选择您喜欢的一个”,您的电子邮件客户端显然已设置为选择HTML版本。事实上,这两个部分都在那里,但您已经将它们标记为其中一个/或您显然想要两者的位置
传统的快速修复方法是切换到multipart/related
,但实际上,简单地说内容在别处的文本部分的目的是什么
如果您想将HTML作为附件,还可以为HTML部分设置内容处置:附件(并提供文件名)。使用(完全公开:我是维护者/开发者):
yagmail的存在使我们能够非常方便地使用附件、图像和html等发送电子邮件
开始使用安装它
pip install yagmail
这是我需要的电子邮件格式,是后续问题、新问题、对我答案的评论,还是只是一般性评论?这个答案有助于你实现你的目标吗?
pip install yagmail