In this tutorial we will see how send html email using Python with few lines of code. We will use the default smtplib library and email module of Python to send HTML email. In the first part of this tutorial we covered how to send plain emails using Python and Gmail SMTP.
In the first part of the tutorial we discussed How to send plain email using Python and Gmail. Here also we are using the same modules to send HTML emails.
Steps to Send HTML Email Using Python
Step1: First required smtplib and email module needs to be imported.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
Here MIMEMultipart and MIMEText are imported for constructing the email message. These are the class from email.mime.multipart
and ‘email.mime.text‘ module.
Step2: Next define all the values required for connection and sending the email.
# Gmail SMTP configuration
smtp_server = 'smtp.gmail.com'
smtp_port = 587
sender_email = 'sender_email_address'
sender_password = 'Gmail Application Password'
receiver_email = 'receiver email address'
# Create a multipart message
message = MIMEMultipart('alternative')
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = 'HTML Email'
From line number 2 to 6 we are giving the configuration values for connecting to Gmail SMTP. Note: You have use the Gmail application password for connection. The regular password will not work for security reason. Check this tutorial how to generate Gmail App password for send mail.
In line number 10, we create a MIMEMultipart("alternative")
instance. This allows us to create separate text and HTML versions of the email and combine them into a single message. If, for any reason, the HTML version fails to render properly, the text version will serve as a fallback option. This ensures that the email can be viewed even if HTML rendering issues occur.
Step3: After that create the body of the email (both text version and HTML version)
# write the text/plain part
text = """\
Hi,
Check out the new blog post on the Python Addict: "Send Email Using Python:
at https://pythonaddict.com/python/sending-emails-with-python/.
Let us know what content would be useful for you!"""
# write the HTML part
html = """\
<html>
<body>
<p>Hi,<br>
Check out the new blog post on the Python Addict: "Send Email Using Python:</p>
<p><a href="https://pythonaddict.com/python/sending-emails-with-python/">Send Email Using Python</a></p>
<p> Feel free to <strong>let us know</strong> What else we can add for you!</p>
</body>
</html>
"""
Step4: Convert both the part to MIMEText and add to MIMEMultipart message.
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)
Step5: Now create the connection and send mail using the send_message() function.
# Create a secure connection and send the email
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, sender_password)
server.send_message(message)
print('Email sent successfully.')
Here is the full code to send HTML email using Python and free Gmail SMTP connection.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Gmail SMTP configuration
smtp_server = 'smtp.gmail.com'
smtp_port = 587
sender_email = 'sender_email_address'
sender_password = 'Gmail App password'
receiver_email = 'receiver_email_address'
# Create a multipart message
message = MIMEMultipart('alternative')
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = 'HTML Email'
# write the text/plain version of the email
text = """\
Hi,
Check out the new blog post on the Python Addict: "Send Email Using Python:
at https://pythonaddict.com/python/sending-emails-with-python/.
Let us know what content would be useful for you!"""
# write the HTML version of the email
html = """\
<html>
<body>
<p>Hi,<br>
Check out the new blog post on the Python Addict: "Send Email Using Python:</p>
<p><a href="https://pythonaddict.com/python/sending-emails-with-python/">Send Email Using Python</a></p>
<p> Feel free to <strong>let us know</strong> What else we can add for you!</p>
</body>
</html>
"""
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)
# Create a secure connection and send the email
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, sender_password)
server.send_message(message)
print('Email sent successfully.')
Conclusion:
In this tutorial we covered how to send HTML email using Python and free Gmail SMTP connection. If you have any doubt regarding any part of the topic please feel free to comment below. In the next post we have discussed how to send email with attachment using Python.
You may also like how to send plain email using Python.