2

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

我的烧瓶结构看起来像

app/
    main/
        __init__.py
        mail.py
    __init.py
manage.py
config.py

我在 __init__.py 中注册了蓝图,用于app/__init__.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

把配置放进去config.py

class Config():
    MAIL_SERVER='<My e-mail SMTP>'
    MAIL_DEFAULT_SENDER=('TOPIC', 'mailID')
    MAIL_USERNAME='<EMail>'
    MAIL_PASSWORD='<EMail Password>'

当我在中编写这样的代码时,它返回 smtplib.SMTPSenderRefused 错误app/main/mail.py

@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/__init__.py,但是为什么我找不到 MAIL_SERVER 和相关配置app/main/mail.py

但是如果我再次重新加载配置app/main/mail.py,它会成功发送邮件

app.config.update(
    MAIL_SERVER='<SMTP>',
    MAIL_DEFAULT_SENDER=('<TOPIC>', 'mailID'),
    MAIL_USERNAME='<email>',
    MAIL_PASSWORD='<PASSWORD>'
)

我不知道为什么我必须做两次

4

3 回答 3

2

以下代码是用于发送邮件正文的呈现 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)

正如您在上面提到的代码。我已经两次初始化 Mail 对象,这是不必要的。

@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'
于 2020-03-02T13:22:15.630 回答
2

你应该像这样使用 app_context() :

from flask import current_app
from flask_mail import Message, Mail

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

更多信息https://flask.palletsprojects.com/en/1.1.x/extensiondev/

于 2020-03-02T10:33:39.223 回答
1

不是您问题的直接答案,但实际上我认为您不需要两次初始化邮件。

如果您要在 中初始化邮件app/main/mail.py,那么 in 毫无意义,mail.init_app(app)因为app/__init__.py您从未导入过它。

否则,app/main/mail.pyimport app.mail不会创建另一封邮件,这样您就不会首先遇到此配置问题。

于 2018-06-08T18:33:19.197 回答