how to add text to an email I'm forwarding using python/outlook without breaking original formatting? -
i'm trying open email add text , forward using python windows extensions.
this have:
import win32com.client outlook = win32com.client.dispatch("outlook.application").getnamespace("mapi") inbox = outlook.getdefaultfolder(6).folders('subfolder') messages = inbox.items message = messages.getlast() newmsg = message.forward() newmsg.to = "email@email.com" #i want forward address newmsg.body = "new text"+newmsg.body
when copy front of email doing this: newmsg.body = "new text"+newmsg.body, breaks hyperlinks in original message , removes original formatting. there anyway keep original formatting(bolded/colored words/hyperlinks) , add new text?
in outlook object model, body
property of [mailitem
] represents "the clear-text body of outlook item".
there's separate rtfbody
property represents "the body of microsoft outlook item in rich text format."
if rewrite either 1 of these, re-creates other. way works doesn't seem documented in oom, mapi equivalent, pidtagbody
/pr_rtf_compressed
explains it:
when pr_body stored first time, message store generates , stores pr_rtf_compressed (pidtagrtfcompressed) property, rtf version of message text. if imapiprop::savechanges method subsequently called , pr_body has been modified, message store calls rtfsync function ensure synchronization rtf version. if white space has been changed, properties left unchanged.
so, if make changes besides whitespace body
, rtfbody
may re-created scratch. , there's nothing can that.
there's third property, htmlbody
, lot easier edit in python rtfbody
, whether using stdlib solutions elementtree
or third-party libraries beautifulsoup
.
it isn't documented anywhere kinds of changes can break exact round-tripping, it's not hard test (and of changes you'd want make won't have visible effect, if change details of rtf, may enough).
or, maybe better, can force converted-and-edited html primary representation of message instead of rtf, setting bodyformat
olformathtml
. way, you'll know gets delivered same html see in code.
if need edit rtfbody
, can. it's array of bytes representing possibly-compressed , definitely-encoded rtf. have call strconv
on convert unicode rtf string, , have process rtf. can in pure python if want (see rtf spec), or using third-party library, there com apis that can find searching msdn.
there 1 last possibility consider: can create , automate message editor window. horribly hacky, guarantee you're doing same thing if user copied , pasted email manually…
Comments
Post a Comment