Often you may need to send HTML mail to your website/application users. Python allows you to easily create and send HTML emails. In this article, we will look at how to send HTML mail using Python.
How to Send HTML Mail Using Python
Here are the steps to send HTML mail using python’s smtplib.
Create an empty python file
$ sudo vi html_mail.py
Add the following line to define execution environment
#! /usr/bin/python
Next, add the following line to import smtplib required to send emails.
import smtplib
Next, import required functions to send both plain-text as well as HTML email. This is because if you recipient does not support or has disabled HTML emails, we want our email to default to the plain text version.
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
Next, we specify sender and receiver email addresses.
sender= "admin@example1.com"
receiver= "user@example2.com"
Create a message container by specifying MIME type of email, along with sender & receiver email addresses.
# Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternative') msg['Subject'] = "Test HTML Email" msg['From'] = sender msg['To'] = receiver
Then create the message body in both plain-text and HTML version. If the email recipient does not support HTML emails, then python will automatically send the plain text version.
# Create the body of the message (a plain-text and an HTML version). text = "Hi!\nHow are you?\nHere is the link for activation:\nhttp://example2.com" html = """\ <html> <head></head> <body> <p>Hi!<br> How are you?<br> Here is the <a href="http://example2.com">link</a> you wanted. <img src="http://example2.com/static/hello.jpg"/> </p> </body> </html> """
In above HTML message, we have also included an image for your reference, in case you want to send HTML email with image. As shown above, just add img tag with src attribute containing full URL to the image file (. Add the two messages to your message container. According to RFC 2046, the last part of a multipart message, in this case the HTML message, is preferred.
# 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.
msg.attach(part1)
msg.attach(part2)
Finally, we setup an SMTP server and call the sendmail function to send the actual email. It takes 3 arguments – sender, receiver and message to send.
# 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(sender, receiver, msg.as_string()) s.quit()
Please note, although we create a python object to store message details, we convert it to serialized string while using it inside sendmail function.
Here is the full code
#! /usr/bin/python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender= "admin@example1.com"
receiver= "user@example2.com"
# Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternative') msg['Subject'] = "Test HTML Email" msg['From'] = sender msg['To'] = receiver # Create the body of the message (a plain-text and an HTML version). text = "Hi!\nHow are you?\nHere is the link for activation:\nhttp://example2.com" html = """\ <html> <head></head> <body> <p>Hi!<br> How are you?<br> Here is the <a href="http://example2.com">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.
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(sender, receiver, msg.as_string()) s.quit()
In this article, we have learnt how to create simple HTML emails that you can easily modify as per your requirement.
Also read:
How to Find & Replace String in VI Editor
How to Find Parent Process PID in Linux
How to Change Default Shell in Linux
How to Install Moodle with NGINX in Ubuntu
How to Download File in Python