How to Send Email Using Python: A Step-by-Step Guide

SRISHTI AGARWAL
3 min readAug 13, 2023

--

Introduction:
In this tutorial, we’ll learn how to send emails using Python. Before we dive into the code, let’s configure our Gmail account to enable sending emails programmatically. We’ll set up the necessary security measures and then proceed with the code to send an email using Python

Step 1: Configure Your Gmail Account

  1. Go to your Gmail account and click on “Manage your Google Account.”
  2. Select “Security” from the left side panel, then set up “2-Step Verification.”
  3. After enabling 2-Step Verification, search for “App Passwords” and click on it.
  4. Choose “Select app,” then select “Other” and provide a name for the app (e.g., Python Email) that will use the app password. Click “Done.”

Step 2: Setting Up Python Code

  1. First, we need to install the “smtplib” library. Open your Jupyter Notebook or any Python environment and install the library using the following command:

2. Next, import the “smtplib” module:

Step 3: Sending the Email

  1. To send an email, we need to define two variables: sender’s email and the app password obtained from Step 1.

2. Connect to the Gmail SMTP server using TLS for security:

3. Log in to your Gmail account:

4. Compose the email that you want to send and store it in a variable named “mail_content” or any other name of your choice:

5. Send the email:

Replace “receiver_email” with the recipient’s email address.

6. Close the connection to the SMTP server:

Complete Code:
Here’s the complete Python code to send an email:

Conclusion:
In this tutorial, we learned how to send emails using Python. By following the step-by-step guide, you can now send emails programmatically using the “smtplib” library and a configured Gmail account.

THANKYOU!!

HAPPY CODING:)

--

--