Home

pboling edited this page Aug 13, 2012 · 6 revisions
Clone this wiki locally

Welcome to the sanitize_email wiki!

Note all of these examples assume the following:

You have set :activation_proc such that sanitize email is 'always on':

SanitizeEmail::Config.configure do |config|
  config[:activation_proc] = Proc.new {true}
end

Example using SanitizeEmail.force_sanitize

class User < ActiveRecord::Base
  def test_signup_email_me_only
    SanitizeEmail.force_sanitize = true
    UserMailer.signup_notification(self).deliver
    SanitizeEmail.force_sanitize = nil
  end
  def test_signup_email_user_only
    SanitizeEmail.force_sanitize = false
    UserMailer.signup_notification(self).deliver
    SanitizeEmail.force_sanitize = nil
  end
end

Now regardless of other considerations: User.find(4).test_signup_email_me_only will have its normal recipients, bcc, and cc overridden to be whatever you set the sanitized values to be.

Then if you want to send it to the actual user, instead of yourself User.find(4).test_signup_email_user_only

Example using SanitizeEmail.sanitary

class User < ActiveRecord::Base
  def test_signup_email_me_only
    SanitizeEmail.sanitary do
      UserMailer.signup_notification(self).deliver
    end
  end
end

Now regardless of other considerations: User.find(4).test_signup_email_me_only will have its normal recipients, bcc, and cc overridden to be whatever you set the sanitized values to be.

You can also pass in any of SanitizeEmail's configuration options to the sanitary call to do instance specific overrides. For example if SanitizeEmail generally is setup to go to joe@example.org, but here you want to send it to bob@example.org

class User < ActiveRecord::Base
  def test_signup_email_me_only
    SanitizeEmail.sanitary({:sanitized_to => 'bob@example.org'}) do
      UserMailer.signup_notification(self).deliver
    end
  end
end

Example using SanitizeEmail.unsanitary

class User < ActiveRecord::Base
  def test_signup_email_live_user_only
    SanitizeEmail.unsanitary do
      UserMailer.signup_notification(self).deliver
    end
  end
end

Now regardless of other considerations: User.find(4).test_signup_email_me_only will never be overridden, and will be completely ignored by SanitizeEmail.