3

I'm writing a site on Flask and I came across the problem that when sending emails to email users there is such an error.

smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials i1-20020ac25221000000b00478f5d3de95sm4732790lfl.120 - gsmtp')

I searched Google for a solution to the problem, but it said that you need to disable a feature that has not been supported by GMAIL recently. Maybe someone knows how to solve this problem now?

Here is my config connection:

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.getenv('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD')

Help me please

davidism
  • 121,510
  • 29
  • 395
  • 339
Dimaapp
  • 123
  • 1
  • 1
  • 7

1 Answers1

10

Since You can't use the less secure app now as the deadline was 30th May 2022. An alternative way to send Gmail via flask using an App password

Before generating the APP password your google account must enable the 2FA. If the 2FA is enabled then you can hop on to the security section in the manage accounts and there you can see the APP password section.

For generating the APP password you also can read -> here

The below code I tried and it's working

I have used python another dependencies

pip install Flask-Mail

from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)
mail= Mail(app)

app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'someone@gmail.com'
app.config['MAIL_PASSWORD'] = 'YOUR_APP_PASSWORD'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)

@app.route("/")
def index():
   msg = Message('Hello', sender = 'someone@gmail.com', recipients = ['someone@gmail.com'])
   msg.body = "Hello Flask message sent from Flask-Mail"
   mail.send(msg)
   return "Sent"

if __name__ == '__main__':
   app.run(debug = True)
peerpressure
  • 390
  • 4
  • 19