0

I am trying to reply to an email via python and win32com. Below is my code :

    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    o = win32com.client.Dispatch("Outlook.Application")
    inbox = outlook.GetDefaultFolder(6)
    messages = inbox.Items


    for message in messages:
        if message.Subject == "aaa":
            print("Found message")             
            message.body="This is a reply"        
            message.Reply()

The reply is getting sent to the sender of "aaa" but the previous email on which I am trying to reply does not appear below the new email.The history of emails on which I am replying gets lost.

1 Answer 1

2

I do not have the means to test this, but from the code you shared, I think the problem is as follows:

you iterate over messages and each found MailItem is assigned the loop variable "message". Next you set the body of message as "This is a reply" - in other words: You overwrite the original message with the new string and then send the reply. .Reply() then simply creates a new MailItem object from message, just with Sender and Recipient Properties switched... and the new body you yourself assigned it.

https://learn.microsoft.com/en-gb/office/vba/api/outlook.mailitem.reply(method)

EDIT:

So I made this code:

import win32com.client as win32

outlook = win32.Dispatch("Outlook.Application").GetNamespace("MAPI")
acc = outlook.Folders("[email protected]")
inbox = acc.folders("Inbox")   #change to localized versions
drafts = acc.folders("Drafts") #if necessary

def createReply(email:object):
        reply = email.Reply()
        newBody = "Dear friend,\n\nThis should be added on top.\nI hope this 
                   works\n\nkr\ntst\n"
        reply.HTMLBody = newBody + reply.HTMLBody
        reply.Move(drafts)

for mailItem in inbox.Items:
        if mailItem.Subject == "Test4Reply":
                print("Start")
                createReply(mailItem)

First, I sent an email to myself with the Subject line "Test4Reply" so I can grab that. I added some gibberish into the Email body, just to check if it gets retained. Then, I created a new MailItem Object reply from the email in my inbox using the .Reply() Method, which I then moved (with .Move()) into my Drafts folder. There I can inspect it and see that, indeed, the original email is preserved in the history, as well as the Subject line automatically gaining the "AW: " prefix.

So: To preserve the original email, you just need to make sure to not overwrite the original Body and only insert new text at the beginning of the MailItem.HTMLBody.

Sign up to request clarification or add additional context in comments.

7 Comments

You are right. I tried creating new email object but do not see any means for reply to an email keeping previous conversations intact. Reply()/ReplyAll() do not take new email object I created as argument.Not sure if win32com can suffice my requirement.
I will be at my workstation in a bit, I can try it out and do some tests then. Reply() should not send the email yet though, right? According to the docs, it only creates a new MailItem object which you then have to send by calling .Send
Already appreciating your help. I tried for message in messages: if message.Subject == "aaa": newMessage=message.Reply() newMessage.body="Body in reply" newMessage.Send() but it did the same as in original question :(
Hey! Thanks it worked. It at least retained the previous mail. A little better formatting after I concatenated my MailItem.HTMLBody.Will still have to do the manipulation in the mail body to make it look like its actually an email chain.Appreciate your help..Cheers!
Oh... yeah I just figured it out. The HTMLBody is literally all you need. I added to my code in the above answer! Obviously the linebreaks in my newBodystring dont show, because it now should be HTML formatted, not plain text.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.