【问题标题】:How will I pass parameters such as, user id in hyper link?我将如何在超链接中传递参数,例如用户 ID?
【发布时间】:2020-10-12 08:19:30
【问题描述】:

我有一个 HTML 页面,其中有一个超链接。这封html邮件会通过outlook发送给用户(我用flask python写过邮件功能),当用户点击邮件正文上的超链接时,最终会打开另一个页面。这个页面是一样的,但是页面的内容会根据用户的email id对不同的用户有所不同。

现在,我的要求是通过超链接传递用户电子邮件 ID,这样我就可以根据电子邮件 ID 显示不同的内容。可以通过超链接完成吗?既然你知道outlook使用Microsoft Word作为渲染引擎,那么通过超链接传递参数会不会很困难?

或者,我可以在发送邮件时通过我的flask函数传递电子邮件ID吗?

下面是我的flask函数,它将向outlook发送邮件

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>

任何建议都会非常有帮助。

【问题讨论】:

  • 你能和我们分享一些代码吗?
  • 我刚刚添加了

标签: html email flask outlook hyperlink


【解决方案1】:

Flask 已经有一个内置函数url_for 可以正确生成带有额外参数的链接。参考这个doc

更新

  • 建议为路线选择准确的名称
  • 建议在命名视图时使用snake_case
  • 推荐你参考官方Flask-Maildoc部分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函数的逻辑,当用户点击链接时,它将被重定向到你的应用程序,向他展示一个定制的页面/教程:

@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 更有效地处理批量电子邮件,因为发送邮件是一项阻塞任务,您的应用程序将非常缓慢且没有响应。

【讨论】:

  • 感谢您的回复,此超链接是否适用于 Outlook 电子邮件正文?如前所述,我的邮件功能将使用 Outlook 向用户发送邮件。
  • @DebarthaMitra 我更新了我的帖子
  • @DebarthaMitra oulook, gmail .. 有一个非常强大的自动化流程来按标题、正文或内容过滤电子邮件.. 但如果他们检测到附件是似是而非的(病毒、可执行代码甚至 js 脚本.. )或链接(钓鱼..)他们会将电子邮件标记为危险并阻止内容。在您的情况下,Outlook 将主要将批量电子邮件标记为垃圾邮件,以避免尝试使用 Sendgrid、mailshimp 通过 api 发送跨国电子邮件。
  • 谢谢您,您的解决方案有效,让我看看您提到的api。
  • @DebarthaMitra 这是一个起点:twilio.com/blog/…github.com/miguelgrinberg/sendgrid-flask-mail,你也可以在 pypi 上找到第三个包
猜你喜欢
  • 1970-01-01
  • 2017-10-14
  • 1970-01-01
  • 2023-03-20
  • 2017-06-28
  • 2020-02-17
  • 1970-01-01
  • 2020-02-15
  • 2015-12-07
相关资源
最近更新 更多