【发布时间】:2020-08-28 14:49:30
【问题描述】:
我有一本 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_group包含什么?同样正如错误所暗示的那样,file是什么? -
道歉 - 我更正了这个问题。 email_group 是电子邮件地址和相应附件的字典。 File 是在桌面文件夹中找到的文件。
-
哪一行导致错误?我假设
email_sender.send_emails(email_group, attachment_path + file)。你想在这里做什么?此行执行时没有名为file的变量。 -
我试图使用带有电子邮件地址和文件名的 dict 'email_group' 将正确的 XLSX 文件附加到正确的电子邮件地址并发送电子邮件。我已经在下面回复了你的回答。
标签: python outlook attachment win32com