Django 2 电子邮件退订链接 [英] Django 2 email unsubscribe link

查看:50
本文介绍了Django 2 电子邮件退订链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在发送给用户的电子邮件中添加取消订阅链接.我不确定如何实现它.我希望用户无需登录即可取消订阅.有关如何实现这一点的任何建议将不胜感激.谢谢

I want to add an unsubscribe link in emails being sent to the user. I am unsure on how to achieve it. I want the user to be able to unsubscribe without having to log in. Any suggestions on how to achieve this would be appreciated. Thanks

推荐答案

我假设您知道如何使用 django 发送电子邮件以及其他基本知识.

I'll assume you know how to send an email with django, and other basic things.

对于您发送给他们的每封电子邮件,您必须先建立取消订阅链接,如下所示.请注意,以下有一个 url 模式,以及一个采用 uuid 字段的模型(具体来说是 uuid4,因为它是最安全的).你想这样做的原因是随机的人不能去YourWebsite.com/unsub_email/1、/2、/3、/4等,和取消订阅尽可能多的人他们可能可以通过猜测数字来做到这一点.按照我所展示的方式进行操作,有可能是 100 分之一的概率,之后有 50 个以上的零,他们甚至会得到 1.所以它更安全.

For every email you send to them, you have to build the unsubscribe link first, like the following. Note that the following has a url pattern, and a model that takes a uuid field (uuid4 to be specific, because it's the safest one). The reason why you want to do it this way is so random people can't go to YourWebsite.com/unsub_email/1, /2, /3, /4, etc, and unsub as many people as they possibly can by guessing the number. By doing it the way I show, there's something like a chance of 1 in 100, with like 50+ zero's after it that they would even get 1. So it's safer.

def send_user_an_email(id_of_your_object):
    users_email = UsersEmail.objects.get(id=id_of_your_object)
    unsub_link = 'https://www.YourWebsite.com/unsub_email/{}/{}/'.format(users_email.id, users_email.random_uuid)
    subject = 'Your subject line.'
    message = 'Your message body.\n\nGo here to unsubscribe: {}'.format(unsub_link)
    mail_sent = send_mail(subject, message, 'DoNotReply@YourWebsite.com', [users_email.users_email_char_field])
    return mail_sent

然后您只需编写一个函数,让他们单击该页面上的按钮,然后他们就会取消订阅,或者您想以其他方式执行此操作.一个更安全的选择是向他们显示一个有效的取消订阅页面,但让他们再次输入他们的电子邮件,但不要向他们显示与有效 url 模式相关的电子邮件.对于想要编写糟糕脚本以取消订阅列表中的每个人的人来说,这个额外的步骤会变得更加困难.

Then you just write a function to let them click a button on that page, and they get unsubbed, or however else you want to do it. An even safer option would be to show them a valid unsub page, but make them type out their email again, but don't show them the email related to the valid url pattern. That extra step would make it that much harder for someone who'd want to write a bad script to unsub everyone on your list.

这篇关于Django 2 电子邮件退订链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆