pythonemailsendmailwin32comsender

Want to send an Outlook email from Python with win32com.client from a other email address that default email address


I would like to be able to send a mail automatically with Python and win32com.client. Here is my code that works:

import win32com.client
o = win32com.client.Dispatch("Outlook.Application")
    
mail = o.CreateItem(0)
mail.To = "recipient@domain.com"
    
mail.CC = "..@..."
    
mail.Subject = "subject"
mail.Body = "main body
  
mail.Send()

However, I would like to be able to send this mail from another of my Outlook mail addresses. So I would like to put in argument or option the sender of the mail.

I searched a lot on the internet but I didn't find anything.


Solution

  • What about this (SentOnBehalfOfName)? There seems to be a difference of usage depending on the kind of the account. See here: Choosing "From" field using python win32com outlook

    outlook = win32com.client.Dispatch("Outlook.Application")
    mail = outlook.CreateItem(0)
    mail.SentOnBehalfOfName = 'xyz@gmail.com'
    mail.To = 'mymail@gmail.com'
    mail.Subject = 'Hello'
    mail.HTMLBody = template