flaskflask-mail

Why am I facing flask-mail issues?


I am not sure what should I do

from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)

with app.app_context():
    app.config['MAIL_SERVER'] = 'smtp.gmail.com'
    app.config['MAIL_PORT'] = 587
    app.config['MAIL_DEFAULT_SENDER'] = 'sender'
    app.config['MAIL_USERNAME'] = 'sender'
    app.config['MAIL_PASSWORD'] = '*************'
    app.config['MAIL_USE_TSL'] = True
    mail = Mail(app)
    mail.send_message('Mail sent', sender = 'sender', recipients = ['recipients'], body = 'Mail sent successfully')

It tells me smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server I've even enabled emails from less secure apps in gmail. Why does it not seem to work? I've tried looking the error up and haven't found much of a satisfactory answer yet.

Edit: I used SSL instead of TLS for the encryption and it seems to show me a different error. Now it tells me smtplib.SMTPSenderRefused: (530, b'5.7.0 Authentication Required. Learn more at\n5.7.0 https://support.google.com/mail/?p=WantAuthError I have no idea what to do next. I don't know how to enable DisplayUnlockCaptcha. https://accounts.google.com/DisplayUnlockCaptcha is the link for it. I proceeded with what the link said, it told "Account access enabled Please try signing in to your Google Account again from your new device or application." and I retried, still fail to sign in.

Issue has been fixed!


Solution

  • There are several ways you can try, such as activating ssl

    app.config['MAIL_PORT'] = 465
    app.config['MAIL_USE_TLS'] = False
    app.config['MAIL_USE_SSL'] = True
    

    You must not enable both tls and ssl together

    You need to apply a series of settings to your Gmail account, such as disabling 2FA or Display Unlock Captcha, etc.

    The steps of the work are outlined in this tutorial.

    https://twilio.com/blog/2018/03/send-email-programmatically-with-gmail-python-and-flask.html