Creating an email message without opening all the email

49 Views Asked by At

I'm trying to automate my Outlook emails with Python on MacOS. I managed to get most of the code working, but there is a slight problem as whenever I run the code, it will pop up all 10-20 emails. How do i achieve the drafting but it doesn't pop out but just be drafted within Outlook Draft box. Any help will be appreciative.

I have tried using msg.draft() or msg.save(), but it doesn't work.

attachment_path = '#attachment path'

#Create Outlook client
outlook = app('Microsoft Outlook')

email_messages = []

#loop through each row of data in df
for index, row in df.iterrows():
    code = row['x']
    fac_name = row['y']
    fc1 = row['name1']
    fc2 = row['name2']
    rt = row['name3']
    
    # Set email details with dynamic subject based on code and fac_name
    subject = f'[{code}/{fac_name}] Important E-mail'
    body = 'Just kidding, it\'s not.'

    # Set recipients (modify this according to your needs)
    to_recip = [fc1, fc2, rt]
    
    #Filter out empty email addressses
    to_recip = [recipient for recipient in to_recip if recipient]

    # Create new email message
    msg = outlook.make(new=k.outgoing_message, with_properties={k.subject: subject, k.content: body})

    # Add recipients
    for recipient in to_recip:
        msg.make(new=k.to_recipient, with_properties={k.email_address: {k.address: recipient}})

    # Attach file
    attachment = Alias(str(Path(attachment_path)))
    msg.make(new=k.attachment, with_properties={k.file: attachment})

    # Open and activate the email message
    msg.activate()
    msg.open()```
0

There are 0 best solutions below