[Python] python send_message vs sendmail with attached MIMEText

Discussão em 'Python' iniciado por Stack, Julho 3, 2025.

  1. Stack

    Stack Membro Participativo

    I found lots of sample code showing how to use

    sendmail(sender_email, receiver_emails, msg.as_string())

    for a MIMEMultipart message containing an html body and attachments.

    Then I read that an EmailMessage object would be even more convenient, as the send_message method is supposed to extract the from, to, cc, bcc directly from the EmailMessage headers.

    This works fine for plain text messages, but if I do

    msg.attach(MIMEText(body, 'html'))

    instead of

    msg.set_content(body) # displaying the html body as plain text

    the final

    server.send_message(msg)

    fails with AttributeError: 'list' object has no attribute 'encode'

    Do I have to add a header to the MIMEText part to supply 'utf-8' encoding to the MIMEtext part ?

    Continue reading...

Compartilhe esta Página