0

I am trying to use flask_mail to send emails with Gmail but for some reason, my emails won't go through. I successfully manage to login to my Gmail account just to ensure that my password is correct.

The function looks like the following:

def send_reset_email(user):
    token = user.get_reset_token()

    msg = Message('Password reset request', sender='myemail@gmail.com', recipients=['anotheremail@example.com'])
    msg.body = f'''To reset your password visit the following link:
{url_for('reset_token', token=token, _external=True)}

If you did not make this request simply ignore this email and no changes will be made
    '''
    mail.send(msg)

Inside my init.py I have the following configurations for flask mail:

from flask_mail import Mail

######## EMAIL CONFIURATION ##########
app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_POST'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'myemail@gmail.com'
app.config['MAIL_PASSWORD'] = 'mygmailpassword'

mail = Mail(app)

When I press the button to send the email I see the following 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 v13sm322058edy.8 - gsmtp')

Note: I already went to my Gmail settings and disabled less secure apps

How do I use flask-mail to send emails?

EDITED: The answer of this link does not answer my question because I still see the same error. I changed my mail server to smtp.gmail.com but nothing.

Sidney Sousa
  • 3,378
  • 11
  • 48
  • 99
  • Does this answer your question? [Configure Flask-Mail to use GMail](https://stackoverflow.com/questions/37058567/configure-flask-mail-to-use-gmail) – Chris Doyle Mar 29 '20 at 16:37
  • Infact the above question i linked says to use `stmp.gmail.com` and not `smtp.googlemail.com` However in a very similar question you asked in July last year your code then actually used the correct mail server. `'host' => env('MAIL_HOST', 'smtp.gmail.com'),` – Chris Doyle Mar 29 '20 at 16:40
  • @ChrisDoyle it shouldnt make a difference whether its googlemail or Gmail. And yes, it is exactly because I know how to setup smtp credentials that I posted the question, which is using flask_mail, not PHP. – Sidney Sousa Mar 29 '20 at 16:43
  • https://support.google.com/mail/answer/7126229?hl=en-GB google would disagree, they specifically state in their docs the mail host is smtp.gmail.com – Chris Doyle Mar 29 '20 at 16:46
  • @ChrisDoyle I have already changed to gmail and it doesn't work. But I am more looking for an answer to the question. Do you have an answer for how to send an email with Gmail in flask_email? – Sidney Sousa Mar 29 '20 at 16:48

1 Answers1

0

try to take as MAIL_USERNAME the part of your Gmail address before @gmail.com is your Google account username!!

app.config['MAIL_USERNAME'] = 'myemail'
mguerrou
  • 11
  • 2
  • Have you had success with this approach previously? Just guessing at solutions is wasting other people's time. Google's docs for GMail SMTP says to use "Your full email address" for username. – totokaka Feb 14 '22 at 11:06