我将如何在 hyper link 中传递诸如用户 ID 之类的参数?
How will I pass parameters such as, user id in hyper link?
我有一个 HTML 页面,其中有一个超链接。这 html 封邮件将通过 outlook 发送给用户(我使用 flask python 编写了邮件功能),当用户点击邮件正文上的超链接时,它最终会打开另一个页面。此页面将相同,但根据用户的电子邮件 ID,页面内容将因用户而异。
现在,我的要求是通过超链接传递用户的电子邮件ID,这样我就可以根据电子邮件ID显示不同的内容。可以通过超链接完成吗?因为,你知道outlook使用Microsoft Word作为渲染引擎,所以通过超链接传递参数会很困难吗?
或者,我可以在发送邮件时通过我的 flask 函数传递电子邮件 ID 吗?
我将邮件发送到 outlook 的 flask 函数如下
from flask import Flask, render_template
from flask_mail import Mail, Message
app = Flask(__name__)
app.config.update(
DEBUG=True,
MAIL_SERVER='My Company SMTP MAIL SERVER',
MAIL_PORT=My Company SMTP PORT NUMBER,
# MAIL_USE_SSL=True,
MAIL_USERNAME='XXXXX.YYYY@mycompanyname.com',
)
mail = Mail(app)
@app.route('/')
def mailSend():
try:
recipeint_emails = fetch_recipient_emails
msg = Message("Send Mail Tutorial!",
sender="XXXXX.YYYY@mycompanyname.com",
recipients=recipeint_emails)
msg.html = render_template('linkPage.html')
mail.send(msg)
return 'Mail sent!'
except Exception as e:
print(type(e))
print(e)
return 'error'
linkPage.html 将包含下面提到的超链接
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hyperlinkdemo</title>
</head>
<body>
<a href="https://hyperlinkflask.azurewebsites.net/helloworld" target="_blank">Visit Dynamic Page</a>
</body>
</html>
任何建议都会很有帮助。
Flask
已经有一个内置函数 url_for
可以正确生成带有额外参数的 link。参考这个doc
更新
- 建议选择准确的路线名称
- 建议在命名视图时使用snake_case
- 推荐你参考官方
Flask-Mail
doc部分Bulk Mail
@app.route('/bulk-email')
def bulk_mail():
[..]
# Get all users first
with mail.connect() as conn:
for user in users:
msg = Message(subject="Tutorial",
sender="XXXXX.YYYY@mycompanyname.com",
recipients=[user.email])
# pass dynamically the user to the template
msg.html = render_template('linkPpage.html', user=user)
conn.send(msg)
在linkPage.html
模板中你可以做
<p>Dear {{ user.username }},</p>
<p>
<a href = "{{ url_for('link_tutorial', user_id=user.id, _external=True) }}">Open Link tutorial</a>
</p> //added double quotation
您必须实现 link_tutorial
功能的逻辑,当用户点击 link 时,它将被重定向到您的应用以向他显示自定义页面/教程:
@app.route('/link-tutorial/<int:user_id>')
def link_tutorial(user_id):
# fetch the user with the given user_id and render the right template for him.
[..]
return render_template('tutorial.html')
最后,我建议您使用 celery
异步任务队列来比 Flask-Mail
更有效地处理群发电子邮件,因为发送邮件是一项阻塞任务,您的应用程序会非常慢并且没有反应。
我有一个 HTML 页面,其中有一个超链接。这 html 封邮件将通过 outlook 发送给用户(我使用 flask python 编写了邮件功能),当用户点击邮件正文上的超链接时,它最终会打开另一个页面。此页面将相同,但根据用户的电子邮件 ID,页面内容将因用户而异。
现在,我的要求是通过超链接传递用户的电子邮件ID,这样我就可以根据电子邮件ID显示不同的内容。可以通过超链接完成吗?因为,你知道outlook使用Microsoft Word作为渲染引擎,所以通过超链接传递参数会很困难吗?
或者,我可以在发送邮件时通过我的 flask 函数传递电子邮件 ID 吗?
我将邮件发送到 outlook 的 flask 函数如下
from flask import Flask, render_template
from flask_mail import Mail, Message
app = Flask(__name__)
app.config.update(
DEBUG=True,
MAIL_SERVER='My Company SMTP MAIL SERVER',
MAIL_PORT=My Company SMTP PORT NUMBER,
# MAIL_USE_SSL=True,
MAIL_USERNAME='XXXXX.YYYY@mycompanyname.com',
)
mail = Mail(app)
@app.route('/')
def mailSend():
try:
recipeint_emails = fetch_recipient_emails
msg = Message("Send Mail Tutorial!",
sender="XXXXX.YYYY@mycompanyname.com",
recipients=recipeint_emails)
msg.html = render_template('linkPage.html')
mail.send(msg)
return 'Mail sent!'
except Exception as e:
print(type(e))
print(e)
return 'error'
linkPage.html 将包含下面提到的超链接
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hyperlinkdemo</title>
</head>
<body>
<a href="https://hyperlinkflask.azurewebsites.net/helloworld" target="_blank">Visit Dynamic Page</a>
</body>
</html>
任何建议都会很有帮助。
Flask
已经有一个内置函数 url_for
可以正确生成带有额外参数的 link。参考这个doc
更新
- 建议选择准确的路线名称
- 建议在命名视图时使用snake_case
- 推荐你参考官方
Flask-Mail
doc部分Bulk Mail
@app.route('/bulk-email')
def bulk_mail():
[..]
# Get all users first
with mail.connect() as conn:
for user in users:
msg = Message(subject="Tutorial",
sender="XXXXX.YYYY@mycompanyname.com",
recipients=[user.email])
# pass dynamically the user to the template
msg.html = render_template('linkPpage.html', user=user)
conn.send(msg)
在linkPage.html
模板中你可以做
<p>Dear {{ user.username }},</p>
<p>
<a href = "{{ url_for('link_tutorial', user_id=user.id, _external=True) }}">Open Link tutorial</a>
</p> //added double quotation
您必须实现 link_tutorial
功能的逻辑,当用户点击 link 时,它将被重定向到您的应用以向他显示自定义页面/教程:
@app.route('/link-tutorial/<int:user_id>')
def link_tutorial(user_id):
# fetch the user with the given user_id and render the right template for him.
[..]
return render_template('tutorial.html')
最后,我建议您使用 celery
异步任务队列来比 Flask-Mail
更有效地处理群发电子邮件,因为发送邮件是一项阻塞任务,您的应用程序会非常慢并且没有反应。