Python Forum
email Library: properly send message as quoted-printable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
email Library: properly send message as quoted-printable
#1
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.
Reply
#2
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)
Reply
#3
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.
Reply
#4
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send email with smtp without using Mimetext mgallotti 0 717 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  how to check if someone send a message in a discord channel? Zerolysimin 1 785 Nov-06-2022, 11:10 AM
Last Post: Larz60+
  Unable to send email attachments cosmarchy 7 2,591 Mar-09-2022, 09:29 PM
Last Post: bowlofred
  How to make scraper send email notification just once themech25 0 1,392 Nov-08-2021, 01:51 PM
Last Post: themech25
  is this Unicode printable? Skaperen 2 1,451 Sep-23-2021, 01:25 AM
Last Post: Skaperen
  send email smtp rwahdan 0 1,802 Jun-19-2021, 01:28 AM
Last Post: rwahdan
  Bluetooth send message after connecting? korenron 2 2,679 Apr-26-2021, 05:50 AM
Last Post: korenron
  Need Outlook send email code using python srikanthpython 3 8,278 Feb-28-2021, 01:53 PM
Last Post: asyswow64
  How to send email using python? Gigux 2 2,868 Jul-04-2020, 07:53 AM
Last Post: Gigux
  EOF while scanning triple-quoted string literal louis216 1 3,954 Jun-30-2020, 04:11 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020