How to send an HTML email using Python


This tutorial will show you how to send an HTML email using Python 3. In our previous example we have seen how to send text message using Python 3 email library. Another variant of sending text message is using RFC 822 standard. In simple text email you don’t have option to decorate your message using styles or you cannot put any HTML link. We will connect to both TLS and SSL ports over Gmail SMTP server.

We will use here email package from Python programming language.


Python 3.8.0, Gmail Security Settings, Gmail SMTP Server –, Gmail SMTP Ports – 587, 465

Creating Python Script

We will create below Python script called You can choose any location to place this file. Make sure your Python is accessible from anywhere using command prompt.

import smtplib

from email.message import EmailMessage
from email.utils import make_msgid

msg = EmailMessage()

asparagus_cid = make_msgid()
    <p>Here is an example of sending HTML email using Python. Please click on below link:
        <a href="">
            Send an HTML email using Python
""".format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')

fromEmail = ''
toEmail = ''

msg['Subject'] = 'HTML Message'
msg['From'] = fromEmail
msg['To'] = toEmail

s = smtplib.SMTP('', 587)
s.login(fromEmail, 'gmail password')

In the above Python script, we first import smtplib for sending the email.

In the next line we import email module for creating email message.

Related Posts:

Next we create the EmailMessage() object and set the body of the message using msg.add_alternative() function.

Then we set the subject for the email and we also set from and to email addresses to send and receive the email respectively.

Next we create a session using Gmail SMTP server – and port – 587.

We need to issue STARTTLS command otherwise you will get the following error:

#smtplib.SMTPSenderRefused: (530, b'5.7.0 Must issue a STARTTLS command first. p7sm24605501pfn.14 - gsmtp', '')

You will get below error if your security level is high in gmail settings:

#smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 x10sm26098036pfn.36 - gsmtp')

Therefore you need to lower your Gmail’s security settings to send email from your gmail address.

Using SSL Port 465

If you want to use SSL port 465 to establish the secure connection with Gmail SMTP server then do the following changes:

Replace s = smtplib.SMTP_SSL('', 587) by s = smtplib.SMTP_SSL('', 465).

Remove line s.starttls().

Testing the Email Sending

Just executing the file using command line tool will send the email.

Once your email successfully sent you will see the following output in Gmail Inbox:

how to send an html email using python

The actual difference between text and HTML email message you will find when you open the message.

As I have sent from my gmail address to my gmail address, so the from email address you see as me in the above image.

When you open the message you will see below output as shown in the image:

how to send an html email using python

Now clicking on the link in the email message will take you to the appropriate page on the website.

That’s all. Hope you find this useful on how to send simple HTML mail using Python 3.

Source Code


Thanks for reading.

1 thought on “How to send an HTML email using Python

Leave a Reply

Your email address will not be published. Required fields are marked *