繁体   English   中英

使用 Python Dict 发送带有特定附件的电子邮件

[英]Using Python Dict to Send Emails with Specific Attachments

我有一个 python 字典:

df.set_index('EMAIL').to_dict()['ID'] 
   
email_group = {'test1@test.com': 'ID1.xlsx',
               'test2@test.com': 'ID2.xlsx',
               'test3@test.com': 'ID3.xlsx'
                   }

预期结果:我想将ID1.xlsx附加到第一个电子邮件地址并发送,将ID2.xlsx到第二个电子邮件地址并发送,最后将ID3.xlsx到第三个电子邮件地址并发送。

以下代码产生错误“名称'文件'未定义”。 我缺少什么才能获得上述预期结果?

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)
            mail.Send()
    
        def send_emails(self, email_group, attachment_path=None):
            for email, file in email_group.items():
                self.send_email(email, attachment_path + file)
    
    attachment_path = r'C:\Users\Desktop\Test'
    email_sender = EmailsSender()
    email_sender.send_emails(email_group, attachment_path + file)

我认为你需要改变

email_sender.send_emails(email_group, attachment_path + file)

email_sender.send_emails(email_group, attachment_path)

解释会抱怨,因为这里没有声明file

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM