python - 在 python 中使用 win32 发送 Outlook 电子邮件

标签 python python-3.x outlook

我对编码世界比较陌生,并尝试使用以下 python 代码发送自动电子邮件报告。 但我只得到自动电子邮件中的数据框,而不是 Outlook 电子邮件正文中的“嗨,这是一封测试电子邮件”

不太确定这里出了什么问题。

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=list('ABCD'))

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'user@email.com'
mail.Subject = 'Insert Subject here'
mailerr1=df.to_html()
attachment  = 'shot.png'
mail.Attachments.Add(attachment)
mail.Body = "hi this is  a test email"
mail.HTMLBody = mailerr1

mail.Send()

如果有人能让我了解这里发生的事情,我将非常感激。 谢谢。

最佳答案

当您分配给 HTMLBody 时,您将覆盖您的 Body,您的电子邮件客户端默认情况下必须使用 HTMLBody

因此,您需要将数据帧合并到 Body 字符串中,或​​者将您的 Body 作为 html 添加到另一个,这是一个示例:

email.HTMLBody = '<p>hi this is a test email</p><br>' + mailerr1

您还需要将其格式化为实际的 html,请参阅这个问题:Sending HTML email using Python

tl:博士

email.HTMLBody = """
<html>
  <head></head>
  <body>
    <p>hi this is a test email</p><br>
""" + mailerr1 + """
  </body>
</html>
"""

关于python - 在 python 中使用 win32 发送 Outlook 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58230436/

相关文章:

python - 获取两个日期 Pandas 之间的周数

python - 如何从 pandas groupby().sum() 的输出创建一个新列?

html - 删除 outlook 中 html 电子邮件周围的空间

python - Python 中的 options(scipen=999) 相当于什么

python - Perl中相当于$|=1的python是什么

html - Gmail Android 和 iOS 应用程序。从 MS Outlook 发送时,CSS 样式(td 和 p 元素)不起作用

php - 从 Outlook 中删除格式

python - 我的 Mac OS X Mountain Lion 上有太多 python

python - flask-admin is_accessible 用法

python - 从 BSE 网站提取数据