16

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" address (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.

4 Answers 4

14

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.

2
  • When I do this I end up with emails with no body. All the headers work great. I don't see how "template path" is defined - is it possible that because I'm using generated views they're not working?
    – Calciphus
    Sep 18, 2013 at 4:41
  • Nevermind - it turns out that if I change :template_path to: :template_path => "devise/mailer" I can use the templates I already created.
    – Calciphus
    Sep 18, 2013 at 4:54
11

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"
5
  • One question... where would I save that new file? That's very helpful info. The backup email comes from data on each user's registration form, hopefully we can use something like :bcc => resource.backup_email.
    – jpw
    Jun 8, 2011 at 21:01
  • 1
    You would actually have to make a mailer, follow this guide but make it a subclass of Devise::Mailer instead.
    – David
    Jun 8, 2011 at 22:11
  • PERFECT THANKS. I've documented the changes at the end of my original question, since I also needed to move the mailer erb templates to the new view
    – jpw
    Jun 12, 2011 at 3:29
  • In last version of Devise this answer wouldn't work. I'll submit a new one. Jul 10, 2013 at 11:36
  • How can I do this while loading the resource as well; i.e. I want to set the 'from' field to be resource.invited_by.full_name.
    – cjm2671
    Dec 30, 2013 at 13:35
11

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.