How to Send Beautiful Emails in Python
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
EMAIL_ADDRESS = 'emailaddress@domain.com'
EMAIL_PASSWORD = 'yellowboxpassword'
|
Step 3: Create an instance of EmailMessage class.
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 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(
, 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
Last Updated :
10 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...