1

message.send(connection) 2021-06-20T00:49:29.013482+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask_mail.py", line 427, in send 2021-06-20T00:49:29.013483+00:00 app[web.1]: connection.send(self) File self.host.sendmail(sanitize_address(envelope_from or message.sender), 2021-06-20T00:49:29.013484+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/smtplib.py", line 882, in sendmail 2021-06-20T00:49:29.013484+00:00 app[web.1]: raise SMTPSenderRefused(code, resp, from_addr) 2021-06-20T00:49:29.013486+00:00 app[web.1]: smtplib.SMTPSenderRefused: (530, b'5.7.0 Authentication Required. Learn more at\n5.7.0 https://support.google.com/mail/?p=WantAuthError y24sm8273493qtn.57 - gsmtp', 'xxx@gmail.com')

I have deployed my flask app to heroku and I am facing this email authentication error. On my local machine the email part is working perfectly. Only after deploying to heroku it is giving this error. I have checked all the email and password variables and also allowed less secure apps access. Can some one pls advise. I am using flask-mail

My code is

__init__.py
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('EMAIL_USER')
app.config['MAIL_PASSWORD'] = os.environ.get('EMAIL_PASS')

In routes.py
def send_reset_email(user):
    token=user.get_reset_token()
    msg=Message("Password  Reset Request",sender='xxx@gmail.com',recipients=[user.email])
    msg.body=f'''To reset your password visit the following link:
    {url_for('reset_password',token=token,_external=True)}
    If you did not make this request please ignore this message.
    '''
    mail.send(msg)
@app.route("/reset_request",methods=["GET","POST"])
def reset_request():
    form=ResetRequestForm()
    if form.validate_on_submit():
        user=User.query.filter_by(email=form.emailmain.data).first()
        if user is None:
            flash("No account exists with this email. Pls register first.",'danger')
        else:
            send_reset_email(user)
            flash("An email has been sent with instructions to reset your password",'success')
            time.sleep(2)
            return redirect(url_for('login'))

In models.py

def get_reset_token(self, expires_sec=1800):
        s = Serializer(app.config['SECRET_KEY'], expires_sec)
        return s.dumps({'user_id': self.id}).decode('utf-8')


    @staticmethod
    def verify_reset_token(token):
        s = Serializer(app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None

Can someone pls tell me what I am missing

anish
  • 27
  • 1
  • 5
  • 1
    First of all, use code formatted for your errors. Second it says authentication required , see your inbox there might google security mail. when you try to send mail – charchit Jun 20 '21 at 06:44
  • 1
    Did you add ```EMAIL_USER``` and ```EMAIL_PASS``` as environment variables in Heroku ? – Ram Jun 20 '21 at 08:24
  • No how to do that? – anish Jun 20 '21 at 12:21
  • @charchit it works locally so there is no issue i think with gmail ,there might be some configuration missing on heroku. Can there be some issue from that end – anish Jun 20 '21 at 12:22
  • @Ram I just added thosev variables to heroku. now its sayhing smtplibb authentication error, do i have to add all mail variables to heroku whichever were there in the python code:? – anish Jun 20 '21 at 12:30
  • @anish You are getting Authentication error because Google blocks sign-in attempt by apps for security reasons. Please go through this: https://stackoverflow.com/questions/26852128/smtpauthenticationerror-when-sending-mail-using-gmail-and-python – Ram Jun 20 '21 at 12:38

1 Answers1

0

Google doesn't allow you to sign in like that from heroku. You can do the following.

  1. Check your mails, there might be one telling about signin into your account.
  2. Go to security and enable enable less secure app.

Note -After doing so my app to worked, but one day it again gave the same error. I got this DisplayUnlockCaptcha link from a SO question. But I need to fo it whenever I send email. So finally I got this.

  1. Go to security enable 2 step verification and head to app passwords.
  2. Choose your device (I chose windows computer ) and in app select mail. App password will be generated. Replace your password with this password in heroku env variables.
charchit
  • 1,492
  • 2
  • 6
  • 17