Gmail SMTP authentication issue 534 with Python deployment

I’m experiencing a challenging SMTP authentication issue while attempting to send emails via Gmail using Python on a cloud-based service. This error has cropped up recently, even though my code has been functioning properly for months.

The specific error message I receive is:

SMTPAuthenticationError: (534, '5.7.14 Please sign in through your web browser and then try again')

Here’s a portion of my code that’s used for sending emails:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

company_name = 'hamburger heaven'
deal_type = 'consultation'
analyzer = 'John'
item_id = '12345678'
sales_rep = 'alice smith'
website_link = 'link to company website'
analyzer_firstname = analyzer.split(' ')[0]
sales_rep_firstname = sales_rep.split(' ')[0]
contact_email = '[email protected]'
analyzer_email = '[email protected]'
email_pass = 'securepassword'

email_message = MIMEMultipart()
email_message['From'] = analyzer
email_message['To'] = contact_email
email_message['Subject'] = f'Introduction to {company_name} - {deal_type} onboarding'

email_body = f'Thank you {sales_rep_firstname},\n\nHi {analyzer_firstname},\n\nWe are happy to collaborate with you. To begin our onboarding process, please visit this link (link to onboarding {website_link}/{item_id}). If you have any questions while preparing the required documents, don't hesitate to reach out to me.'

email_message.attach(MIMEText(email_body, 'plain'))

smtp_server = smtplib.SMTP('smtp.gmail.com', 587)
smtp_server.ehlo()
smtp_server.starttls()
smtp_server.login(analyzer_email, email_pass)
smtp_server.sendmail(analyzer_email, contact_email, email_message.as_string())
smtp_server.quit()

I’ve ensured that less secure apps are permitted in my Gmail settings, but the issue continues. Has anybody else faced this authentication problem lately? Any help in resolving it would be greatly appreciated.

Error 534 indicates that Gmail is blocking your login due to potential suspicious activity. If this code was working well for an extended period, it’s likely that Google flagged your cloud service’s IP or detected unusual login behaviors.

I encountered a similar issue while using AWS and resolved it by utilizing App Passwords rather than standard passwords. Make sure to enable Two-Factor Authentication (2FA) in your Google Account settings first, then generate an App Password specifically for your Python application. Replace your existing password with this 16-character App Password.

Additionally, you may want to log into Gmail directly from the IP address where your cloud service operates, as this can sometimes mitigate geographic restrictions that trigger this error.

Had this exact problem last week! Google changed something with their security recently. Switch to OAuth2 instead of basic auth - it’s more secure and Gmail prefers it. Stack Overflow has solid tutorials for setting it up with Python’s smtplib.

Had this exact problem two months ago on my production server. Gmail flags automated logins from cloud providers as suspicious, especially when you’re spinning up new instances or changing IPs frequently. Here’s what fixed it for me: SSH into your server and access Gmail through a text browser like lynx from the same IP your app uses. That’ll temporarily solve the auth issue. But honestly? Just switch to Gmail’s API instead of SMTP. Yeah, it’s more setup work upfront, but you’ll never deal with these auth headaches again since it uses OAuth tokens instead of passwords.