Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a mail from Flask-Mail (SMTPSenderRefused 530)

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?

like image 918
Anuj Avatar asked Jan 29 '15 07:01

Anuj


People also ask

What does flask mail do?

flask-mail¶ One of the most basic functions in a web application is the ability to send emails to your users. The Flask-Mail extension provides a simple interface to set up SMTP with your Flask application and to send messages from your views and scripts.


2 Answers

I also followed this book and get the same problem, after some digging here and there, I found out the root cause of the problem. However, I am not sure whether it will be the same case for you or not.

app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')

As you can see your flask app gets the your email credentials through os.environ.get(), and if you set this environment variables temporarily in your system, in my case Mac OSX, after your terminal session they will be gone, so you need to set them again for the next time you enter the terminal, like below:

export MAIL_USERNAME=**YOUR EMAIL**
export PASSWORD=**YOUR PASSWORD**

I got this error because of this scenario, in order to set them permanently you need to include these variables into .bash_profile file in your home directory.

like image 116
Humoyun Ahmad Avatar answered Oct 14 '22 05:10

Humoyun Ahmad


Do these 2 things to resolve:

  1. Use this link and turn on 'Allow less secure apps'- https://myaccount.google.com/lesssecureapps

  2. Use hard-coded value for email and password and it worked fine. Simply in the file 'init.py', edit the following section:

Don't use os.environ.get

app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = 'yourpassword'
like image 26
Davies Tonui Avatar answered Oct 14 '22 03:10

Davies Tonui