Rails 3.1:在哪里过滤掉已选择退出的电子邮件地址

发布于 2024-12-05 02:10:10 字数 331 浏览 1 评论 0原文

尝试找出最干净的方法来防止向在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2024-12-12 02:10:10

如果确定是否接收电子邮件通知的属性只是数据库中模型上的一个字段,您可以创建一个名为“want_email_notifications”之类的命名范围来获取所有已订阅的用户。

因此,如果您有一个 User 类,并且该类有一个名为 opt_out 的属性,那么您可以执行以下操作:

class User < ActiveRecord::Base

  named_scope :want_email_notifications, :conditions => ['opt_out = ?', false]

  ...
end

然后,要调用它,您可以执行 User.want_email_notifications,它为您提供了所有需要电子邮件通知的 User 对象的数组。

然后,当您检查给定用户是否应该接收电子邮件通知时,编写类似于以下的条件:

send_email_notification(user_in_question) if User.want_email_notifications.include?(user_in_question)

在此示例中,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 called opt_out, then you could do something like:

class User < ActiveRecord::Base

  named_scope :want_email_notifications, :conditions => ['opt_out = ?', false]

  ...
end

Then, to call it, you do User.want_email_notifications, which gives you an array of all User 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:

send_email_notification(user_in_question) if User.want_email_notifications.include?(user_in_question)

In this example, send_email_notification is the method where you would call the associated delivery method, which actually sends the email.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文