This project is focused on setting up a test Gmail account and enabling the necessary APIs to allow for programmatically sending emails and searching for specific messages in the mailbox based on keywords in the subject or body text. The goal of this project is to provide a simple guide for setting up a Gmail API connection in Python and demonstrating the basic functionalities that it offers. To get started, you will need to follow a few basic steps to set up a test Gmail account and enable the necessary APIs.
To create a Gmail Test account, follow the steps below:
-
Go to the Gmail website (https://mail.google.com/) and click on the "Create account" button.
-
Fill out the registration form with your personal information, such as your name, birthdate, and location. You will also need to choose a username and password.
-
For security reasons, you may be required to provide a phone number or an alternate email address for verification purposes while creating a Gmail Account.
-
Once you have set up your new Gmail account, you can use it to send and receive test emails, as well as to enable and test the Gmail API functionality.
-
Go to Google Cloud Console and click Create project and select the project.
-
To enable API, Go to the navigation bar > APIs & Services > Library, go to GMail API, and click on the enable button.
-
Go to navigation bar > APIs & Services > OAuth Consent Screen
-
Add the scope https://mail.google.com/ and add test users (i.e., Email ID), and check test users and scopes in the summary section.
-
To create an OAuth Client ID - go to the navigation bar > APIs & Services > Create Credentials > OAuth Client ID. Choose Application type as Desktop App and click on Create.
-
OAuth client ID is successfully created. Download the JSON file and rename the file as 'credentials.json'. Now move the file to the Python code's working directory (i.,e, location at which the src.py file exists)
Python3.11.2
To check if the specific version of Python has been installed in your system
- Go to terminal/command prompt, and type, python --version
- The Python version will be displayed on the screen. Update version 3.11.2.
- If Python is not installed in your system, please follow the steps in this document
- You have now successfully installed/updated Python!
To install the required packages, follow the steps below:
-
Open a command-line interface (e.g. Terminal on macOS, Command Prompt on Windows).
-
Navigate using the cd command to the directory where the requirements.txt file is located. for example cd folder name
-
Run the following command to install the packages listed in the requirements.txt file: pip install -r requirements.txt
-
This command will read the requirements.txt file and install all the packages listed in it, along with their dependencies, if they are not already installed.
-
Wait for the installation process to complete.
-
Once the installation is complete, you should be able to use the installed packages.
To run the above code,
-
Replace the receiver email ID in the create_message(), In addition, you can also make changes in the header, body of the email as well as the query in search_email(), based on requirements.
-
Open the terminal on your computer. On Windows, this is the Command Prompt, On macOS it is terminal.
-
Navigate to the directory where the Python file is located using the "cd" command. For example, if the file is located on your Desktop, you can navigate to it by typing "cd Desktop" in the terminal.
-
Once you are in the correct directory, type "python" followed by the name of the Python file you want to run. For example, if your file is named "src.py", you would type "python src.py" in the terminal/command prompt.
-
Press Enter to run the file.
If this is the first time running the code, you must authorize the user on the redirected sign-in page. To do so, sign in to your Google account using your Gmail login credentials. Once you've approved the user, you should be able to see the relevant emails in your inbox, and the terminal will display a list of emails that contain the keyword 'recommendation service' or the keyword specified in the query.