Celery Flask-Mail异步失败。没有异步发送。 SMTPSenderRefused

时间:2015-12-28 21:56:19

标签: python asynchronous flask celery flask-mail

我刚开始与Celery合作完成我最新的工作项目;我在异步执行任务时遇到了一些麻烦。

所有代码均来自Miguel Grinbergs 'Using Celery with Flask'

在不执行任务的情况下发送邮件时,它发送完全正常。虽然当我尝试延迟发送邮件时,但它没有给出如下错误。

smtplib.SMTPSenderRefused: (530, b'5.5.1 Authentication Required. Learn more at\n5.5.1  https://support.google.com/mail/answer/14257 h19sm960819igq.6 - gsmtp', 'email-removed@gmail.com')

这是我在Flask应用中使用的代码。

import os
import time
from flask import Flask, request, render_template, session, flash, redirect, url_for, jsonify
from flask.ext.mail import Mail, Message
from celery import Celery

app = Flask(__name__)

app.config['SECRET_KEY'] = 'super-duper-secret'

# Celery Configuration
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'

# Configuration for Flask-Mail
app.config['MAIL_SERVER'] = "smtp.gmail.com"
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USERNAME'] = 'email-removed@gmail.com'
app.config['MAIL_PASSWORD'] = 'password-removed'
app.config['MAIL_DEFAULT_SENDER'] = 'email-removed@gmail.com'

celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

mail = Mail(app)


@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'GET':
        return render_template('index.html', email=session.get('email', ''))

    email = request.form['email']
    session['email'] = email

    msg = Message("Hello from Flask", recipients=[email])
    msg.body = "This is a test message from the flask application!"

    if request.form['submit'] == "Send":
        send_async_email(msg)
        flash('Sending email to {0}'.format(email))
    else:
        send_async_email.apply_async(args=[msg], countdown=20)
        flash('An email to {0} will be sent in a minute.'.format(email))

    return redirect(url_for('index'))


@celery.task()
def send_async_email(msg):
    with app.app_context():
        mail.send(msg)


if __name__ == "__main__":
    app.run(debug=True)

我很欣赏一些见解,也许是对如何使这项工作的解释,以及为什么它不起作用。

我还在这里查看了其他主题,并为我的Google帐户启用了不安全的应用访问权限,并按照错误返回网址中的建议清空了验证码。

0 个答案:

没有答案