15

I'm getting the following error when I attempt to use flask-mail to send an email through my gmail account.

error: [Errno 10061] No connection could be made because the target machine actively refused it

I've tried configuring flask-mail in various ways, but so far I always get this error.

Here are some sample configurations I've tried:

  1. app = Flask(__name__)
    mail = Mail(app)
    
    app.config.update(dict(
        DEBUG = True,
        MAIL_SERVER = 'smtp.gmail.com',
        MAIL_PORT = 465,
        MAIL_USE_TLS = False,
        MAIL_USE_SSL = True,
        MAIL_USERNAME = 'my_username@gmail.com',
        MAIL_PASSWORD = 'my_password',
    ))
    
  2. app = Flask(__name__)
    mail = Mail(app)
    
    app.config.update(dict(
        DEBUG = True,
        MAIL_SERVER = 'smtp.gmail.com',
        MAIL_PORT = 587,
        MAIL_USE_TLS = True,
        MAIL_USE_SSL = False,
        MAIL_USERNAME = 'my_username@gmail.com',
        MAIL_PASSWORD = 'my_password',
    ))
    
  3. This configuration is from the flask mega-tutorial (http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xi-email-support)

    app = Flask(__name__)
    mail = Mail(app)
    
    app.config.update(dict(
        DEBUG = True,
        # email server
        MAIL_SERVER = 'smtp.googlemail.com',
        MAIL_PORT = 465,
        MAIL_USE_TLS = False,
        MAIL_USE_SSL = True,
        MAIL_USERNAME = 'my_username',
        MAIL_PASSWORD = 'my_password',
    
        # administrator list
        ADMINS = ['my_username@gmail.com']
    ))
    

Has anyone else experienced a similar problem?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Brett
  • 155
  • 1
  • 1
  • 5

3 Answers3

48

As far as I can tell there is nothing wrong with this configuration. The only problem is that your application is not using it. You should update configuration before you initialize Mail:

app = Flask(__name__)

app.config.update(dict(
    DEBUG = True,
    MAIL_SERVER = 'smtp.gmail.com',
    MAIL_PORT = 587,
    MAIL_USE_TLS = True,
    MAIL_USE_SSL = False,
    MAIL_USERNAME = 'my_username@gmail.com',
    MAIL_PASSWORD = 'my_password',
))

mail = Mail(app)
zero323
  • 322,348
  • 103
  • 959
  • 935
  • 3
    This worked, thanks! It seems simple and logical now. I think I was misled by the flask-mail docs which show Mail being instantiated without any prior configuration (http://pythonhosted.org/flask-mail/) and also this snippet (http://flask.pocoo.org/snippets/85/) which I didn't notice actually instantiates mail twice! – Brett Sep 18 '13 at 21:18
  • 2
    Now it gives... something like smtplib.SMTPAuthenticationError SMTPAuthenticationError: (534, '5.7.14 Please log in via your web browser and then try again.\n5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787\n5.7.14 54 q44sm5794504eez.1 - gsmtp') – OWADVL Mar 13 '14 at 08:55
  • If you are sure you provided correct credentials it is most likely related to high traffic or some kind o suspicious activity and is not programming related. – zero323 Mar 13 '14 at 16:34
3

In addition to zero323's answer, adding the configuration before creating a Mail object should help, but if it gives an SMTPAuthentication error with a gmail server, then just for testing purpose one may allow less secure apps to login for a while - https://myaccount.google.com/security#signin

user2725012
  • 61
  • 1
  • 5
1
from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'youremail@gmail.com'
app.config['MAIL_PASSWORD'] = 'your_email_password'

mail = Mail(app)

@app.route('/')
def home():
    msg = Message('mail title', sender='sender of the email', recipients=['recipient2gmail.com'])
    msg.body = 'Body of the email to send'
    return 'Mail Sent...'


if __name__ == '__main__':
    app.run()

Running the with the right configurations should enable send an email. Notice the mail is initialized after the configurations have been sent.

In case of any other errors, make sure your google account allows access from less secure applications.