Configure Flask-Mail to use GMail

When I try to send an email using Flask-Mail to the Gmail SMTP server using the settings below, I get [Errno -2] Name or service not known . How do I fix my configuration for sending emails using Gmail?

 from flask import Flask, render_template, redirect, url_for from flask_mail import Mail, Message app = Flask(__name__) app.config.update( MAIL_SERVER=' smtp@gmail.com ', MAIL_PORT=587, MAIL_USE_SSL=True, MAIL_USERNAME = 'ri****** a@gmail.com ', MAIL_PASSWORD = 'Ma*****fe' ) mail = Mail(app) @app.route('/send-mail/') def send_mail(): msg = mail.send_message( 'Send Mail tutorial!', sender='ri****** a@gmail.com ', recipients=['ri********* 07@msn.com '], body="Congratulations you've succeeded!" ) return 'Mail sent' 
+9
source share
3 answers
  • Server "smtp.gmail.com".
  • The port must match the type of security used.
    • If you use STARTTLS with MAIL_USE_TLS = True , use MAIL_PORT = 587 .
    • If you use SSL / TLS directly with MAIL_USE_SSL = True , use MAIL_PORT = 465 .
    • Enable STARTTLS or SSL / TLS, not both.
  • Depending on the security settings of your Google account, you may need to generate and use
+13
source

A small but important addition to Davidism:

You must have "2-step verification" enabled in your Google account before you can configure application passwords.

+8
source

smtplib.SMTPSenderRefused: (530, b'5.5.1 Authentication required. Read more in \ n5.5.1 https://support.google.com/mail/?p=WantAuthError s79sm14668833pgs.50 - gsmtp ',' noreply @demo. com ")

0
source

All Articles