Rails 3.1:在哪里过滤掉已选择退出的电子邮件地址
尝试找出最干净的方法来防止向在 Rails 3.1 中选择不接收电子邮件的用户发送电子邮件。
我正在考虑重写 Mail.deliver 来检查数据库并确定收件人是否取消订阅,然后有条件地发送电子邮件。
这似乎是侵入性最小的方法,但需要创建永远不会发送的 Mail 对象。
似乎最资源意识的方法是在控制器中进行检查,从而防止永远不会发送的 Mail 对象摆脱存在的负担。
但这似乎更具侵入性,并且开发人员容易在创建新邮件程序时忘记进行检查。
对于这种情况有标准做法吗?
** 编辑 ** 这是为了管理选择不接收通知的用户集合,而不是管理新闻通讯的订阅。
Trying to figure out the cleanest way prevent sending email to users who have opted out from receiving them in rails 3.1.
I was thinking about overriding Mail.deliver to check the db and determine if the recipients are unsubscribed or not, then conditionally delivering the email.
That seems like the least intrusive way to go about it, but requires creating the Mail objects that are never going to be sent.
Seems like the most resource conscious way would be to do the check in the controller, thus preventing the Mail objects that are never going to be sent from the burden of existence.
This though seems more intrusive and prone to developers forgetting to make the check when creating new mailers.
Is there a standard practice for this situation?
** edit **
This is for managing a collection of users who have opted out of receiving notifications, rather than something like managing subscriptions to a news letter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果确定是否接收电子邮件通知的属性只是数据库中模型上的一个字段,您可以创建一个名为“want_email_notifications”之类的命名范围来获取所有已订阅的用户。
因此,如果您有一个
User
类,并且该类有一个名为opt_out
的属性,那么您可以执行以下操作:然后,要调用它,您可以执行
User.want_email_notifications
,它为您提供了所有需要电子邮件通知的User
对象的数组。然后,当您检查给定用户是否应该接收电子邮件通知时,编写类似于以下的条件:
在此示例中,
send_email_notification
是您将调用关联的传递方法的方法,该方法实际上发送电子邮件。If the attribute that determines whether or not to get email notifications is just a field on a model in the DB, you could create a named scope called something like 'want_email_notifications' to get all the users that have subscribed.
So, if you have a
User
class, and that class has an attribute calledopt_out
, then you could do something like:Then, to call it, you do
User.want_email_notifications
, which gives you an array of allUser
objects that want email notifications.Then, when you're checking whether or not a given user should receive an email notification, write a condition similar to:
In this example,
send_email_notification
is the method where you would call the associated delivery method, which actually sends the email.