Question

I'm having trouble sending email with flask-mail ( http://pythonhosted.org/flask-mail/ )

from flask.ext.sqlalchemy import SQLAlchemy
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from flask.ext.mail import Mail, Message
import os

# configuration
DEBUG = True
SECRET_KEY = 'hidden'
USERNAME = 'secret'
PASSWORD = 'secret'

MAIL_SERVER='smtp.gmail.com'
MAIL_PORT=587
MAIL_USE_TLS = False
MAIL_USE_SSL= True
MAIL_USERNAME = 'user@gmail.com'
MAIL_PASSWORD = 'password'

app = Flask(__name__)
mail = Mail(app)

@app.route('/minfo')
def send_mail():
    msg = Message(
      'Hello',
       sender='user@gmail.com',
       recipients=
       ['user@gmail.com.com'])
    msg.body = "This is the email body"
    mail.send(msg)
    return "Sent"

When I go to /minfo I get

12:25:57 web.1  |     return socket.create_connection((port, host), timeout)
12:25:57 web.1  |   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
12:25:57 web.1  |     raise err
12:25:57 web.1  | error: [Errno 61] Connection refused

I have no idea what is breaking or how to fix it, been googling for hours. Has someone experienced this?

Was it helpful?

Solution

  1. You must set up flask config:

    app = Flask(__name__)
    app.config.from_object(__name__)
    mail = Mail(app)
    
  2. Use 465 port.

OTHER TIPS

MAIL_USE_TLS = True then use 587 port MAIL_USE_SSL = True then use 465 port

I also have the same issue and I was also using Flask-Mail. It was actually part of Flask-User package. I have also enabled TLS port, since my mail server was using TLS and I disable SSL. I also change the port 465 to 587. The code of my application is follows:

 MAIL_PORT =           int(os.getenv('MAIL_PORT',            '587'))
 MAIL_USE_SSL =        int(os.getenv('MAIL_USE_SSL',         False))
 MAIL_USE_TLS =        int(os.getenv('MAIL_USE_TLS',         True))

This change solved my problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top