Search code examples
ruby-on-railsrubydeviserubygemsdevise-invitable

how display current_user/invited_by user email display of devise invitable invitation


I use my own mailer instead of having devise email things for me, as per devise documentation.

Here is how I do it:

class User < ActiveRecord::Base
  attr_reader :raw_invitation_token
end

class InvitationsController < Devise::InvitationsController
  def create
    @from    = params[:from]
    @subject = params[:invite_subject]
    @content = params[:invite_content]

    @user = User.invite!(params[:user], current_user) do |u|
      u.skip_invitation = true
    end


    email = NotificationMailer.invite_message(@user, @from, @subject, @content)
  end
end

class NotificationMailer < ActionMailer::Base
  def invite_message(user, venue, from, subject, content)
    @user = user
    @token = user.raw_invitation_token
    invitation_link = accept_user_invitation_url(:invitation_token => @token)

    mail(:from => from, :bcc => from, :to => @user.email, :subject => subject) do |format|
      content = content.gsub '{{first_name}}', user.first_name
      content = content.gsub '{{last_name}}', user.first_name
      content = content.gsub '{{full_name}}', user.full_name
      content = content.gsub('{{invitation_link}}', invitation_link)
      format.text do
        render :text => content
      end
    end
  end
end

but when I received email that from is [email protected] and that is defined in development.rb for configration of mailer. however I wana email of from should be current user means that who is inviting someone when I received.

please help me.


Solution

  • This isn't possible. Think about the consequences if anybody could send email using anybody else's email address! Your best option is to set the Sender's name in the from header while keeping your standard email address. You can set it with

    :from => "#{user.first_name} #{user.last_name} <#{from}>"
    

    Basically just get the from to be formatted as Sender's name <[email protected]>

    If you want to add a reply-to with the user's email address, you can pass it in as a parameter with this format:

    :reply_to => user.email
    

    To use the example you gave, it'd look like this:

    mail(:reply_to => from, :bcc => from, :to => @user.email, :subject => subject) do |format|
    

    (this assumes that you've already got the from param set as a default somewhere, which I believe you do)