Thu*_*yTM 2 python project flask web flask-mail
import os
import secrets
from PIL import Image
from flask import url_for, current_app
from flask_mail import Message
from app import mail
def save_picture(form_picture):
random_hex = secrets.token_hex(8)
f_name, f_ext = os.path.splitext(form_picture.filename)
picture_fn = random_hex + f_ext
picture_path = os.path.join(current_app.root_path, 'static/profile_pics', picture_fn)
output_size = (125, 125)
i = Image.open(form_picture)
i.thumbnail(output_size)
i.save(picture_path)
return picture_fn
def send_reset_email(user):
token = user.get_reset_token()
msg = Message('Password Reset Request', sender='noreplies@diemo.hr', recipients=[user.email])
msg.body = f''' To reset your password, visit the following link:
{url_for('users.reset_token', token=token, **_external=True**)}
If you did not make this request then simply ignore this email and no changes will be made
'''
mail.send(msg)
Run Code Online (Sandbox Code Playgroud)
我正在制作一个 Flask 应用程序并学习如何使用 Flask_mail 模块发送重置电子邮件,所以我想知道当我将重置链接放在消息正文上时 _external=True(send_reset_email function) 意味着什么。我在谷歌上搜索过,但没有找到任何东西。先感谢您。
_external=True告诉 Flask 它应该生成一个绝对 URL,而不是相对 URL。例如,https://example.com/my-page是绝对URL,但/my-page是相对URL。由于您要发送电子邮件,因此您网站中页面的相对 URL 将不起作用。
您可以在此处查看文档url_for: https: //flask.palletsprojects.com/en/1.1.x/api/#flask.url_for
\n\n_external \xe2\x80\x93 如果设置为 True,则会生成绝对 URL。服务器地址可以通过 SERVER_NAME 配置变量进行更改,该变量会回退到主机标头,然后回退到请求的 IP 和端口。
\n
| 归档时间: |
|
| 查看次数: |
3209 次 |
| 最近记录: |