How can I send the HTML content in an email using Python? I can send simple text.
for python3, improve @taltman 's answer:
email.message.EmailMessage
instead of email.message.Message
to construct email.email.set_content
func, assign subtype='html'
argument. instead of low level func set_payload
and add header manually.SMTP.send_message
func instead of SMTP.sendmail
func to send email.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('red color text', 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)