我有一个关于蓝图结构的问题
我的烧瓶结构看起来像
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>'
)
我不知道为什么我必须做两次