HTML 和 Python 驱动电子邮件中的文本
HTML and Text in Python Driven Email
我正在使用 MIMEMultipart 从 Python 发送电子邮件。代码如下:
sender = "EMAIL"
recipients = ["EMAIL"]
msg = MIMEMultipart('alternative')
msg['Subject'] = "Subject Text"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
html = PandasDataFrame.to_html()
part2 = MIMEText(html, 'html')
msg.attach(part2)
SERVER = "SERVER"
server = smtplib.SMTP(SERVER)
server.sendmail(sender, recipients, msg.as_string())
server.quit()
这会插入一个 Python Pandas 数据框作为 HTML 并且工作正常。是否也可以将脚注作为文本添加到电子邮件正文中?代码将如何同时完成这两项工作?或者,我可以将评论添加为 HTML,但更多或更少需要在电子邮件正文中添加一些脚注。
谢谢
所以下面的没用,看看
也许是这个?
html = pd.DataFrame([[1,2,3], ['dog', 'cat', 42]]).to_html()
part1 = MIMEText(html, 'html')
msg.attach(part1)
part2 = MIMEText('html')
coolstring = 'This is a dope-ass DataFrame yo'
part2.set_payload(coolstring)
msg.attach(part2)
尽管我认为它与下面的#2 太相似了。输出如下:
>> print msg
# ...header with my email whoops...
--===============0888735609==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
<table border="1" class="dataframe">
#...DataFrame html...
</table>
--===============0888735609==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
This is a dope-ass DataFrame yo
--===============0888735609==--
通过 the examples 找到了几种方法,并通过等效于 dir(MIMEMultipart)
列出了 MIMEMultipart
上的方法
您必须验证的三个猜测:
1) 您可以通过
设置结语
msg.epilogue = 'This is a dope-ass DataFrame yo'
但是不确定这会出现在电子邮件正文中的何处或是否结束。
2) 创建另一个 MIMEText 并附加它。这似乎是他们在示例中一次发送大量图片的方式,因此这可能是您最好的选择。可能应该以此为首。
part_text = MIMEText('This is some text, yessir')
msg.attach(part_text)
看起来有点像,因为边界划分是一样的。
>> print msg.as_string()
Content-Type: multipart/alternative; boundary="===============1672307235=="
MIME-Version: 1.0
Subject: Subject Text
From: EMAIL
To: EMAIL
--===============1672307235==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
<table border="1" class="dataframe">
# ...DataFrame in HTML here...
</table>
--===============1672307235==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
This is some text, yessir
--===============1672307235==--
3) 由于要在 server.sendmail(sender, recipients, msg.as_string())
中实际发送它,您将 msg
转换为字符串,您的另一个选择是手动将一些 HTML 文本添加到 msg.as_string()
直接地。像
msg.as_string().replace('</table>', '</table>\n<p>...your text here</p>')
会很乱,但应该可以。
如果这些有帮助,请告诉我!我有点在黑暗中拍摄,因为我现在无法测试。祝你好运!
此代码有效:
首先,导入:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication #Used for attachments
import smtplib
和代码:
sender = "EMAIL"
recipients = ["EMAIL1","EMAIL2"]
msg = MIMEMultipart('mixed') #use mixed instead of alternative to load multiple things
msg['Subject'] = "Subject Text"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
html = PandasDataFrame1.to_html() #first dataframe
#insert text as follows
html += '''
<br><br>
This is a new line of random text.
<br><br>
'''
html += PandasDataFrame2.to_html() #second dataframe
#put the html into the email body
html = MIMEText(html, 'html')
msg.attach(html)
如果您还想将文件附加到电子邮件中,请使用此代码
ATTACHMENT_PATH = 'path\file.type'
with open(ATTACHMENT_PATH, 'r') as fileobj:
attachment = MIMEApplication(fileobj.read(), Name='file.type')
attachment['Content-Disposition'] = 'attachment; filename="file.type"'
msg.attach(attachment)
以及使用服务器发送的代码
SERVER = "SERVER"
server = smtplib.SMTP(SERVER)
server.sendmail(sender, recipients, msg.as_string())
server.quit()
我正在使用 MIMEMultipart 从 Python 发送电子邮件。代码如下:
sender = "EMAIL"
recipients = ["EMAIL"]
msg = MIMEMultipart('alternative')
msg['Subject'] = "Subject Text"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
html = PandasDataFrame.to_html()
part2 = MIMEText(html, 'html')
msg.attach(part2)
SERVER = "SERVER"
server = smtplib.SMTP(SERVER)
server.sendmail(sender, recipients, msg.as_string())
server.quit()
这会插入一个 Python Pandas 数据框作为 HTML 并且工作正常。是否也可以将脚注作为文本添加到电子邮件正文中?代码将如何同时完成这两项工作?或者,我可以将评论添加为 HTML,但更多或更少需要在电子邮件正文中添加一些脚注。
谢谢
所以下面的没用,看看
也许是这个?
html = pd.DataFrame([[1,2,3], ['dog', 'cat', 42]]).to_html()
part1 = MIMEText(html, 'html')
msg.attach(part1)
part2 = MIMEText('html')
coolstring = 'This is a dope-ass DataFrame yo'
part2.set_payload(coolstring)
msg.attach(part2)
尽管我认为它与下面的#2 太相似了。输出如下:
>> print msg
# ...header with my email whoops...
--===============0888735609==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
<table border="1" class="dataframe">
#...DataFrame html...
</table>
--===============0888735609==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
This is a dope-ass DataFrame yo
--===============0888735609==--
通过 the examples 找到了几种方法,并通过等效于 dir(MIMEMultipart)
MIMEMultipart
上的方法
您必须验证的三个猜测:
1) 您可以通过
设置结语msg.epilogue = 'This is a dope-ass DataFrame yo'
但是不确定这会出现在电子邮件正文中的何处或是否结束。
2) 创建另一个 MIMEText 并附加它。这似乎是他们在示例中一次发送大量图片的方式,因此这可能是您最好的选择。可能应该以此为首。
part_text = MIMEText('This is some text, yessir')
msg.attach(part_text)
看起来有点像,因为边界划分是一样的。
>> print msg.as_string()
Content-Type: multipart/alternative; boundary="===============1672307235=="
MIME-Version: 1.0
Subject: Subject Text
From: EMAIL
To: EMAIL
--===============1672307235==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
<table border="1" class="dataframe">
# ...DataFrame in HTML here...
</table>
--===============1672307235==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
This is some text, yessir
--===============1672307235==--
3) 由于要在 server.sendmail(sender, recipients, msg.as_string())
中实际发送它,您将 msg
转换为字符串,您的另一个选择是手动将一些 HTML 文本添加到 msg.as_string()
直接地。像
msg.as_string().replace('</table>', '</table>\n<p>...your text here</p>')
会很乱,但应该可以。
如果这些有帮助,请告诉我!我有点在黑暗中拍摄,因为我现在无法测试。祝你好运!
此代码有效:
首先,导入:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication #Used for attachments
import smtplib
和代码:
sender = "EMAIL"
recipients = ["EMAIL1","EMAIL2"]
msg = MIMEMultipart('mixed') #use mixed instead of alternative to load multiple things
msg['Subject'] = "Subject Text"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
html = PandasDataFrame1.to_html() #first dataframe
#insert text as follows
html += '''
<br><br>
This is a new line of random text.
<br><br>
'''
html += PandasDataFrame2.to_html() #second dataframe
#put the html into the email body
html = MIMEText(html, 'html')
msg.attach(html)
如果您还想将文件附加到电子邮件中,请使用此代码
ATTACHMENT_PATH = 'path\file.type'
with open(ATTACHMENT_PATH, 'r') as fileobj:
attachment = MIMEApplication(fileobj.read(), Name='file.type')
attachment['Content-Disposition'] = 'attachment; filename="file.type"'
msg.attach(attachment)
以及使用服务器发送的代码
SERVER = "SERVER"
server = smtplib.SMTP(SERVER)
server.sendmail(sender, recipients, msg.as_string())
server.quit()