简体   繁体   中英

Python : How to send mails using outlook and attach files

I have a python array with 3 email addresses:

email_group = df.EMAIL.unique()

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

How do I loop through the array of emails, assign the first email address to the "mail.To" field and send? Then loop to the 2nd email address and send, and finally sending a 3rd email with the final address in the array.

End result: I need one email sent to each address in the array using a loop.

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()

Option 1 : Using a class

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)

Option 2 :Using a function

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)

Option 3 : Just It

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()

If I understand your question, you can simply use a for loop over your list of addresses

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()

I think is something like this:

emails = [
    'test@test.com',
    'test1@test.com',
    'test2@test.com'
]

for email in emails:
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = email
    mail.Subject = 'Report'
    mail.Body = """Report is attached."""
    mail.Send()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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