0

Mailboxer is sending email notifications when the user has received a new message inside their inbox. This is great, however some users may not want to receive emails. How can I present a checkbox option in the view for the user that will disable the "new message" email notifications?

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
pwz2000
  • 1,385
  • 2
  • 16
  • 50

2 Answers2

2

You could give the user a choice by doing:

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

or

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

You have to create a no_email attribute if going with second method.

Cornelius Wilson
  • 2,844
  • 4
  • 21
  • 41
1

Assuming it is your User model which is messagable, you could override the mailboxer_email method along these lines:

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
  • 1
    To simplify he could just do `self.email unless self.opts_out` – Cornelius Wilson May 07 '14 at 15:32
  • I get a ``undefined method `opts_out'``. ``def mailboxer_email(object)`` originally only contained `email`. Anything else I add to it gives undefined method. – pwz2000 May 15 '14 at 18:02
  • `opts_out` was simply an example of an attribute you could put on the User model to indicate that the user wishes to opt out of these email notifications. I don't know how you identify such users in your application; I was just showing that if you return `nil` from the `mailboxer_email` method (instead of an email address) that user won't receive the notification email. –  May 15 '14 at 18:05