首页 > 解决方案 > 如何使用 href url 传递变量?

问题描述

下面是我在 python 中使用烧瓶发送电子邮件的代码。

def sendPasswordResetLink(email, token):
    message = Message()
    message.subject = "Reset your password"
    message.sender = "********@gmail.com"
    message.recipients = email.split()
    message.html = '<p>Hello there,</p>\n' \
                   '<p>Please click on the below link to reset your password</p>\n' \
                   '<a href=http://localhost:5002/resetpassword.html?token= +token+>'
    mail.send(message)

线路有问题<a href....。它没有在我收到的邮件中打印任何内容。我使用烧瓶邮件扩展。有人可以快速解决这个问题。正如我所说,我正在使用 Flask-mail 扩展,它提供了一个简单的界面来使用我们的 Flask 应用程序设置 SMTP 并从我们的视图和脚本发送消息。

预期行为 - 它应该向我的 gmail 发送主题为“重设密码”的邮件,并且在邮件正文中我应该在那里打招呼,请点击下面的链接重设密码。 http://localhost:5002/resetpassword.html?token=2

令牌是我与函数定义一起发送的参数。它包含请求重置密码的用户的用户标识。但是 URL 没有打印在我收到的邮件中。

标签: pythonhtmlflask-mail

解决方案


def sendPasswordResetLink(email, token):
    message = Message()
    message.subject = "Reset your password"
    message.sender = "********@gmail.com"
    message.recipients = email.split()
    message.html = '<p>Hello there,</p>\n' \
                   '<p>Please click on the below link to reset your password</p>\n' \
                   '<a href="http://localhost:5002/resetpassword.html?token=' + token + '">Some message here</a>'
    mail.send(message)

您的问题是您没有逃避'...'字符串,因此永远不会添加token到组合中。但是您也没有<a></a>. 那里没有消息。这里有一些例子


推荐阅读