使用 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 到第三个电子邮件地址并发送。

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

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 没有在这里声明。