How do I generate and open an Outlook email with Python (but do not send)
Call mail.Display(True)
instead of mail.send
.
Python `mail.Send` successful but outlook email not delivered
The difference between Outlook and your code is the synchronization with the mail server. Outlook may cache submitted items and send them when the store is synced with the mail server.
The NameSpace.SendAndReceive method initiates immediate delivery of all undelivered messages submitted in the current session, and immediate receipt of mail for all accounts in the current profile. SendAndReceive
provides the programmatic equivalent to the Send/Receive All
command that is available when you click Tools
and then Send/Receive
. All accounts defined in the current profile are used in Send/Receive All
. If an online connection is required to perform the Send/Receive All
, the connection is made according to user preferences.
Read more about that in the How To: Perform Send/Receive in Outlook programmatically article.
Also you may try to run the following code:
mail = outlook.CreateItem(0)
for acc in outlook.Session.Accounts:
if acc.DisplayName == 'user2@org.com':
print("hi")
mail.SendUsingAccount = acc.DisplayName
mail.To = 'user1@org.com'
mail.Subject = 'Test Email'
mail.HTMLBody = '<h3>This is HTML Body</h3>'
mail.Body = "This is the normal body"
mail.Send()
Send an outlook email without any permission using python library win32com.client
Microsoft has an article about why and how to fix the issue here
Send Outlook Email Via Python?
For a solution that uses outlook see TheoretiCAL's answer.
Otherwise, use the smtplib that comes with python. Note that this will require your email account allows smtp, which is not necessarily enabled by default.
SERVER = "smtp.example.com"
FROM = "yourEmail@example.com"
TO = ["listOfEmails"] # must be a list
SUBJECT = "Subject"
TEXT = "Your Text"
# Prepare actual message
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
import smtplib
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()
EDIT: this example uses reserved domains like described in RFC2606
SERVER = "smtp.example.com"
FROM = "johnDoe@example.com"
TO = ["JaneDoe@example.com"] # must be a list
SUBJECT = "Hello!"
TEXT = "This is a test of emailing through smtp of example.com."
# Prepare actual message
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
import smtplib
server = smtplib.SMTP(SERVER)
server.login("MrDoe", "PASSWORD")
server.sendmail(FROM, TO, message)
server.quit()
For it to actually work with gmail, Mr. Doe will need to go to the options tab in gmail and set it to allow smtp connections.
Note the addition of the login line to authenticate to the remote server. The original version does not include this, an oversight on my part.
How to trigger a python script on receiving email on outlook with a specific subject?
Which version of Outlook are you using? Office 365 Outlook have built in "Trigger" & "Event" function
I don't use Outlook so I don't know how it would call your script, but you could attach to "When new email arrive" trigger.
Related Topics
How to Change the Title Bar in Tkinter
Pyside - Pyqt:How to Make Set Qtablewidget Column Width as Proportion of the Available Space
Turn the Column Headers into the First Row and Row Headers into the First Column in Pandas Dataframe
How to Find the Average of Particular Numbers in a CSV File
Google Chrome Closes Immediately After Being Launched With Selenium
Python-3: Why This Following Code Returns None in Print Statement
Python Check Multi-Level Dict Key Existence
Python-Compare Two String Columns in Same Dataframe, Return Matching Result
What Is the Most Efficient Way to Sum a Dict With Multiple Keys by One Key
Python Not Working in the Command Line of Git Bash
Iterating Over Every Two Elements in a List
Formal and Actual Parameters in a Function in Python
Pandas - Drop Last Column of Dataframe
Conversion of String to Upper Case Without Inbuilt Methods
Discord.Py Show Who Invited a User
Pandas: Calculate Total Percent Difference Between Two Data Frames