How to Send Beautiful Emails in Python
Last Updated :
28 Apr, 2025
In this article we will send stylized emails using Python we will be using smtplib. The Python smtplib module defines an SMTP client session object which can be used to send mail to any recipient with an SMTP listener. To send emails to any legitimate email address on the internet, "smtplib" produces a Simple Mail Transfer Protocol client session object. Here, the port number is 465(Message submission over TLS protocol).
Setting up your Gmail ID
Step 1: Log in to your Gmail ID and ensure that 2-factor authentication is enabled.
Step 2: Visit this URL and confirm your password. You will be redirected to this webpage.
Step 3: Choose Mail from the Select app drop-down.
Step 4: From the Select device drop-down, choose Other (Custom name).
Step 5: Write Python Mail App and click Generate.
A Generated app password window will pop up, with your password in a yellow box. Copy that password.
Creating Beautiful Emails in Python
Step 1: Import smtplib and EmailMessage classes in your code.
Python3
import smtplib
from email.message import EmailMessage
Step 2: Create two variables to hold the email address and password copied from the yellow box. Do not use your primary Gmail password here.
Python3
Step 3: Create an instance of EmailMessage class.
Python3
Step 4: Set the Subject, From email address, To email address (this can also be a list of email addresses), and Content in the msg object.
Python3
msg['Subject'] = 'Subject of mail sent by Python code'
msg['From'] = EMAIL_ADDRESS
msg['To'] = EMAIL_ADDRESS
msg.set_content('Content of mail sent by Python code')
Step 5: Now set the SMTP host and port number for SSL. Then provide the email address and password to the login() function. Using send_message() function with msg object we can send the composed email.
Python3
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
Sending Simple Emails in Python
Sending Simple Beautiful Emails in Python, After the successful execution of the code, you will receive this email in your inbox.
Python3
import smtplib
from email.message import EmailMessage
EMAIL_ADDRESS = 'replace with your email address'
EMAIL_PASSWORD = 'replace with yellow box password'
msg = EmailMessage()
msg['Subject'] = 'Subject of mail sent by Python code'
msg['From'] = EMAIL_ADDRESS
msg['To'] = EMAIL_ADDRESS
msg.set_content('Content of mail sent by Python code')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
Output:
Plain EmailSending Stylized Beautiful Email in Python
Using HTML and CSS we can add various styles to the email. Set the HTML formatted content using the set_content() function as follows:
Syntax: set_content('''<HTML code>''', subtype='html')
Example
Using SMTPLib library and HTML/CSS we can beautify our email composition. After executing the below code, you will receive a mail with rendered HTML content.
Python3
import smtplib
from email.message import EmailMessage
EMAIL_ADDRESS = 'replace with your email address'
EMAIL_PASSWORD = 'replace with yellow box password'
msg = EmailMessage()
msg['Subject'] = 'Beautiful Subject'
msg['From'] = EMAIL_ADDRESS
msg['To'] = EMAIL_ADDRESS
msg.set_content('''
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" hs-webfonts="true" href="https://fonts.googleapis.com/css?family=Lato|Lato:i,b,bi">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
h1{font-size:56px}
h2{font-size:28px;font-weight:900}
p{font-weight:100}
td{vertical-align:top}
#email{margin:auto;width:600px;background-color:#fff}
</style>
</head>
<body bgcolor="#F5F8FA" style="width: 100%; font-family:Lato, sans-serif; font-size:18px;">
<div id="email">
<table role="presentation" width="100%">
<tr>
<td bgcolor="#00A4BD" align="center" style="color: white;">
<h1> Welcome!</h1>
</td>
</table>
<table role="presentation" border="0" cellpadding="0" cellspacing="10px" style="padding: 30px 30px 30px 60px;">
<tr>
<td>
<h2>Custom stylized email</h2>
<p>
You can add HTML/CSS code here to stylize your emails.
</p>
</td>
</tr>
</table>
</div>
</body>
</html>
''', subtype='html')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
Output:
Stylized Email
Similar Reads
How to Send Automated Email Messages in Python In this article, we are going to see how to send automated email messages which involve delivering text messages, essential photos, and important files, among other things. in Python. We'll be using two libraries for this: email, and smtplib, as well as the MIMEMultipart object. This object has mul
6 min read
How To Send Files Using Python Built-In Http Server Python's built-in HTTP server offers a straightforward way to share files over a local network or the internet without the need for complex setups. In this tutorial, we'll walk through the step-by-step process of using Python's built-in HTTP server to send files to clients. Setting Up the HTTP Serve
2 min read
How to read Emails from Gmail using Gmail API in Python ? In this article, we will see how to read Emails from your Gmail using Gmail API in Python. Gmail API is a RESTful API that allows users to interact with your Gmail account and use its features with a Python script. So, let's go ahead and write a simple Python script to read emails. RequirementsPytho
6 min read
How to Create a Link to Send Email in HTML ? Incorporating email links into HTML pages facilitates a smooth way for users to send a message directly from the webpage. We will see how to create these email links, making your web pages user-friendly and interactive.Using Mailto Protocol with Predefined SubjectTo create a link to send an email in
2 min read
How to Schedule an Email in Gmail In today's digital age, email is an indispensable tool for both personal and professional communication. While we often send emails immediately, there are times when scheduling an email to be sent later is more beneficial. Scheduling emails in Gmail ensures timely delivery, even when you're unavaila
7 min read