Open In App

How to Send Beautiful Emails in Python

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Setting up your Gmail ID

 

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




EMAIL_ADDRESS = 'emailaddress@domain.com'
EMAIL_PASSWORD = 'yellowboxpassword'


Step 3: Create an instance of EmailMessage class. 

Python3




msg = EmailMessage()


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:

Creating Beautiful Emails in Python

Plain Email

Sending 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:

Sending Stylized Beautiful Email in Python

Stylized Email



Last Updated : 10 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads