简体   繁体   English

Python发送带有相应附件的电子邮件

[英]Python send emails with corresponding attachments

I have a desktop folder with 3 XLSX files: ID1.xlsx , ID2.xlsx , ID3.xlsx我有一个包含 3 个 XLSX 文件的桌面文件夹: ID1.xlsxID2.xlsxID3.xlsx

I can use the following code to loop through a list of 3 email addresses and send individual emails to each address, but it attaches all XLSX files in the folder.我可以使用以下代码循环遍历 3 个电子邮件地址的列表并向每个地址发送单独的电子邮件,但它会附加文件夹中的所有 XLSX 文件。

Expected Result: I want to attach ID1.xlsx to the first email and send, ID2.xlsx to the 2nd email and send, and finally ID3.xlsx to the 3rd email and send.预期结果:我想将ID1.xlsx附加到第一封电子邮件并发送, ID2.xlsx到第二封电子邮件并发送,最后将ID3.xlsx到第三封电子邮件并发送。

email_addresses = ['test1@test.com', 'test2@test.com', 'test3@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 = r'C:\Users\Desktop\Test\*.xlsx'
email_sender = EmailsSender()
email_sender.send_emails(email_addresses, attachment_path)

You will need to reference the file name with email so switch to Dictionaries .您需要使用电子邮件引用文件名,因此请切换到Dictionaries

Example例子

email_addresses = {'test1@test.com': 'ID1.xlsx',
                   'test2@test.com': 'ID2.xlsx',
                   'test3@test.com': 'ID3.xlsx'
                   }

attachment_path = r'C:\Users\Desktop\Test'


for email, file in email_addresses.items():
    print(email, attachment_path + file)

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

相关问题 使用 Python Dict 发送带有特定附件的电子邮件 - Using Python Dict to Send Emails with Specific Attachments 如何使用 python 发送带有多个附件的电子邮件 - How to use python to send emails with multiple attachments 在 Python (pywin32) 中发送带有附件的 Outlook 电子邮件 - Send Outlook Emails with attachments in Python (pywin32) 如何使用 Python 循环发送多封带有附件的电子邮件 - How to send multiple emails in a loop with attachments using Python 带有 AWS SES 的 smtplib 无法发送带附件的电子邮件 - smtplib with AWS SES could not send emails with attachments 使用 python 3 和 Gmail API 发送带有附件的电子邮件,我最终遇到文件损坏或 ConnectionAbortedError - Using python 3 and Gmail API to send emails with attachments, I end up with either corrupted files or ConnectionAbortedError 如何从使用 Python 作为附件发送的电子邮件中下载附件? - How can I download attachments from emails sent as attachments with Python? 使用Python从附件列表发送电子邮件 - Sending emails with Python from a list of attachments 使用python在Outlook电子邮件中保存附件 - Saving attachments in Outlook emails using python 看不到从 Python 发送的电子邮件中的附件 - Attachments in emails sent from Python are not being seen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM