You are currently browsing the tag archive for the ‘Mail’ tag.

No need to check in normal code, just do everything as usual and this interceptor will prevent you from spamming people who do not want any emails.
Its not perfect(missing bc/bcc filters but if should be fine for 90% of cases)

#config/initializers/blacklisted_emails.rb
class MailInterceptor
  def self.delivering_email(message)
    if User.where(:email => message.to, :receive_emails => false).any?
      message.perform_deliveries = false
    end
  end
end

Mail.register_interceptor(MailInterceptor)

Activation_code, password_reminder_token and more to come.

Strange code:

before_create :make_activation_token

def make_activation_code
  self.activation_code = Digest::SHA1.hexdigest( ...)
end

def forgot_password
  ...
  password_reset_token=Digest::SHA1.hexdigest(...)
end
#and so on...

Let the mailman handle the postbox keys
Stay with a simple email token. It is updated every time we send any activation/reset/verification mail so no user can perform two action with the same token or say ‘find’ an old token and then request a password reset.

#user.rb
def update_email_token
  update_attribute(:email_token,Digest::SHA1.hexdigest(..)
end

#user_mailer.rb
def setup_email(user)
  user.update_email_token
  ...
end
Follow

Get every new post delivered to your Inbox.

Join 63 other followers