Gmail is one of the most popular mail services. Owned by Google the trusty worthy company and many people use Gmail to send official and casual emails. As Gmail provides 2-Step Verification(2SV) to its users, so the hacker cannot easily hack the user account and the details and mails remain safe.

Sign in - Google Accounts. Gmail API is a RESTful API that allows users to interact with your Gmail account and use its features with a Python script. So, let’s go ahead and write a simple Python script to read emails. Simple Brute force gmail using python. GitHub Gist: instantly share code, notes, and snippets. A single username and password gets you into everything Google (Gmail, Chrome, YouTube, Google Maps). Set up your profile and preferences just the way you like. Switch between devices, and pick up wherever you left off.

Although the 2 Step Verification of Gmail is a must option to activate, if your 2SV is on for your Gmail account, you will find difficulties sending email from the Python SMTP module. This is obvious because Gmail does not allow you to use the third party software or package to login account if your 2SV is active.

Although with the Python SMTP module you can send emails using your Gmail account, but you have to downgrade your Gmail security by deactivating the 2SV feature.

Is there any other option of sending Email with Gmail in Python without deactivating the 2 Factor Authentication?, the answer is Yes It’s all thanks to Gmail App Passwords API which allows you to send an email with Python without deactivating the 2SV and without even specifying the original password in the Python script.

What is Gmail App Passwords?

The Gmail APP Password is an API that generates a 16-digit passcode for your non-google application and allows you to use that password to access your Google account. And You can only use the Gmail App Password if your 2 Step Verification is on.

Here in this Python tutorial, we will walk you through How to send email in Gmail using Python. And we will also learn how to send attachments files to Gmail Emails using Python.

Send Gmail Email with Python

Gmail Account Creator Python

Step 1: Create a Gmail Account

This goes without saying, if you want to send an email you first require a Gmail account. You can create a Gmail account by signup here.

For this tutorial, we have created a new Gmail account by name codehundred100@gmail..com

Step2: Create a Gmail App Password

How to create and use an App Password detail you will find here.

According to the given instruction

a: Go to your Google Account

b: Click on Security

In the security, section looks for the App passwords and click on it to create a new app Passcode.

Note: Also, make sure that your 2-Step Verification is on

c: Select Other Custom Name:

In the App Passwords select the Other (Custom Name) for Select App option

d: Generate App

Now generate an App By name My App, the name My App is arbitrary.

e: Copy the Generated App Password

Note: Do not share the Generated passcode with anyone

Once the password is created copy it to your clipboard because we will be using it in our Python script to access our google email account.

Note: Do not click on the Done button without copying the 16 character passwords else you will lose the password and you have to delete the app and create a new one.

f: All done with App password

Gmail account creator python file

Once your app password is generated you will see a similar screen. Now we are done with the app password, now let’s code to send an email.

Note: You will also be receiving an email from Google after creating the App password.

Step 3: Write Python Program to Send Email through Gmail

#Example 1: python program to send Gmail mails with Python

Execute the Program

Gmail Account Creator Python File

Output

To send the email we are using the Python Standard smtplib and email modules. Both the modules come with Python so you do not need to install them separately.

In the above example, you can see that we have specified the my_address and app_generated_passwordvariables in the open string format which is not a good python practice. You definitely do not want other developers to read your password and email, so we prefer to use environmental variables. But for the ease of this tutorial, we have used them with the simple string.

Check Gmail Sent Box

After executing the above program if you check your Gmail sent box you will see that you have sent an email using Python

As you can see that your message has been saved to the sent box which means the message has been sent.

As the receiver account is also my other account I can check if the receiver account received the mail.

As you can see that I have received the mail from codehundred100@gmail.com.

Gmail

Send Multimedia file with Gmail using Python

In the above example, we only send the text mail using our codehudndred100@gmail.com Gmail account. But if we wish to send an image as mail to someone we have to attach the image file with our mail so how can we do that.

To attach an image file to our mail body we can use the email module add_attachment method, but we also have to import the image file and read it as a binary object, for that we will be using Python standard imghdr module.

#Example 2: python program to send mail with an image attached

Execute the Program

Output

Check your Gmail Sent box

As you can see that the image file has been attached to the mail and send to the receiver address.

Conclusion

In this Python tutorial, we learned how to use the Google app password API to generate 16 characters long passwords and send Gmail emails using Python. In our first python example, we show you how to send simple text mail and in the second example, we show you how to attach an image file to your mail msg and send it to the receiver. Now you know how can you send emails in Gmail using Python without deactivating the 2Step Verification.

If you like this article or have any suggestions please let us know by commenting down below.

Creator

Gmail Account Creator Python

People are also reading:

In a previous post we saw how you could access Gmail in NodeJS. In this post we will see how we can access Gmail using Python.

Turning on the Gmail api

You first need to enable Gmail api and get the required OAuth credentials from your Google account. The steps which are shown below.

1. Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials.

Gmail Account

2. On the Add credentials to your project page, click the Cancel button.

Python Email Gmail

3. At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.

4. Select the Credentials tab, click the Create credentials button and select OAuth client ID.

Gmail Account Creator Python App

5. Select the application type Other, enter the name “Gmail API Quickstart”, and click the Create button.

6. Click OK to dismiss the resulting dialog.

7. Click the Download JSON button to the right of the client ID.

8. Move this file to your working directory and rename it client_secret.json.

Bulk Gmail Account Creator

Install the Gmail api Python library.

Once the above process has been done run the following Python file and you will get the list of labels for your Gmail account.

Now to get an actual message from Gmail we will use the following code. The initial authorization code is the same, just the api calls change.

Mass Gmail Account Creator Free

Detailed functions list for accessing the Gmail api is given here.