使用flask+发送邮件;蓝图

使用flask+发送邮件;蓝图,flask,flask-mail,Flask,Flask Mail,我有一个关于蓝图结构的问题 我的烧瓶结构看起来像 app/ main/ __init__.py mail.py __init.py manage.py config.py 我在app/\uuuuuuu init\uuuuuuuuuuuuuuuuuuupy.py中注册蓝图 from flask_mail import Mail from flask import Flask mail = Mail() def create_app(config_

我有一个关于蓝图结构的问题

我的烧瓶结构看起来像

app/
    main/
        __init__.py
        mail.py
    __init.py
manage.py
config.py
我在app/\uuuuuuu init\uuuuuuuuuuuuuuuuuuupy.py中注册蓝图

from flask_mail import Mail
from flask import Flask

mail = Mail()
def create_app(config_name='develop'):
    app = Flask(__name__)

    from config import cfg      # load EMAIL config from config.py
    app.config.from_object(cfg[config_name])
    cfg[config_name].init_app(app)

    from .main import main      # register blueprint
    app.register_blueprint(main)

    mail.init_app(app)          # load related mail config???

    return app
class Config():
    MAIL_SERVER='<My e-mail SMTP>'
    MAIL_DEFAULT_SENDER=('TOPIC', 'mailID')
    MAIL_USERNAME='<EMail>'
    MAIL_PASSWORD='<EMail Password>'
@main.route('/mail')
def sendmail():
    receivers = ['<Receiver 1>', '<Receiver 2>'] 
    app = current_app._get_current_object() # <class 'flask.app.Flask>
    mail = Mail(app)
    with mail.connect() as conn:
        for receiver in receivers:
            msg = Message(
                        subject='subject test',
                        recipients=[receiver],
                        html='<h1>Hi~~</h1>')
            mail.send(msg)
    return 'ok'
将配置放入
config.py

from flask_mail import Mail
from flask import Flask

mail = Mail()
def create_app(config_name='develop'):
    app = Flask(__name__)

    from config import cfg      # load EMAIL config from config.py
    app.config.from_object(cfg[config_name])
    cfg[config_name].init_app(app)

    from .main import main      # register blueprint
    app.register_blueprint(main)

    mail.init_app(app)          # load related mail config???

    return app
class Config():
    MAIL_SERVER='<My e-mail SMTP>'
    MAIL_DEFAULT_SENDER=('TOPIC', 'mailID')
    MAIL_USERNAME='<EMail>'
    MAIL_PASSWORD='<EMail Password>'
@main.route('/mail')
def sendmail():
    receivers = ['<Receiver 1>', '<Receiver 2>'] 
    app = current_app._get_current_object() # <class 'flask.app.Flask>
    mail = Mail(app)
    with mail.connect() as conn:
        for receiver in receivers:
            msg = Message(
                        subject='subject test',
                        recipients=[receiver],
                        html='<h1>Hi~~</h1>')
            mail.send(msg)
    return 'ok'
它引发了553错误

smtplib.SMTPSenderRefused: (553, b'Domain name required.', '=?utf-8?q?<TOPIC>?= <mailID>')

我不知道为什么我要做两次

这不是对你问题的直接回答,但实际上我认为你不需要两次初始化邮件

如果您想在
app/main/mail.py
中初始化邮件,那么在
app/\uuuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

否则,在
app/main/mail.py
中,我将在不创建其他邮件的情况下导入app.mail
,这样您就不会出现此配置问题。

您应该像这样使用app\u context()

from flask import current_app
from flask_mail import Message, Mail

with current_app.app_context():
    mail = Mail()
    mail.send(msg)

更多信息

以下代码是用于发送邮件正文的呈现HTML模板,包括样式或格式化HTML

from <project-module> import mail

token = user.get_reset_token()
msg = Message('Password Reset Request', sender='<sender-mail>', recipients=[user.email])
msg.html = render_template('email_templates/password_reset.html',
                               home_link=url_for('home.index', _external=True),
                               reset_link=url_for(
                                   'account.reset_token', token=token, _external=True),
                               name=user.name,
                               email=user.email)
mail.send(msg)
从导入邮件
令牌=用户。获取\u重置\u令牌()
msg=Message('密码重置请求',发件人='',收件人=[user.email])
msg.html=render_template('email_templates/password_reset.html',
home\u link=url\u for('home.index',_external=True),
重置链接=的url\u(
“account.reset_token”,token=token,_external=True),
name=user.name,
email=user.email)
邮件发送(msg)
正如您上面提到的代码。我已经两次初始化邮件对象,这是不必要的

@main.route('/mail')
def sendmail():
    receivers = ['<Receiver 1>', '<Receiver 2>'] 
    mail = Mail()
    with mail.connect() as conn:
        for receiver in receivers:
            msg = Message(
                        subject='subject test',
                        recipients=[receiver],
                        html='<h1>Hi~~</h1>')
            mail.send(msg)
    return 'ok'
@main.route(“/mail”)
def sendmail():
接收者=['',]
mail=mail()
使用mail.connect()作为conn:
对于接收器中的接收器:
消息(
受试者=“受试者测试”,
收件人=[receiver],
html='Hi~~')
邮件发送(msg)
返回“ok”