Last active
May 28, 2024 16:59
-
-
Save ITSecMedia/b45d21224c4ea16bf4a72e2a03f741af to your computer and use it in GitHub Desktop.
Python: Create an Email with Outlook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 protected]" | |
attachment1 = r"C:\Temp\example.pdf" | |
newMail.Attachments.Add(Source=attachment1) | |
newMail.display() | |
newMail.Send() |
Hello, just tried Email.Sender & Email.SenderEmailAddress & Email.SenderName, also tried Email.SendUsingAccount , but none of them works.
Any idea? Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ITSecMedia
Hello, i want to ask how i can set a random sender and how i can set random Timestamp?
With the Faker module like this:
newMail.Sender= fake_data.email()
newMail.Timestamp=fake_data.iso8601()
It doesnt work yet. Has someone an idea?