Automatic Email Sender in Python – Python Code to Send Email

In today’s world emails have become an integral part of our lives, and sending personalized emails to a group of people is a common practice. But what if you have to send emails to a large group of people, and that too, based on certain criteria like birthday greetings or reminders? Sending such emails manually could be a tedious and time consuming task. That is where automated email programs come into the picture. In this post we’ll learn how to build a program that automatically sends emails to a group of people based on certain criteria.

Create a News Feed Application in Python | Python Project

Automatic Email Sender in Python
Step 1: Set up a Gmail account

Automatic Email Sender in Python we need to set up a Gmail account that will act as a sender for our program. We’ll use the Google API to interact with our Gmail account so let’s get started by creating a new Google API project.

Step 2: Create a Google API project

Go to the Google API Console and create a new project. Once the project is created enable the Gmail API by following these steps:

Click on the Library tab on the left sidebar.
Search for Gmail API and select it.
Click on the “Enable” button.
Step 3: Create credentials

To access our Gmail account via the API we need to create credentials. Follow these steps to create credentials:

Click on Credentials tab on left sidebar.
Click on “Create credentials” button.
Select “OAuth client ID” as the credential type.
Choose Desktop App as the application type.
Give your OAuth client ID a name and click on the “Create” button.
On the next screen click on the “Download” button to download credentials file.
Step 4: Install dependencies

We will use Google API client library for Python to interact with Gmail API. Install it using pip:

pip install --upgrade google-api-python-client google-auth google-auth-oauthlib google-auth-httplib2
Step 5: Email Sender Code

Let’s start by writing code to send an email. We’ll create a function send_email that takes recipient’s email address the email subject and the email body as arguments. This function will use Gmail API to send the email.

import base64
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google.oauth2.credentials import Credentials

def send_email(to, subject, body):
try:
credentials = Credentials.from_authorized_user_file('credentials.json', ['https://www.googleapis.com/auth/gmail.send'])
service = build('gmail', 'v1', credentials=credentials)
message = create_message(to, subject, body)
send_message(service, 'me', message)
print(f"Email sent to to")
except HttpError as error:
print(f"An error occurred: error")
message = None
return message

def create_message(to, subject, body):
message = f"From: ChatGPT nTo: tonSubject: subjectnnbody"
b64_bytes = base64.urlsafe_b64encode(message.encode('utf-8'))
b64_string = b64_bytes.decode('utf-8')
return 'raw': b64_string

def send_message(service, user_id, message):
try:
message = (service.users().messages().send(userId=user_id, body=message).execute())
return message
except HttpError as error:
print(f"An error occurred: error")
message = None
return message
The send_email function takes three arguments to (recipient’s email address), subject (email subject), and body (email body). The function first reads the credentials from the credentials.json file and creates a Gmail API client using the build method. It then creates a message using the create_message function and sends it using the send_message function.

The create_message function creates a message string with sender’s name and email, recipient’s email, subject, and body. The message is then base64 encoded and returned as a dictionary with the raw key.

The send_message function takes the Gmail API client, user ID (which is ‘me’ for the authenticated user) and the message as arguments. It sends the message using the messages().send() method of the API client and returns the response.

With this code we can send an email to a recipient using the Gmail API.automatic email sender

Leave a Reply

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