'flask mail raise exception when there is error with authentication
First of all, I would like to thank everyone.
I have a quick question with flask mail.
I would like to have an exception or catch the error while sending an email.
from flask import Flask, request
from flask_mail import Mail, Message
import os, sys
app = Flask(__name__)
mail= Mail(app)
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_DEBUG'] = True
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = 'njhkjkjj'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)
@app.route('/email')
def get_email():
if mail.connect() == None:
print("Email setup/password is incorrect. Input your email creadentials in python app..")
else:
msg = Message('visitor blocked', sender = '[email protected]', recipients = ['[email protected]'])
msg.body = "Hello, this is email test."
mail.send(msg)
print("Email setup is ready. Email is sent..")
I am trying to get an error and raise an error message with the above code of block where the password is incorrect. But it ends up with an exception.
smtplib.SMTPAuthenticationError
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 o23sm485224lfr.302 - gsmtp')
Any help or suggestion on what can be improved would be helpful. Thank you.
Solution 1:[1]
Did you enable "Access to less secure applications" from your google account? Go to your google account settings, Security > Access to less secure applications. You have to enable it. It worked for me, your flask app should work then.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mart1 |