How to set the 'from' header for devise invitable invitation_instructions?

459 Views Asked by At

So I'm trying to override the 'from' field so it actually includes the name of the inviter, rather than the system default.

I've followed the instructions here https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer

I've got my custom mailer set up, but the problem I'm having is that it doesn't look like there is an opts object that comes through that I can modify, unlike the example in the documentation.

Anyone have any suggestions?

class MCDeviseMailer < Devise::Mailer
  helper :application # gives access to all helpers defined within `application_helper`.

  def invitation_instructions(record)        
    # opts[:from] = "#{resource.invited_by.full_name rescue "Mission Control"} <notifications@#{DOMAIN}>"
    super
  end
end

Many thanks!

2

There are 2 best solutions below

0
John Bachir On

You have to monkeypatch send_devise_notification. put something like this in User:

def send_devise_notification(notification, *args)
  if :invitation_instructions == notification
    args << {
      from: "#{inviter_name} <#{inviter_email}>",
    }
  end
  super
end
0
Nadeem Yasin On

Take a look at devise wiki

You can write your own instruction. Simply override

def invitation_instructions(record, opts = {})
  ...
end