I'm exploring flask and attempting to setup a simple web app with secure registration and sign in. I'm using flask-security to do this. Unfortunately, when I navigate to the send confirmation page I'm getting the error: "smtpserverdisconnected: please run connect() first".
Below are the relevant files I've written. run.py
drives the entire application.
run.py (this is next to the app
folder)
#!venv/bin/python
from app import app
app.run(debug = True)
Everything below here is in the app
folder
__init__.py
from flask import Flask
from flask.ext.mail import Mail
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
mail = Mail(app)
import models
import views
@app.before_first_request
def create_user():
db.create_all()
models.user_datastore.create_user(email = '[email protected]',
password = 'password')
db.session.commit()
if __name__ == '__main__':
app.run()
models.py
from app import db, app
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.security import SQLAlchemyUserDatastore,\
UserMixin, RoleMixin, Security
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key = True)
name = db.Column(db.String(80), unique = True)
description = db.Column(db.String(255))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key = True)
email = db.Column(db.String(255), unique = True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary = roles_users,
backref = db.backref('users', lazy = 'dynamic'))
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
views.py
from app import app
from flask import render_template
from flask.ext.security import login_required
@app.route('/')
@login_required
def index():
return render_template('index.html')
edit: also, here is the config file I'm using
DEBUG = True
SECRET_KEY = 'secret'
SQLALCHEMY_DATABASE_URI = 'sqlite://'
SECURITY_PASSWORD_HASH = 'sha512_crypt'
SECURITY_PASSWORD_SALT = 'salt'
SECURITY_CONFIRMABLE = True
SECURITY_REGISTERABLE = True
SECURITY_RECOVERABLE = True
SECURITY_TRACKABLE = True
SECURITY_CHANGEABLE = True
MAIL_SERVER = 'smtp.zoho.com'
MAIL_PORT = 465
MAIL_USE_TLS = False
MAIL_USE_SSL = True
MAIL_DEBUG = True
MAIL_USERNAME = 'myaddress@mydomain'
MAIL_PASSWORD = 'password'
Alright, so I figured out what the problem was. By default flask-security is set up to send mail as "localhost". My mail provider is Zoho, but I'm just using them as a mail server for a domain I run. My mail settings are such that I can only send mail from certain addresses. Because 'localhost' is not one of these flask-security was not able to connect to Zoho's servers.
So the solution was to add to my config file the line
SECURITY_EMAIL_SENDER = 'valid_email@my_domain.com'
Hopefully this will save someone else some time trying to figure out why flask-security's email isn't working.