Nov-14-2022, 11:13 AM
I am struggling to get email clients to properly render HTML messages. I believe it is a result of the content transfer encoding. I've tried several different HTML code styles, using the net extensively. Nothing permits an email client to render HTML properly (I get the code in the message). Here's my code:
To sum up, all HTML messages are displaying as raw code. My HTML is solid, I'm at the point now where I just copy and pasted a working email. My problem is something in the transfer, and I think it's the content transfer encoding and encoding the email properly. But, whatever the problem is, I want to fix it.
def create_message(sndr, rcvr, subj, body, extra=None): """ @type sndr: str @type rcvr: str @type subj: str @type body: tuple @type extra: tuple @return: bytes """ msg = message.EmailMessage() if '<' in sndr: name, sep, addr = sndr.rpartition(' ') # Partition at space between display name & address name = name.strip() # Remove leading and trailing spaces addr = addr.strip('<>') # Remove leading and trailing angle brackets msg['From'] = headerregistry.Address(display_name=name, addr_spec=addr) else: msg['From'] = headerregistry.Address(display_name='', addr_spec=sndr) if '<' in rcvr: name, sep, addr = rcvr.rpartition(' ') name = name.strip() addr = addr.strip('<>') msg['To'] = headerregistry.Address(display_name=name, addr_spec=addr) else: msg['To'] = headerregistry.Address(display_name='', addr_spec=rcvr) msg['Subject'] = subj msg.set_content(body[0], charset='utf-8') msg.add_alternative(body[1], cte='quoted-printable') return msg.as_bytes()The above is in the middle of things, but it's not working at the time of this post. NOTE: I don't want to use the legacy email API. All you can do with the legacy, you can do with the current. I've seen several SO posts, but they all use the legacy API as they are old posts (ca 10 years).
To sum up, all HTML messages are displaying as raw code. My HTML is solid, I'm at the point now where I just copy and pasted a working email. My problem is something in the transfer, and I think it's the content transfer encoding and encoding the email properly. But, whatever the problem is, I want to fix it.