我有一个 Flask 服务器,我正在使用 Flask-mail。通过pip安装后,我这样配置文件flask_server.py
:
from flask_mail import Mail, Message
app = flask.Flask(__name__, static_folder='static')
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'myemail@gmail.com'
app.config['MAIL_PASSWORD'] = 'mypw'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail= Mail(app)
总是在 flask_server.py
我有这个发送邮件的功能:
def send_email():
msg = Message('Title', sender = 'myemail@gmail.com', recipients = ['target@gmail.com'])
msg.body = "Hello"
mail.send(msg)
如果我尝试在一个 API 中发送邮件,它会起作用。例如:
@app.route('/computeModels', methods=['GET', 'POST'])
def compute_models():
send_email()
现在我想删除调用 send_email()
并将其放在另一个脚本的函数中。
在models.py
我是这样工作的:
import flask_server as fs
def try_mail():
fs.send_email()
函数像第一种情况一样执行,但电子邮件没有到达。