如何禁用特定用户的电子邮件通知

时间:2014-05-06 19:21:34

标签: ruby-on-rails mailboxer

当用户在收件箱内收到新邮件时,Mailboxer会发送电子邮件通知。这很好,但有些用户可能不想接收电子邮件。如何在用户的视图中显示一个复选框选项,该选项将禁用"新消息"电子邮件通知?

mailboxer.rb:

Mailboxer.setup do |config|

  #Configures if you applications uses or no the email sending for Notifications and Messages
  config.uses_emails = true

  #Configures the default from for the email sent for Messages and Notifications of Mailboxer
  config.default_from = "no-reply@mailboxer.com"

  #Configures the methods needed by mailboxer
  config.email_method = :mailboxer_email
  config.name_method = :name

  #Configures if you use or not a search engine and wich one are you using
  #Supported enignes: [:solr,:sphinx]
  config.search_enabled = false
  config.search_engine = :solr
end

2 个答案:

答案 0 :(得分:2)

您可以通过执行以下操作为用户提供选择:

def mailboxer_email(object)
  self.email if self.preferences.receive_direct_message_by_email?
end

def mailboxer_email(object)
    if self.no_email
    else
        nil
    end
end

如果使用第二种方法,则必须创建no_email属性。

答案 1 :(得分:1)

假设您的User模型是可消息的,您可以按以下方式覆盖mailboxer_email方法:

class User < ActiveRecord::Base
  acts_as_messageable

  ...

  def mailboxer_email(object)
    if self.opts_out   # some attribute on the user to indicate they opt out of receiving emails
      nil
    else
      return self.email   # or whatever address the email is to be sent to
    end
  end
end