• Home
  • Jobs
  • Courses
  • Questions
  • Teachers
  • For business
  • ES/EN

0

275
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 ๐Ÿ™

4 months 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
4 months 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

4 months ago ยท Santiago Trujillo Report
Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
ยฉ 2022 PeakU Inc. All Rights Reserved.