Sending a mail from Flask-Mail (SMTPSenderRefused 530)

Anuj picture Anuj · Jan 29, 2015 · Viewed 8.7k times · Source

The app configuration used in a Flask Mail application (following Miguel Grinberg Flask developlemt book) :

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')

The Mail Username and Password variables have been set correctly and rechecked. While trying to send a message using the following code,

from flask.ext.mail import Message
from hello import mail
msg = Message('test subject', sender='same as MAIL_USERNAME', recipients=['[email protected]'])
msg.body = 'text body'
msg.html = '<b>HTML</b> body'
with app.app_context():
    mail.send(msg)

While sending, the application is again and again resulting in the following error:

SMTPSenderRefused: (530, '5.5.1 Authentication Required. Learn more at\n5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qb10sm6828974pbb.9 - gsmtp', u'configured MAIL_USERNAME')

Any workaround for the error?

Answer

Anuj picture Anuj · Jan 29, 2015

While digging into the issues faced, I rechecked the SMTP settings for Google,

Google SMTP settings

Changing the

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'

to

app.config['MAIL_SERVER'] = 'smtp.gmail.com'

did the trick.

Also make sure that the full username is used as Gmail SMTP username, i.e., [email protected] as shown in the image above.

Hope this helps!!!