簡體   English   中英

在 python 中使用 win32 發送 Outlook 電子郵件

[英]Sending Outlook emails with win32 in python

I am relatively new to the coding world and trying to use the following python code to send automated email reports.. But i only get the dataframe in the automatic email and not the "Hi this is a test email" in the outlook email body.

不太確定這里出了什么問題。

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()

如果有人能告訴我這里發生的事情,我將不勝感激。謝謝。

當您將 email 客戶端分配給HTMLBody時,您將覆蓋您的Body ,默認情況下必須使用HTMLBody

因此,您需要將 dataframe 組合到Body字符串中,或者將您的Body作為 html 添加到另一個,這是一個示例:

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

您還需要將其格式化為實際的 html,但請參閱此問題:Sending HTML email using ZA7F5F32316B923821731

tl:博士

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM