2026-07-15

Sending email with python. Google disables less secure apps

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

Sending email with python. Google disables less secure apps

Sending Email with Python: Navigating Google's Security Changes and SMTP Authentication Errors

As a senior developer, I often see these kinds of friction points arise when relying on third-party services for core functionality. The scenario you are describing—where working code suddenly fails due to changes in provider security policies—is extremely common, especially with major platforms like Google updating their security protocols (such as disabling "Less Secure Apps").

This post will diagnose why your Python smtplib script is failing and provide the modern, secure solution for sending emails via Gmail.


The Root Cause: Why Your Code Fails Now

You are encountering smtplib.SMTPAuthenticationError and smtplib.SMTPNotSupportedError because Google and other major email providers have significantly tightened security measures. They no longer accept your regular account password for programmatic access to services like Gmail when two-factor authentication (2FA) is enabled or if older, less secure application methods are deprecated.

The old method relied on simple username/password credentials being sufficient. Modern systems require stronger authentication mechanisms. When you tried to use your standard Google password, the server rejected it as insufficient for this type of connection, leading to the authentication error. The SMTPNotSupportedError often appears when attempting protocol negotiation (like STARTTLS) over connections that are now locked down by stricter security layers.

The Solution: Using Application-Specific Passwords

The solution is to stop using your primary Google account password and instead generate a specific, restricted password designed only for this application—this is known as an App Password.

Google strongly recommends this method because it allows you to maintain strong security on your main account while granting third-party applications limited access. This bypasses the need for legacy "Less Secure Apps" permissions entirely.

Step 1: Generating an App Password in Google

To use this solution, you must enable 2-Step Verification on your Google Account first. Once enabled, follow these steps to generate the required password:

  1. Go to your Google Account Security settings.
  2. Navigate to the "App Passwords" section (this is often found under the Advanced Security settings).
  3. Generate a new password for the specific application you are using (in this case, the Python script).
  4. Google will provide you with a unique, 16-character password. Use this generated password in place of your regular email/password when configuring your Python script.

Step 2: Implementing the Fix in Python

You must replace serverPw with the newly generated App Password. The rest of your smtplib logic remains sound, but the credentials provided to the server must be valid under current security standards.

Here is the corrected structure emphasizing the use of the secure application password:

import smtplib
from email.mime.text import MIMEText

# --- Configuration ---
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
# Use your actual Gmail address
serverEmail = "YOUR_GMAIL_ADDRESS@gmail.com"
# *** IMPORTANT: Use the generated App Password here, NOT your main account password ***
serverPw = "YOUR_GENERATED_APP_PASSWORD" 

subject = "Rejection"
body = "Hi! You've been unfortunately declined access to our system."

message = f'Subject: {subject}\n\n{body}'

try:
    # Login using the application-specific credentials
    server.login(serverEmail, serverPw)
    
    # Send the mail
    server.sendmail(serverEmail, doctorEmail['email'], message)
    print("Email sent successfully!")

except smtplib.SMTPAuthenticationError as e:
    print(f"Authentication Error: Failed to log in. Check if your App Password is correct. Details: {e}")
except smtplib.SMTPServerDisconnected as e:
    print(f"Connection Error: Server disconnected unexpectedly. Details: {e}")

finally:
    server.quit()

Best Practices for Modern Application Development

This situation highlights a fundamental principle in modern software development: never hardcode or store sensitive credentials. Whether you are building a simple script or a large-scale application, security must be paramount. When integrating services like email providers, relying on established, secure protocols is crucial. For complex service integrations and robust backend architecture—much like the systems built using frameworks like Laravel—understanding these authentication layers is essential for creating reliable and secure applications. Always favor OAuth2 flows or dedicated API keys over direct password usage whenever possible.

Conclusion

The error you faced was not a bug in your Python syntax but a necessary security upgrade from Google. By switching to App Passwords, you have successfully moved your email sending mechanism onto an accepted, modern authentication pathway. Remember that security posture is not optional; it is the foundation upon which all reliable software must be built. Always prioritize secure credential management in your code.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.