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