Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

741
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda