Search This Blog

Tuesday, June 7, 2022

Python with Gmail - post May 2022 'less secure' option removed

 https://www.abstractapi.com/guides/sending-email-with-python


Useful article on how to get Gmail to work with Python post May 2022 when the "Less Secure" app access was turned off.

Easiest method is to turn on 2FA and then create an application password.



=========

[  Back up of the important text from the link above ] 

Two-factor Authentication

In order to allow our Python client script to access Gmail accounts we will enable two-factor authentication (2FA) which Google calls "2-step verification".

With 2FA you add an extra layer of security to your account in case your password is stolen. After you set it up, you’ll sign in to your account in two steps using:

  • something you know, like your password and
  • something you have, like your phone.

To enable 2FA:

  1. Open your Google Account.
  2. In the navigation panel, select Security.
  3. Under “Signing in to Google,” select 2-Step Verification
Signing in to Google: 2-step Verification
Signing in to Google: 2-step Verification (close-up)

Click the blue GET STARTED button:

Login using your password. Make sure you are using the right account.

Google login

Enter your phone number and click SEND:

Phone number

Wait for the confirmation code to arrive and enter it in:

Confirmation code

Click the TURN ON button:

Turn on

2FA should now be enabled:

2FA on

App Passwords

Now we will generate a special password that our app can use as the 2-step verification.

Back in the Signing in to Google panel select "App passwords" to add a new password:

App passwords: none

Under the "Select app" drop-down select "Other":

App passwords: other

Give your app a name and click GENERATE:

App passwords: generate

Write down (copy to clipboard) your generated password to use in the app:

App password: generated

We should now have our generated app password:

App password: list

Script: Plain Text

Create a file called "gmail.py" and enter the following script:


import smtplib
import ssl

ctx = ssl.create_default_context()
password = "qwertyuiopasdfgh"    # Your app password goes here
sender = "somename@gmail.com"    # Your e-mail address
receiver = "recipient@gmail.com" # Recipient's address
message = """
Hello from Python.
"""

with smtplib.SMTP_SSL("smtp.gmail.com", port=465, context=ctx) as server:
    server.login(sender, password)
    server.sendmail(sender, receiver, message)

The code above creates a secure connection with Gmail’s SMTP server using Secure Sockets Layer encryption (SSL) and automatically upgrades it to TLS. Passing in the context from the create_default_context() function will load the system’s trusted CA certificates, enable certificate validation and hostname checking and try to choose reasonably secure protocol and cipher settings.

Notice how the message variable actually contains a newline as the first character.

Note: Be sure to fill in your: password, sender and receiver strings.

Save the file and go to your terminal and run the script:

No comments: