Introduction

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.

Prerequisites

Python 3.8.0, Gmail Security Settings, Gmail SMTP Server – gmail.smtp.com, Gmail SMTP Ports – 587, 465

Creating Python Script

We will create below Python script called html_email.py. 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()
msg.add_alternative("""\
<html>
  <head></head>
  <body>
    <p>Hello</p>
    <p>Here is an example of sending HTML email using Python. Please click on below link:
        <a href="https://www.roytuts.com/how-to-send-an-html-email-using-python/">
            Send an HTML email using Python
        </a>
    </p>
  </body>
</html>
""".format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')

fromEmail = '[email protected]'
toEmail = '[email protected]'

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

s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(fromEmail, 'gmail password')
s.send_message(msg)
s.quit()

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 – smtp.gmail.com 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', '[email protected]')

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  https://support.google.com/mail/?p=BadCredentials 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('smtp.gmail.com', 587) by s = smtplib.SMTP_SSL('smtp.gmail.com', 465).

Remove line s.starttls().

Testing the Email Sending

Just executing the file html_email.py 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.

Thanks for reading.

Tags:

Leave a Reply

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