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("myemail@provider.com")
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
.