Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python: Create an Email with Outlook
# https://itsec.media/post/python-send-outlook-email/
import win32com.client
from win32com.client import Dispatch, constants
const=win32com.client.constants
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "I AM SUBJECT!!"
# newMail.Body = "I AM\nTHE BODY MESSAGE!"
newMail.BodyFormat = 2 # olFormatHTML https://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx
newMail.HTMLBody = "<HTML><BODY>Enter the <span style='color:red'>message</span> text here.</BODY></HTML>"
newMail.To = "email@demo.com"
attachment1 = r"C:\Temp\example.pdf"
newMail.Attachments.Add(Source=attachment1)
newMail.display()
newMail.Send()
@ltpitt

This comment has been minimized.

Copy link

@ltpitt ltpitt commented Nov 1, 2017

Jewel! 🍡

@kuznetsov-e

This comment has been minimized.

Copy link

@kuznetsov-e kuznetsov-e commented Mar 27, 2018

Hello. I have some little error with this code. But "newMail.display(True)" solves it
: )

@iasantosr

This comment has been minimized.

Copy link

@iasantosr iasantosr commented Jun 30, 2018

thanks was very helpful

@UnderJollyRoger

This comment has been minimized.

Copy link

@UnderJollyRoger UnderJollyRoger commented Sep 7, 2018

I copied your script verbatim then I get an error. But the email still sends anyway.
line 20, in <module> newMail.send() TypeError: 'bool' object is not callable

@voidsteed

This comment has been minimized.

Copy link

@voidsteed voidsteed commented Nov 28, 2018

I copied your script verbatim then I get an error. But the email still sends anyway.
line 20, in <module> newMail.send() TypeError: 'bool' object is not callable

I figured it out. Use newMail.Send() instead of newMail.send()

@mohanramselvaraj

This comment has been minimized.

Copy link

@mohanramselvaraj mohanramselvaraj commented Apr 9, 2019

Thanks.. it works for me,
How to import existing mail signature into the new mail

@ragaveenaitsolutions

This comment has been minimized.

Copy link

@ragaveenaitsolutions ragaveenaitsolutions commented May 20, 2019

I want to create a email and save it in draft.. How to do it ? Any help would be highly appreciated and Thank you in Advance!

@Stehos

This comment has been minimized.

Copy link

@Stehos Stehos commented Mar 4, 2020

Hi Norman, first I want to thank you for publishing this snippet. Please do you know, if there is any change to select a sender account from outlook? I have 5 inboxes in my outlook and the email is sent from the 3rd account.

Thanks.

@ITSecMedia

This comment has been minimized.

Copy link
Owner Author

@ITSecMedia ITSecMedia commented Mar 6, 2020

@Stehos, I would expect that you define the default sender account in your Email client, try to look there for a solution.

@ayjagadish

This comment has been minimized.

Copy link

@ayjagadish ayjagadish commented Jun 2, 2020

Hello,
Please help/guide me to achieve below goal. Any help is truly appreciated and I remember for my life time 😊.

What I want to do?

  1. I have outlook template file (.oft).
  2. I have Some values in excel.
  3. Insert values of excel into outlook template using python.

Thanks in advance

@debbyca

This comment has been minimized.

Copy link

@debbyca debbyca commented Sep 29, 2020

I want to create a email and save it in draft.. How to do it ? Any help would be highly appreciated and Thank you in Advance!

You can use newMail.Save().

@mmelitba

This comment has been minimized.

Copy link

@mmelitba mmelitba commented Aug 4, 2021

Hi,
Thanks for this post! It´s really usefull.
Do you know the propertie or the command to change the "from" mailbox?

Thanks in advance!

@ITSecMedia

This comment has been minimized.

Copy link
Owner Author

@ITSecMedia ITSecMedia commented Aug 4, 2021

Hi,
Thanks for this post! It´s really usefull.
Do you know the propertie or the command to change the "from" mailbox?

Thanks in advance!

@mmelitba

Try it with the Sender properties, check the link below

newMail.Sender = "...."
newMail.SenderEmailAddress= "...."
newMail.SenderName = "...."

https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment