Require receipts when sending Outlook email by Python - python

Simple lines to send Outlook email by Python,
referencing from Send email through Python using Outlook 2016 without opening it
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'contact#sample.com'
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.Send()
Is it possible to require Delivery Receipt and Read Receipt when sending an email? What would be the good way?

Sure, use ReadReceiptRequested & OriginatorDeliveryReportRequested property MSDN
Example
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = "0m3r#Email.com"
mail.Subject = 'Message subject'
mail.Body = 'Message body'
# request read receipt
mail.ReadReceiptRequested = True
# request delivery receipt
mail.OriginatorDeliveryReportRequested = True
mail.Send()

Related

Python `mail.Send` successful but outlook email not delivered

I already referred this post. Don't mark it as duplicate please.
I wrote the below code to send an email via python
outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
From = outlook.Session.Accounts[1]
mail.To = 'test#org.com'
mail.Subject = 'Test Email'
mail.HTMLBody = '<h3>This is HTML Body</h3>'
mail.Body = "This is the normal body"
mail._oleobj_.Invoke(*(64209, 0, 8, 0, From))
mail.Send() # successfully executed
The above code is successfully executed but still the email is not delivered and its been more than 15 mins. If I do it manually using outlook,am able to send and receive messages.
Can help me what is the issue here?
update - mail.display() looks like below
update - mail.Send() in outbox looks like below
update - error for 2nd mailbox
update - 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._oleobj_.Invoke(*(64209, 0, 8, 0, mail.SendUsingAccount))
pythoncom.CoInitialize()
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 Outlook email using python win32com and flag as Follow-up

How can I flag a sent email to followup with a reminder date if possible using win32com python library, my code below does send the email, but does not create the followup reminder.
Code:
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
#attachment1 = "x:\\report.htm"
attachment1 = "c:\\installAgent.log"
mail.Attachments.Add(Source=attachment1)
mail.To = "obama#hotmail.com"
mail.Subject = "test"
mail.HtmlBody = '<h2>HTML Message body</h2>' #this field is optional
mail.FlagRequest = "Follow up";
mail.Display(True)
mail.send

Open email window to allow physical input

I want the user to be able to manually complete an email in an actual instance of Outlook as opposed to hard-coding the values or input in the shell.
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = '***'
mail.Subject = '***'
mail.Body = '***'
mail.Send()
Do not call mail.Send() - call mail.Display(true) to display the message modally.
The input() built-in. Example:
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = input("Mail to: ")
mail.Subject = input("Mail Subject: ")
mail.Body = input("Mail Body: ")
attachment = str(output_file)
attachpath = "my_attachment_path"
attachfull = attachpath+attachment
attachf = str(attachfull)
mail.Attachments.Add(Source=attachf)
mail.Send()

how to change from address in sending mail in outlook using python win32

I have the following code and i need to change the from address.
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'madanraj.c#xyz.com;madanraj.c#xxxyyy.com'
mail.Subject = 'Daily Backlog'
mail.Body = 'This Mail is Sent by Python'
mail.HTMLBody = '<h2>HTML Message body</h2>' #this field is optional
# To attach a file to the email (optional):
#attachment = "C:\\Users\madanraj.c\Downloads\Report.csv"
#mail.Attachments.Add(attachment)
mail.Send()
print("success")
I have two mail address in my outlook application and i need to send from other one , not by default one.

How do I generate and open an Outlook email with Python (but do not send)

I have a script that automatically creates and sends emails sends emails using the simple function below:
def Emailer(text, subject, recipient):
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = text
mail.send
But how do I open this email in an Outlook window so that it can be manually edited and sent?
Ideally, I'd like something like this:
def __Emailer(text, subject, recipient, auto=True):
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = text
if auto:
mail.send
else:
mail.open # or whatever the correct code is
Call mail.Display(True) instead of mail.send.
tldr: Use mail.Display(False) instead of mail.Display(True)
mail.Display(False) will still display the window.
If you use mail.Display(True) the scripts stops until the window is closed. So use mail.Display(False) this will open the window and your python script will move on to the next command. It is also useful to know that you can use mail.save() to save as draft in the draft folder.
Visit https://msdn.microsoft.com/en-us/VBA/Outlook-VBA/articles/mailitem-display-method-outlook to read more on this
I like the solution :) But I want to add some infos:
Using the solution, it is probably the best way to add a mail input with Html format for modification.
Also add the file from the working directory...
#requirements.txt add for py 3 -> pypiwin32
def Emailer(text, subject, recipient):
import win32com.client as win32
import os
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = text
###
attachment1 = os.getcwd() +"\\file.ini"
mail.Attachments.Add(attachment1)
###
mail.Display(True)
MailSubject= "Auto test mail"
MailInput="""
#html code here
"""
MailAdress="person1#gmail.com;person2#corp1.com"
Emailer(MailInput, MailSubject, MailAdress ) #that open a new outlook mail even outlook closed.
Here is another option with saving the mail on disk first:
import webbrowser
mail.SaveAs(Path=save_path)
webbrowser.open(save_path)
This way the mail opens maximized.

Categories

Resources