Search code examples
python-3.xemail

How to set the Content-Transfer-Encoding in python


Some users are having the content transfer encoding type set as base64. How do I set the Content-Transfer-Encoding manually.

  import smtplib
  from email.message import EmailMessage
  from email.encoders import encode_7or8bit

  msg = EmailMessage()
  msg.set_content(message,subtype='html')
  #msg.set_charset('UTF-8')
  msg['Subject'] ="your order is {} at {}".format(order_id,today_is)
  msg['From'] = '[email protected]'
  msg['To'] = '[email protected]'
  encode_7or8bit(msg)

This is the error I am getting.

 ValueError: There may be at most 1 Content-Transfer-Encoding headers in a message

If I move encode_7or8bit(msg) above msg.set_content it works, but I don't think the content is being set. Should I send something into set_content


Solution

  • import smtplib
    from email.message import EmailMessage
    
    msg = EmailMessage()
    msg.set_content(message,subtype='html',charset='utf-8',cte='7bit')
    msg['Subject'] ="your order is {} at {}".format(order_id,today_is)
    msg['From'] = '[email protected]'
    msg['To'] = '[email protected]'
    

    On msg.set_content() I have to send in the cte. Python 3 Email Content Encoding Types