Python

How To Send Emails Using Python

Send Emails using Python

In this tutorial, we will be covering the process of how to send emails using python with the help of SMTP library. Generally, we use this to automate sending a type of email triggered by an activity (transaction confirmation, account verification) or to send to multiple users (newsletters, marketing emails). So let’s begin…

Steps to Send Emails using Python from Gmail account: 

1. Security Changes:

Firstly, we need to make some security changes in our Gmail account. To do this, go to your google account (browser or mobile device) and follow the given process:

Settings > See all Settings > Accounts and Import > Change account settings > Other Google Account settings > Security > Less Secure App Access > Turn on Access

Security Changes

2. Import Libraries: 

Next, we need to import necessary libraries that allow us to send the emails. The first one is SMTP and the second library is MIMEText (part of Email library). However, we do not need to worry about installing any of the libraries since the modules are built-in with Python.

This module, however, requires you to setup the email client configurations provided by the SMTP servers. For using a google (gmail) account, the config is as follows:

  • Name: smtp.gmail.com
  • Port: 587

We can then initialize our SMTP instance.

import smtplib
from email.mime.text import MIMEText

s = smtplib.SMTP('smtp.gmail.com', 587)

3. Authentication: 

After initialization, the next step is to put the SMTP connection in the Transport Layer Security (TLS) mode which does the work of encrypting the commands of SMTP. Now comes the authentication part, for this we need to provide the login credentials of the sender i.e the Email Id and Password of the account that will be used to send the email. 

Note: Port 587 is used for TLS. For SSL based emails, you need to use port 465 (if you’re unaware of the terminologies, we need not worry about them for the tutorial).

s = starttls()
s.login('email_sender@example.com, 'email_sender_password')

4. The Message: 

In this step, we will use the variable ‘message’ to store the message we want to send to the receiver. Along with the message, we also provide the subject as well as the list of email ids of receivers.

Once this is done, we can then send our email through the STMP instance.

# Email Body 
message = MIMEText('Hello this is try mail') 

# Email subject 
message['Subject'] = 'Try mail' 

# Receivers Email Id List 
list=['email_receiver1@example.com','email_receiver2@example.com','email_receiver3@example.com'] 
for i in list: 
  s.sendmail('email_sender@example.com', i, message.as_string()) 

s.quit()  

5. Run the Code:

Finally, we get the entire code:

import smtplib 
from email.mime.text import MIMEText 

s = smtplib.SMTP('smtp.gmail.com', 587) 
s.starttls() 

# Enter your account credentials
s.login('email_sender@example.com', 'email_sender_password') 

# Email Body 
message = MIMEText('Hello this is try mail') 

# Email subject 
message['Subject'] = 'Try mail' 
 
# Receivers Email Id List 
list=['email_receiver1@example.com','email_receiver2@example.com','email_receiver3@example.com'] 
for i in list: 
  s.sendmail('email_sender@example.com', i, message.as_string()) 

s.quit()  

6. Output:

Running the above code will send an email as follows

Send Emails using Python

Happy Learning!!!!

More from byteiota

References

You may also like

Leave a reply

Your email address will not be published. Required fields are marked *