使用Python自动化将Outlook邮件发送给具有不同内容的多个用户可以通过以下步骤实现:
import win32com.client as win32
outlook = win32.Dispatch('Outlook.Application')
namespace = outlook.GetNamespace("MAPI")
mail = outlook.CreateItem(0)
mail.Subject = "邮件主题"
mail.Body = "邮件正文"
mail.HTMLBody = "<h3>邮件正文</h3>"
mail.To = "收件人邮箱地址"
mail.CC = "抄送人邮箱地址"
attachment = "附件路径"
mail.Attachments.Add(attachment)
mail.Send()
如果要发送给多个用户,可以使用循环来遍历用户列表,并在每次循环中设置不同的收件人和邮件内容。
以下是一个完整的示例代码:
import win32com.client as win32
def send_email(subject, body, recipients, cc=None, attachment=None):
outlook = win32.Dispatch('Outlook.Application')
namespace = outlook.GetNamespace("MAPI")
mail = outlook.CreateItem(0)
mail.Subject = subject
mail.Body = body
mail.HTMLBody = "<h3>{}</h3>".format(body)
mail.To = recipients
if cc:
mail.CC = cc
if attachment:
mail.Attachments.Add(attachment)
mail.Send()
# 示例用法
subject = "邮件主题"
body = "邮件正文"
recipients = ["收件人1@example.com", "收件人2@example.com"]
cc = "抄送人@example.com"
attachment = "附件路径"
for recipient in recipients:
send_email(subject, body, recipient, cc, attachment)
这样,使用Python自动化发送Outlook邮件给具有不同内容的多个用户就可以实现了。
注意:以上代码使用了win32com.client
库来与Outlook进行交互,因此需要确保已安装了相应的库,并且Outlook已正确配置。
领取专属 10元无门槛券
手把手带您无忧上云