I'm trying to build a python mailserver that rewrites certain URLs (given in a text file). Plaintext messages work without problems, but multipart (HTML) E-Mails are a bit of concern to me. I can't find a proper way to rebuild the message after modifying the body and replace functions do not exist for email in python.
What I have currently (for plaintext messages, currently without replacement):
if(not msg.is_multipart()):
for part in msg.walk():
parttype = part.get_content_type()
partmaintype = parttype.split("/")[0]
partsubtype = parttype.split("/")[1]
parttype=partmaintype + "/" + partsubtype
bodypart = part.get_body(('plain','html'))
#bodypart = bodypart.get_content().encode()
bodypart = bodypart.get_content()
print(bodypart)
newmsg.set_content(bodypart)
#print(newmsg)
newdata = bytes(newmsg)
The _structure(msg) function is in knowledge to me, but I cannot find a way to preserve this message structure (to rebuild the email with a new message object). I also know about the msg.walk() function, but when used with multipart message contents, the subcontents occur twice.
Thanks a lot in advance!