1

I want to use the following python code to automize some reporting

from win32com import client
obj = client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(0x0)
newMail.Subject = "This is the subject"
...
newMail.Body = "This is the text I want to send in the mail body"

But doing it this way deletes the signature. The following code

...
newMail.Body = "This is the text I want to send in the mail body" + newMail.Body

preserves the signature, but destroys the formating. Not acceptable for compliance reasons.

Is there a way to prepend text to the mail body to circumvent the termination of the signatures format?

Lord_Gestalter
  • 500
  • 1
  • 5
  • 14
  • Does the text you want to add have any formatting? What type of formatting does the signature have? Is the email written in html? – jmunsch Apr 02 '14 at 08:02
  • @jmunsch: Nothing special, different font, different size ... It's an HTML. I thought about using HTMLBody, but hoped there was a way to circumvent parsing HTML. It's not really small. And you know what they say about regular expressions. Try to solve a problem with RE. Have two problems afterwards ;-) – Lord_Gestalter Apr 02 '14 at 08:07
  • This might be relevant: http://stackoverflow.com/questions/882712/sending-html-email-using-python – jmunsch Apr 02 '14 at 08:12

1 Answers1

1
tmp = newMail.Body.split('<body>')
# split by a known HTML tag with only one occurrence then rejoin
newMail.Body = '<body>'.join([tmp[0],yourString + tmp[1]])
jmunsch
  • 22,771
  • 11
  • 93
  • 114
  • I tried to make an edit once in a while to this answer, but no change so far (And looking back the edit wasn't very well coded). The above code is wrong, at least in python 2.7. join() needs an iterable, so please replace ...join(tmp[0],yourString + tmp[1]) with ...join((tmp[0],yourString + tmp[1])). Done so I will accept this as answer – Lord_Gestalter Jun 13 '14 at 10:29
  • @Lord_Gestalter You are right, updated the answer. Thanks for catching that. – jmunsch Jun 17 '14 at 13:23