Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

742
Views
Override Devise Invitable `invite!` method in order to add attachment to invitation email - Rails

I have devise_invitable gem and an invitations controller InvitationsController < Devise::InvitationsController. I invite my user with this method User.invite!(user, inviter) from the gem.

Now, I would like to add pdf attachment to this invitation but I am not able to override the mailer method in order to do stuff like this: attachments['terms.pdf'] = File.read('/path/terms.pdf').

My invitation email is working fine and I would like to keep it like that but with an attachment.

Here my routes.rb:

devise_for :users, controllers: { invitations: 'api/v1/users/invitations' }

What am I missing?

Thanks ๐Ÿ™

over 4 years ago ยท Santiago Trujillo
2 answers
Answer question

0

DeviseInvitable has a usefull skip_invitation option. With the following method you can easily use your own mailer and add attachment in it :

def invite_and_notify_user(email)
  user = User.invite!({ email: email }) do |u|
    u.skip_invitation = true
  end
  notify_by_email(user)
end


def notify_by_email(user)
  MyUserMailer.invited_user_email(user).deliver
end

In MyUserMailer class:

  def invited_user_email(user)
    @user = user
    attachments['terms.pdf'] = File.read('/path/terms.pdf')
    mail(to: user.email, from: 'contact@mail.com', subject: "Your subject")
  end
over 4 years ago ยท Santiago Trujillo Report

0

You can override the devise mailer like so...

class MyMailer < Devise::Mailer   
  helper :application # gives access to all helpers defined within `application_helper`.
  include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
  default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views
  # If there is an object in your application that returns a contact email, you can use it as follows
  # Note that Devise passes a Devise::Mailer object to your proc, hence the parameter throwaway (*).
  default from: ->(*) { Class.instance.email_address }
end

You can then in your config/initializers/devise.rb, set config.mailer to "MyMailer". Note that this overrides the mailer used for all devise modules.

Then in that class add your attachment to invited_user_instructions...

def invited_user_instructions(*args)
  attachments['terms.pdf'] = File.read('/path/terms/pdf')
  super
end

This is devise's instructions https://github.com/heartcombo/devise/wiki/How-To:-Use-custom-mailer

over 4 years ago ยท Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!