【问题标题】:Python : How to send mails using outlook and attach filesPython:如何使用 Outlook 发送邮件并附加文件
【发布时间】:2020-08-26 18:48:30
【问题描述】:

我有一个包含 3 个电子邮件地址的 python 数组:

email_group = df.EMAIL.unique()

test@test.com
test1@test.com
test2@test.com

如何遍历电子邮件数组,将第一个电子邮件地址分配给“mail.To”字段并发送?然后循环到第二个电子邮件地址并发送,最后发送第三个电子邮件,其中包含数组中的最终地址。

最终结果:我需要使用循环向数组中的每个地址发送一封电子邮件。

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'POPULATED FROM ARRAY LOOP'
mail.Subject = 'Report'
mail.Body = """Report is attached."""
mail.Send()

【问题讨论】:

    标签: python arrays loops email outlook


    【解决方案1】:

    选项 1:使用类

    email_addresses = ['test@test.com', 'test1@test.com', 'test2@test.com']
    
    
    class EmailsSender:
        def __init__(self):
            self.outlook = win32.Dispatch('outlook.application')
    
        def send_email(self, to_email_address, attachment_path):
            mail = self.outlook.CreateItem(0)
            mail.To = to_email_address
            mail.Subject = 'Report'
            mail.Body = """Report is attached."""
            if attachment_path:
                mail.Attachments.Add(Source=attachment_path, Type=olByValue)
            mail.Send()
    
        def send_emails(self, email_addresses, attachment_path=None):
            for email in email_addresses:
                self.send_email(email, attachment_path)
    
    attachment_path = 'Enter report path here'
    email_sender = EmailsSender()
    email_sender.send_emails(email_addresses, attachment_path)
    
    

    选项 2:使用函数

    outlook = win32.Dispatch('outlook.application')
    
    def send_email(outlook, to_email_address, attachment_path):
        mail = outlook.CreateItem(0)
        mail.To = to_email_address
        mail.Subject = 'Report'
        mail.Body = """Report is attached."""
        if attachment_path:
            mail.Attachments.Add(Source=attachment_path, Type=olByValue)
        mail.Send()
    
    attachment_path = 'Enter report path here'
    for email in email_addresses:
        send_email(outlook, email_addresses)
    

    选项 3:就这样

    email_addresses = ['test@test.com', 'test1@test.com', 'test2@test.com']
    
    outlook = win32.Dispatch('outlook.application')
    
    attachment_path = 'Enter report path here'
    
    for email in email_addresses:
        mail = outlook.CreateItem(0)
        mail.To = email
        mail.Subject = 'Report'
        mail.Body = """Report is attached."""
        mail.Attachments.Add(Source=attachment_path, Type=olByValue)
        mail.Send()
    

    【讨论】:

    • 如果我想在每封电子邮件中附加一个特定的 excel 文件,我可以使用类似的逻辑从文件夹位置提取附件吗?
    • 亲爱的@Mark,感谢您的提问。类和方法的不同之处在于它们更容易激活“启动并忘记”:您所关心的只是传递电子邮件地址(以及 - 如果需要,可以将文件作为参数传递)。它们将发送逻辑放在一起,与您激活它的位置分开,因此代码更易于维护(如果您实际使用该代码,它很重要,并且随着您使用的更多,它会变得更重要)。但是 - 所有选项 - 在功能上是等效的。希望它有所帮助:)
    • @Mark 老实说,我不熟悉 Python 中的 Outlook 发送。但是,为您研究了它,添加并编辑了此功能的每个选项。
    • 代码采用给出其路径的文件。要循环查找目录中的文件,请查看 Find all files in a directory with extension .txt in Python
    • 如何确定文件的顺序?以及如何将文件与电子邮件匹配?请打开一个包含更多详细信息的新问题!
    【解决方案2】:

    如果我理解您的问题,您可以简单地在您的地址列表上使用 for 循环

    emails = ['test@test.com', 'test1@test.com', 'test2@test.com']
    outlook = win32.Dispatch('outlook.application')
    
    for email in emails:
        mail = outlook.CreateItem(0)
        mail.To = email 
        mail.Subject = 'Report'
        mail.Body = """Report is attached."""
        mail.Send()
    

    【讨论】:

    • 这看起来像是一个可行的选项。如果我想在每封电子邮件中附加一个特定的 Excel 文件,我可以使用类似的逻辑从文件夹位置提取附件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2023-04-06
    相关资源
    最近更新 更多