Ok, I'm using Miguel Grinberg's Flask Mega Tutorial (which is awesome by the way!) as a basis for this. If I run this in the python interpreter it works fine, but when I submit the form in the web app, I get no error messages, but the message does not send. Code of my app.py below. Thanks in advance!
from flask import Flask, render_template, request, flash
from forms import ContactForm
from flask_mail import Message, Mail
from packages.decorators import async
app = Flask(__name__)
app.secret_key = 'dummy_key'
app.config["MAIL SERVER"] = "mail.domainname.com"
app.config["MAIL_SUPPRESS_SEND"] = False
app.config["TESTING"] = False
app.config["MAIL_PORT"] = 26
app.config["MAIL_USE_TLS"] = False
app.config["MAIL_USE_SSL"] = False
app.config["MAIL_USERNAME"] = '[email protected]'
app.config["MAIL_PASSWORD"] = 'password'
mail = Mail(app)
@app.route('/')
def index():
return render_template("home.html")
@app.route('/<string:page_name>')
def static_page(page_name):
return render_template('%s.html' % page_name)
@app.route('/contact', methods=['GET', 'POST'])
def contact():
form=ContactForm()
if request.method == 'POST':
if form.validate() == False:
flash('All fields are required.')
return render_template('contact.html', form=form)
else:
msg = Message(form.subject.data, sender='[email protected]', recipients=['[email protected]'])
msg.body = """
From: %s %s <%s>
%s
""" % (form.firstName.data, form.lastName.data, form.email.data, form.comment.data)
@async
def send_async_email(app, msg):
with app.app_context():
mail.send(msg)
send_async_email(app, msg)
return 'Form posted.'
elif request.method == 'GET':
return render_template('contact.html', form=form)
if __name__ == '__main__':
app.run()
So after I submit the form I see "form posted", but never receive an email. The only thing I do differently in the interpreter is I manually fill out the msg variable with strings instead of receiving input from the form fields.
Edit: below is what is in packages.decorators
from threading import Thread
def async(f):
def wrapper(*args, **kwargs):
thr = Thread(target=f, args=args, kwargs=kwargs)
thr.start()
return wrapper