Nov-14-2022, 03:42 PM
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
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)