用python,html + css发送电子邮件,阅读.txt文件
问题描述:
我试图编写一些非常简单的代码。这个想法是读取一个HTML代码写入的.txt文件。具体来说,它是一个表格,其中包含一些css和样式修饰符:用python,html + css发送电子邮件,阅读.txt文件
<td style=""background-color:red"" >-49,2%</td>
我可以成功读取文件的内容并发送电子邮件。但是,发送电子邮件时,不会应用背景样式。桌子的其余部分是完美创建的。 我的代码是这样的:
fp = open(r"filepath", 'r',encoding='utf-8-sig')
# Create a text/plain message
msg = MIMEText(fp.read()) #Here I also tried with the argument 'html'
fp.close()
s.sendmail("email1", "email2",msg.as_string())
有什么办法,我也可以得到应用的风格?电子邮件服务提供者可以在Gmail和我知道它可以工作,因为如果我尝试使用“味精”变量为文字字符串,如:
msg= r"<table...> <td style=""background-color:red"" >-49,2%</td> </table>"
它正确地发送背景格式。这个问题必须是其他类型的。
非常感谢!
答
已解决。
我希望它有帮助, modifier.as_string()是问题。删除它:
s.sendmail("email1", "email2",msg.as_string())
到
s.sendmail("email1", "email2",msg)
使它工作。