开发者

rails 3 + devise: how to modify the mailer method for confirmation emails to add user's second email address

开发者 https://www.devze.com 2023-03-11 13:06 出处:网络
Background: In our app, we often have a sales rep do the setup for our customer using the salesperson\'s computer (often customers don\'t have access to their email at the time we set them up). So we\

Background: In our app, we often have a sales rep do the setup for our customer using the salesperson's computer (often customers don't have access to their email at the time we set them up). So we're thinking to add a field to the devise registration form for the sales rep's email address and have the confirm link ALSO go to that email address.

Question: Is there a way to tell devise to bcc (or cc) the initial confirmation email (only the initial confirmation email) to an (optional) "backup_email" email address that is also provided on the new user registration form?

Alternatively, is there a way to 'disable' the confirmation email process but ONLY when a certain code is entered into the registration field?

I know how to add another field to the devise registration form, but I don't see how/where to modify the devise mailer code so when a confirmation email is sent to the "email" address it ALSO goes to the "backup_email" add开发者_StackOverflow社区ress (if any, sometimes it's blank).


Thanks to Johnny Grass!

I did rails generate mailer CustomerUserMailer and added

#config/initializers/devise.rb
config.mailer = "CustomUserMailer"

my custom mailer looks like:

# app/mailers/customer_user_mailer.rb
class CustomUserMailer < Devise::Mailer
  def headers_for(action)
    headers = {
      :subject       => translate(devise_mapping, action),
      :from          => mailer_sender(devise_mapping),
      :to            => resource.email,
      :cc            => resource.backup_user_email(action),
      :template_path => template_paths
    }
  end
end

Then I moved the 3 mailer templates FROM views/devise/mailer to views/customer_user_mailer (otherwise the emails are empty)

Then I added a method to my User model called backup_user_email() that returns the 'backup' email address (if any) based on the data in the user record and the action. The only "trick" there is that when testing the action it is not action == "confirmation_instructions" it is action == :confirmation_instructions.


Just in case anyone got here through Google - in the latest version of Devise, header_for takes two parameters. So your code would need to be:

class MyMailer < Devise::Mailer
  backup_email = "..."
  def headers_for(action, opts)
    headers = {
      :subject       => subject_for(action),
      :to            => resource.email,
      :from          => mailer_sender(devise_mapping),
      :bcc           => backup_email,
      :reply_to      => mailer_reply_to(devise_mapping),
      :template_path => template_paths,
      :template_name => action
    }.merge(opts)
  end
end

That might not be the best way to do it, but at least it avoids errors.


One way to do it would be to override the headers_for action in Devise::Mailer

class MyMailer < Devise::Mailer
  backup_email = "..."
  def headers_for(action)
    headers = {
     :subject       => translate(devise_mapping, action),
     :from          => mailer_sender(devise_mapping),
     :to            => resource.email,
     :bcc           => backup_email
     :template_path => template_paths
  }
end

And tell devise to use your mailer:

#config/initializers/devise.rb
config.mailer = "MyMailer"


You can use this code which is much cleaner and simpler

# app/mailers/my_mailer.rb
class MyMailer < Devise::Mailer
  def headers_for(action, opts)
    backup_email = "..."
    super.merge!({bcc: backup_email})
  end
end

# config/initializers/devise.rb
...
config.mailer = MyMailer
...

With Hash passed to merge! method you can add or modify any email headers you'd like.


Your answer worked for me. Thank you so much.

I had a scenario wherein, i was required to customize devise to: 1) send signup confirmation emails in BCC to different emails based on environment. 2) emails should be added to BCC only for signup confirmation mails.

To achieve that, i compared values for action argument, like shown in the code snippet below:

def headers_for(action)
  if action == :confirmation_instructions
    if Rails.env.production?
        recipient_email = "user1@example.com"
    else
        recipient_email = "user2@example.com"
    end

    headers = {
      :subject => translate(devise_mapping, action),
      :from => mailer_sender(devise_mapping),
      :to => resource.email,
      :bcc => recipient_email,
      :template_path => template_paths
    }
  else
    super
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消