How to send HTML content in email using Python? I can send simple texts.
-
1Just a big fat warning. If you are sending non-ASCII email using Python < 3.0, consider using the email in Django. It wraps UTF-8 strings correctly, and also is much simpler to use. You have been warned :-)– Anders Rune JensenDec 14, 2009 at 14:42
-
2If you want to send a HTML with unicode see here: stackoverflow.com/questions/36397827/…– guettliApr 11, 2016 at 10:53
12 Answers
From Python v2.7.14 documentation - 18.1.11. email: Examples:
Here’s an example of how to create an HTML message with an alternative plain text version:
#! /usr/bin/python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# me == my email address
# you == recipient's email address
me = "[email protected]"
you = "[email protected]"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()
-
1Is it possible to attach a third and a fourth part, both of which are attachments (one ASCII, one binary)? How would one do that? Thanks. Dec 5, 2010 at 20:55
-
1Hi, I noticed that in the end you
quit
thes
object. What if I want to send multiple messages? Should I quit everytime I send the message or send them all (in a for loop) and then quit once and for all?– xpantaMay 9, 2012 at 9:58 -
4Make sure to attach html last, as the preferred(showing) part will be the one attached last.
# According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred.
I wish i read this 2hrs ago– dwkdJul 4, 2015 at 21:44 -
1
-
8Hmm, I get the error for msg.as_string(): list object has no attribute encode– WJAFeb 11, 2019 at 14:44
Here is a Gmail implementation of the accepted answer:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# me == my email address
# you == recipient's email address
me = "[email protected]"
you = "[email protected]"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via local SMTP server.
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('userName', 'password')
mail.sendmail(me, you, msg.as_string())
mail.quit()
-
4
-
27I use a google application specific password with python smtplib, did the trick without having to go low security.– yoyoJun 30, 2015 at 22:48
-
2for anyone reading the above comments: You only require an "App Password" if you have previously enabled 2 step verification in your Gmail account.– MugenOct 11, 2018 at 16:21
-
Is there a way to append something dynamically in the HTML part of the message?– magmaMay 9, 2020 at 13:59
-
You might try using my mailer module.
from mailer import Mailer
from mailer import Message
message = Message(From="[email protected]",
To="[email protected]")
message.Subject = "An HTML Email"
message.Html = """<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.</p>"""
sender = Mailer('smtp.example.com')
sender.send(message)
-
Mailer module is great however it claims to work with Gmail, but doesn't and there are no docs.– MFBJul 18, 2012 at 19:24
-
1@MFB -- Have you tried the Bitbucket repo? bitbucket.org/ginstrom/mailer Aug 16, 2012 at 4:11
-
4For gmail one should provide
use_tls=True
,usr='email'
andpwd='password'
when initializingMailer
and it will work. Jul 21, 2014 at 8:21 -
I would recommend adding to your code the following line right after the message.Html line:
message.Body = """Some text to show when the client cannot show HTML emails"""
– IvanDAug 5, 2015 at 3:37 -
great but how to add the variables values to the link i mean creating a link like this <a href="python.org/somevalues">link</a> So that i can access that values from the routes it goes to . Thanks Jun 4, 2016 at 11:17
Here is a simple way to send an HTML email, just by specifying the Content-Type header as 'text/html':
import email.message
import smtplib
msg = email.message.Message()
msg['Subject'] = 'foo'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg.add_header('Content-Type','text/html')
msg.set_payload('Body of <b>message</b>')
# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
s.starttls()
s.login(email_login,
email_passwd)
s.sendmail(msg['From'], [msg['To']], msg.as_string())
s.quit()
-
2This is a nice simple answer, handy for quick and dirty scripts, thanks. BTW one can refer to the accepted answer for a simple
smtplib.SMTP()
example, which doesn't use tls. I used this for an internal script at work where we use ssmtp and a local mailhub. Also, this example is missings.quit()
.– Mike SSep 2, 2015 at 14:42 -
1"mailmerge_conf.smtp_server" is not defined... at least is what Python 3.6 says...– ZEEDec 27, 2017 at 20:47
-
Absolutely beautiful! Adding .add_header and using .set_payload instead of .set_content worked like a charm for me! Emails looking snazzy now. Thanks again! Jul 27, 2022 at 8:56
for python3, improve @taltman 's answer:
- use
email.message.EmailMessage
instead ofemail.message.Message
to construct email. - use
email.set_content
func, assignsubtype='html'
argument. instead of low level funcset_payload
and add header manually. - use
SMTP.send_message
func instead ofSMTP.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'] = '[email protected]'
email['To'] = '[email protected]'
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)
-
3As an improvement to this if you want to send an attachment in addition, use
email.add_alternative()
(in the same way as you have withemail.set_content()
to add HTML, and then add an attachment usingemail.add_attachment()
(This took me bloomin ages to figure out) Oct 30, 2020 at 20:26 -
2The
EmailMessage
API was only officially available starting in Python 3.6, though it was shipped as an alternative already in 3.3. New code should absolutely use this instead of the oldemail.message.Message
legacy API which most of the answers here unfortunately still suggest. Anything withMIMEText
orMIMEMultipart
is the old API and should be avoided unless you have legacy reasons.– tripleeeFeb 8, 2022 at 20:53 -
is there a way to use a file instead of typing out the HTML in
email.set context
?– MichaelAug 18, 2022 at 10:05 -
I found the way to add a HTML files. ``` report_file = open('resetpasswordemail.html') html = report_file.read() em.set_content(html, subtype='html')```– MichaelAug 18, 2022 at 10:28
Here's sample code. This is inspired from code found on the Python Cookbook site (can't find the exact link)
def createhtmlmail (html, text, subject, fromEmail):
"""Create a mime-message that will render HTML in popular
MUAs, text in better ones"""
import MimeWriter
import mimetools
import cStringIO
out = cStringIO.StringIO() # output buffer for our message
htmlin = cStringIO.StringIO(html)
txtin = cStringIO.StringIO(text)
writer = MimeWriter.MimeWriter(out)
#
# set up some basic headers... we put subject here
# because smtplib.sendmail expects it to be in the
# message body
#
writer.addheader("From", fromEmail)
writer.addheader("Subject", subject)
writer.addheader("MIME-Version", "1.0")
#
# start the multipart section of the message
# multipart/alternative seems to work better
# on some MUAs than multipart/mixed
#
writer.startmultipartbody("alternative")
writer.flushheaders()
#
# the plain text section
#
subpart = writer.nextpart()
subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
mimetools.encode(txtin, pout, 'quoted-printable')
txtin.close()
#
# start the html subpart of the message
#
subpart = writer.nextpart()
subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
#
# returns us a file-ish object we can write to
#
pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
mimetools.encode(htmlin, pout, 'quoted-printable')
htmlin.close()
#
# Now that we're done, close our writer and
# return the message body
#
writer.lastpart()
msg = out.getvalue()
out.close()
print msg
return msg
if __name__=="__main__":
import smtplib
html = 'html version'
text = 'TEST VERSION'
subject = "BACKUP REPORT"
message = createhtmlmail(html, text, subject, 'From Host <[email protected]>')
server = smtplib.SMTP("smtp_server_address","smtp_port")
server.login('username', 'password')
server.sendmail('sende[email protected]', '[email protected]', message)
server.quit()
-
FYI; this came from code.activestate.com/recipes/67083-send-html-mail-from-python/… Jul 28, 2014 at 23:19
-
1This is even older and cruftier than the
email.message.Message
API. TheMimeWriter
library was deprecated in Python 2.3 in 2003.– tripleeeFeb 8, 2022 at 20:58
Actually, yagmail took a bit different approach.
It will by default send HTML, with automatic fallback for incapable email-readers. It is not the 17th century anymore.
Of course, it can be overridden, but here goes:
import yagmail
yag = yagmail.SMTP("[email protected]", "mypassword")
html_msg = """<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.</p>"""
yag.send("[email protected]", "the subject", html_msg)
For installation instructions and many more great features, have a look at the github.
Here's a working example to send plain text and HTML emails from Python using smtplib
along with the CC and BCC options.
https://varunver.wordpress.com/2017/01/26/python-smtplib-send-plaintext-and-html-emails/
#!/usr/bin/env python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_mail(params, type_):
email_subject = params['email_subject']
email_from = "[email protected]"
email_to = params['email_to']
email_cc = params.get('email_cc')
email_bcc = params.get('email_bcc')
email_body = params['email_body']
msg = MIMEMultipart('alternative')
msg['To'] = email_to
msg['CC'] = email_cc
msg['Subject'] = email_subject
mt_html = MIMEText(email_body, type_)
msg.attach(mt_html)
server = smtplib.SMTP('YOUR_MAIL_SERVER.DOMAIN.COM')
server.set_debuglevel(1)
toaddrs = [email_to] + [email_cc] + [email_bcc]
server.sendmail(email_from, toaddrs, msg.as_string())
server.quit()
# Calling the mailer functions
params = {
'email_to': '[email protected]',
'email_cc': '[email protected]',
'email_bcc': '[email protected]',
'email_subject': 'Test message from python library',
'email_body': '<h1>Hello World</h1>'
}
for t in ['plain', 'html']:
send_mail(params, t)
I may be late in providing an answer here, but the Question asked a way to send HTML emails. Using a dedicated module like "email" is okay, but we can achieve the same results without using any new module. It all boils down to the Gmail Protocol.
Below is my simple sample code for sending HTML mail only by using "smtplib" and nothing else.
```
import smtplib
FROM = "[email protected]"
TO = "[email protected]"
SUBJECT= "Subject"
PWD = "thesecretkey"
TEXT="""
<h1>Hello</h1>
""" #Your Message (Even Supports HTML Directly)
message = f"Subject: {SUBJECT}\nFrom: {FROM}\nTo: {TO}\nContent-Type: text/html\n\n{TEXT}" #This is where the stuff happens
try:
server=smtplib.SMTP("smtp.gmail.com",587)
server.ehlo()
server.starttls()
server.login(FROM,PWD)
server.sendmail(FROM,TO,message)
server.close()
print("Successfully sent the mail.")
except Exception as e:
print("Failed to send the mail..", e)
```
-
The
email
library is not a "new module", it is part of the standard library just likesmtplib
. You should absolutely not create messages by combining strings like this; it will seem to work for very simple messages, but fail in catastrophic ways as soon as you move outside the comfortable realm of a singletext/plain
payload with only 7-bit US-ASCII contents (and even then there are corner cases which are likely to trip you up, especially if you don't know exactly what you are doing).– tripleeeFeb 8, 2022 at 10:14
Here is my answer for AWS using boto3
subject = "Hello"
html = "<b>Hello Consumer</b>"
client = boto3.client('ses', region_name='us-east-1', aws_access_key_id="your_key",
aws_secret_access_key="your_secret")
client.send_email(
Source='ACME <[email protected]>',
Destination={'ToAddresses': [email]},
Message={
'Subject': {'Data': subject},
'Body': {
'Html': {'Data': html}
}
}
Simplest solution for sending email from Organizational account in Office 365:
from O365 import Message
html_template = """
<html>
<head>
<title></title>
</head>
<body>
{}
</body>
</html>
"""
final_html_data = html_template.format(df.to_html(index=False))
o365_auth = ('sender_username@company_email.com','Password')
m = Message(auth=o365_auth)
m.setRecipients('receiver_username@company_email.com')
m.setSubject('Weekly report')
m.setBodyHTML(final_html_data)
m.sendMessage()
here df is a dataframe converted to html Table, which is being injected to html_template
-
2The question doesn't mention anything about using Office or an organizational account. Good contribution but not very helpful to the asker Mar 25, 2020 at 14:49
In case you want something simpler:
from redmail import EmailSender
email = EmailSender(host="smtp.myhost.com", port=1)
email.send(
subject="Example email",
sender="[email protected]",
receivers=["[email protected]"],
html="<h1>Hi, this is HTML body</h1>"
)
Pip install Red Mail from PyPI:
pip install redmail
Red Mail has most likely all you need from sending emails and it has a lot of features including:
- Attachments
- Templating (with Jinja)
- Embedded images
- Prettified tables
- Send as cc or bcc
- Gmail preconfigured
Documentation: https://red-mail.readthedocs.io/en/latest/index.html
Source code: https://github.com/Miksus/red-mail