Python Forum

Full Version: email Library: properly send message as quoted-printable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
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.
Have you tried setting the content type and described here?

https://stackoverflow.com/questions/8827...ith-python

Even though the original post is 10 years old, this update is from 2019

Quote:for python3, improve @taltman 's answer:

use email.message.EmailMessage instead of email.message.Message to construct email.
use email.set_content func, assign subtype='html' argument. instead of low level func set_payload and add header manually.
use SMTP.send_message func instead of SMTP.sendmail func to send email.
use with block to auto close connection.
from email.message import EmailMessage
from smtplib import SMTP

# construct email
email = EmailMessage()
email['Subject'] = 'foo'
email['From'] = 'sender@test.com'
email['To'] = 'recipient@test.com'
email.set_content('<font color="red">red color text</font>', subtype='html')

# Send the message via local SMTP server.
with smtplib.SMTP('localhost') as s:
s.login('foo_user', 'bar_password')
s.send_message(email)
I will check things out in a bit and see how it goes. I didn't know of the "subtype=html" argument. The documentation is not that great.
All works as expected following your SO link, @deanhystad. Thanks a million! I spent two hours trying to figure that out last night.

As a side question (I'm here, you know), I'm sending mail with quoted-printable CTE. That's what the pros use, so, that's something. I noticed certain characters I write get turned into code, such as a dash (-). What's the forums thoughts on fixing that? I know QP encodes a message to 7-bit, and that is the reason for this added code. How can I prevent that?