Adding an HTML prefix to EMail MIME causes some cl

2019-03-01 10:29发布

I'm trying to add an HTML part to an Email - which can be any type of MIME (probably mixed or alternative, we get it raw from GMail).
Here's how we currently do it:

email_copy = deepcopy(original_email)
if 'Content-Type' in original_email:
    original_email.replace_header('Content-Type', 'multipart/mixed')
else:
    original_email.add_header('Content-Type', 'multipart/mixed')
warning_part = [MIMEText(warning_html, 'html', 'UTF-8')]
original_email.set_payload(warning_part)
original_email.attach(email_copy)

The above snippet works well for GMail web client, GMail Android app, Outlook on Mac, etc. However, on some specific EMail clients we the email is displayed differently:

  • On Outlook App for Android, the body is missing and no attachments;
  • And on Outlook 2016 app on Windows 8.1, the body is missing but it appears as 2 attachments (text and html).

Now I came across this thread:
HTML-Email with inline attachments and non-inline attachments (@Renat Gilmanov)
And this one:
Mail multipart/alternative vs multipart/mixed (@Iain)

The first one relates to controlling whether images are displayed inline or as attachments. I tried to apply it to my case but I still get the same behavior. The second suggests a specific structure which I also couldn't make it work on my case.

What is the correct structure I should use in order to add an HTML prefix to the original message - so it will display uniformly on all the main EMail clients? P.S. I'm currently trying to avoid editing the original html part, but it's my last resort.

Any help would be much appreciated!

0条回答
登录 后发表回答