Override mailer in devise_invitable? - ruby-on-rails

I would like the invitations for my app to come from the inviter instead of a system email address. How can I override the config.mailer_sender from devise.rb?
I have this in my mailer and have confirmed that it is getting called, but it does not override the :from. Note: it is a private method, I tried it as a public method with no effect.
private
def headers_for(action)
if action == :invitation_instructions
headers = {
:subject => "#{resource.invited_by.full_name} has invited you to join iTourSmart",
:from => resource.invited_by.email,
:to => resource.email,
:template_path => template_paths
}
else
headers = {
:from => mailer_sender(devise_mapping),
:to => resource.email,
:template_path => template_paths
}
end
if resource.respond_to?(:headers_for)
headers.merge!(resource.headers_for(action))
end
unless headers.key?(:reply_to)
headers[:reply_to] = headers[:from]
end
headers
end

The better solution without any hacks/monkey patches will be:
for example, in your model:
def invite_and_notificate_member user_email
member = User.invite!({ email: user_email }, self.account_user) do |u|
u.skip_invitation = true
end
notificate_by_invitation!(member)
end
def notificate_by_invitation! member
UserMailer.invited_user_instructions(member, self.account_user, self.name).deliver
end
In the mailer:
def invited_user_instructions(user, current_user, sa)
#user = user
#current_user = current_user
#sa = sa
mail(to: user.email, subject: "#{current_user.name} (#{current_user.email}) has invited you to the #{sa} account ")
end
So you can put any subject/data in the body of the mail.
Good luck!

Look at my answer to a similar question, it might help.
Edit: so it seems that you need to define a public headers_for method in your resource class.
Solution: Put some version of this method in User.rb, make sure it's public.
def headers_for(action)
action_string = action.to_s
case action_string
when "invitation" || "invitation_instructions"
{:from => 'foo#bar.com'}
else
{}
end
end
You have to return a hash in because Devise::Mailer will try to merge the hash values.

Take a look at devise_invitable wiki.
class User < ActiveRecord::Base
#... regular implementation ...
# This method is called interally during the Devise invitation process. We are
# using it to allow for a custom email subject. These options get merged into the
# internal devise_invitable options. Tread Carefully.
#
def headers_for(action)
return {} unless invited_by && action == :invitation_instructions
{ subject: "#{invited_by.full_name} has given you access to their account" }
end
end

Related

How to send mail for multiple models in rails

I am new to rails. I am having problem in mail sending to multiple models. Our project contains parent,teacher and student models.each module having number of users(student,parent,teacher). And also I am having three check box.that is student,teacher,parent.when I click student and teacher.the mail should be sent to all teachers and all students.
If I want send a mail to teacher and also student means ,the problem behind this, mail was sending only to teacher not student. how to solve this problem.and I included my coding.
Controller
def send_news_letter
if params[:announcement].present?
#announcement = Announcement.find(params[:announcement].keys).first
end
if params[:students].present? and params[:teachers].present?
#student = Student.pluck(:email)
#teacher = Teacher.pluck(:email)
UserMailer.send_multiple_email(#student,#teacher,#announcement).deliver
redirect_to announcements_url, :notice => "Newsletter Delivered Successfully" a
end
end
Usermailer.rb
class UserMailer < ActionMailer::Base
default to: Proc.new {Teacher.pluck(:email)},
to: Proc.new {Student.pluck(:email)},
from: "from#example.com"
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.user_mailer.password_reset.subject
#
def password_reset(user)
#user = user
mail :to => user.email, :subject => "Password Reset"
end
def send_multiple_email(user,employee,announcement)
#user = user
#employee = employee
#announcement = announcement
mail :subject => "Deliver"
end
end
Please help me.Thanks in advance.
First, in your controller I would store all addresses in one array:
#emails = []
if params[:students].present?
#emails += Student.pluck(:email)
end
if params[:teachers].present?
#emails += Teacher.pluck(:email)
end
if params[:parents].present?
#emails += Parent.pluck(:email)
end
UserMailer.send_multiple_email(#emails,#announcement).deliver
And then in your mailer change to this:
def send_multiple_email(emails,announcement)
#announcement = announcement
emails.each do | address |
mail :to => address, :subject => "Deliver"
end
end
Please note, that if you're referencing your models in the mailer template (such as "Hi <%= #user.name %>!") then you need to load the whole model object. Now you're just using pluck to get a list of all the addresses you want to send to. To get the whole model, change pluck(:email) to all in your controller and change your mailer to reference the attributes in that model instead. This also means your three models need to have the same attribute names (at least the ones you intend to use in the mailer).
Hope it makes sense.

Add dynamic value in devise email subject

Ok I have seen many discussions about customizing devise email subject but none seems to solve what I want. Currently my confirmation email subject reads "Confirm your Qitch.com account". I want to customize this email subject and add a dynamic value of a user's name in it such that if user ALEX signs up for an account, he should get an email address with the subject, Welcome ALEX, confirm your Qitch.com account. How can I achieve this in devise?
devise.en.yml
mailer:
confirmation_instructions:
subject: 'Confirm your Qitch.com account'
reset_password_instructions:
subject: 'Reset your Qitch.com password'
unlock_instructions:
subject: 'Unlock your Qitch.com account'
Lastly, how do I add a name in the reply address or from address, currently when you receive the mail, it says sender: no-reply#qitch.com Is there a way I can customize it to Qitch
Thanks
I see that no answers are clean enough so I would like to make a short summary here.
First of all, you have to tell Devise you're going to override its origin mailer methods by:
config/initializers/devise.rb
config.mailer = 'MyOverriddenMailer'
After that, you need to create your overridden mailer class and override whatever method you want like this:
app/mailers/my_overridden_mailer.rb
class MyOverriddenMailer < 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 you mailer uses the devise views
def confirmation_instructions(record, token, opts={})
if record.name.present?
opts[:subject] = "Welcome #{record.name}, confirm your Qitch.com account"
else
opts[:subject] = "Confirm your Qitch.com account"
end
super
end
end
Remember to restart your Rails server to apply the changes! :)
Note:
List of opts options: subject, to, from, reply_to, template_path, template_name.
record is the instance of User model
And of course, origin document
Devise helper here and How To: Use custom mailer
class MyMailer < Devise::Mailer
def confirmation_instructions(record, opts={})
headers = {
:subject => "Welcome #{resource.name}, confirm your Qitch.com account"
}
super
end
def reset_password_instructions(record, opts={})
headers = {
:subject => "Welcome #{resource.name}, reset your Qitch.com password"
}
super
end
def unlock_instructions(record, opts={})
headers = {
:subject => "Welcome #{resource.name}, unlock your Qitch.com account"
}
super
end
end
Or
class MyMailer < Devise::Mailer
...
...
private
def headers_for(action)
if action == :confirmation_instructions
headers = {
:subject => "Welcome #{resource.name}, confirm your Qitch.com account"
}
elsif action == :reset_password_instructions
headers = {
:subject => "Welcome #{resource.name}, reset your Qitch.com password"
}
else
headers = {
:subject => "Welcome #{resource.name}, unlock your Qitch.com account"
}
end
end
end
And tell devise to use your mailer:
#config/initializers/devise.rb
config.mailer = "MyMailer"
NOTE : I haven't tried them yet, but they may be helpful and for anyone, please correction my answer, if there is an error you could edit my answer
I'm working with devise (3.2.1) and I've implemented the following solution, but to modify the from: field with localization:
# app/mailers/devise_mailer.rb
class DeviseMailer < Devise::Mailer
def confirmation_instructions(record, token, opts={})
custom_options(opts)
super
end
def reset_password_instructions(record, token, opts={})
custom_options(opts)
super
end
def unlock_instructions(record, token, opts={})
custom_options(opts)
super
end
private
def custom_options(opts)
opts[:from] = I18n.t('devise.mailer.from', name: Tenancy.current_tenancy.name, mail: ENV['FROM_MAILER'] )
end
end
Then I've defined the message in my locale files
# config/locales/devise.es.yml
es:
devise:
mailer:
from: "Portal de trabajo %{name} <%{mail}>"
To modify the subject, it should be almost the same:
def confirmation_instructions(record, token, opts={})
custom_options(opts, :confirmation_instructions)
super
end
private
def custom_options(opts, key)
opts[:from] = I18n.t('subject', scope: [:devise, :mailer, key])
end
# and in your locale file
es:
devise:
mailer:
confirmation_instructions:
subject: Instrucciones de confirmaciĆ³n
Hook in headers_for to e.g. prefix the subject for all devise mails.
# config/initializers/devise.rb
Devise.setup do |config|
# ...
config.mailer = 'DeviseMailer'
# ...
end
# app/mailers/devise_mailer.rb
class DeviseMailer < Devise::Mailer
def headers_for(action, opts)
super.merge!({ subject: 'Hi ALEX! ' + subject_for(action) })
end
end
Yo should create an ActionMailer like this one:
class Sender < ActionMailer::Base
default :from => "'Eventer' <dfghjk#gmail.com>"
def send_recover(user, pw)
mail(:to => user.email , :subject => "You have requested to change your password from Eventer") do |format|
format.text {
render :text =>"Dear #{user.name},
**Body**
Regards."
}
end
end
end
Then you should call it from the controller this way:
Sender.send_recover(#mail, current_user, #meetup).deliver
I hope it works for you!
Regards

How can I override destroy_if_previously_invited in devise_invitable?

I'm using devise_invitable with devise in my Rails 3 app. I want to give my users the ability to invite other users and group those invited users in advance of the invitees signing up.
My problem is that once the invitee comes along and signs up (but doesn't use the invite URL), the destroy_if_previously_invited around_filter comes along, destroys the original user record and recreates a new record for the user, retaining the invitation data but not transferring the user_groups records along with it.
I'd like to simply override this around_filter by doing a search for any user_groups that match the originally invited user_id and saving them with the newly created user_id.
I keep getting the error:
LocalJumpError in Users::RegistrationsController#create
no block given (yield)
My route looks like this:
devise_for :users, :controllers => { :registrations => "users/registrations" }
I've set this as the override in app/controllers/users/registrations_controller.rb:
class Users::RegistrationsController < Devise::RegistrationsController
around_filter :destroy_if_previously_invited, :only => :create
private
def destroy_if_previously_invited
invitation_info = {}
user_hash = params[:user]
if user_hash && user_hash[:email]
#user = User.find_by_email_and_encrypted_password(user_hash[:email], '')
if #user
invitation_info[:invitation_sent_at] = #user[:invitation_sent_at]
invitation_info[:invited_by_id] = #user[:invited_by_id]
invitation_info[:invited_by_type] = #user[:invited_by_type]
invitation_info[:user_id] = #user[:id]
#user.destroy
end
end
# execute the action (create)
yield
# Note that the after_filter is executed at THIS position !
# Restore info about the last invitation (for later reference)
# Reset the invitation_info only, if invited_by_id is still nil at this stage:
#user = User.find_by_email_and_invited_by_id(user_hash[:email], nil)
if #user
#user[:invitation_sent_at] = invitation_info[:invitation_sent_at]
#user[:invited_by_id] = invitation_info[:invited_by_id]
#user[:invited_by_type] = invitation_info[:invited_by_type]
user_groups = UserGroup.find_all_by_user_id(invitation_info[:user_id])
for user_group in user_groups do
user_group.user_id = #user.id
user_group.save!
end
#user.save!
end
end
end
I may also just be going about this all wrong. Any ideas would be appreciated.
First, there is no need to set the around_filter again, as devise_invitable already sets this. All you need to do is redefine the methods in your UsersController and those will be called instead of the devise_invitable ones.
Second, it looks like you are combining the two methods when they should remain seperate and overridden (I am basing this off latest version of devise_invitatable 1.1.1)
Try something like this:
class Users::RegistrationsController < Devise::RegistrationsController
protected
def destroy_if_previously_invited
hash = params[resource_name]
if hash && hash[:email]
resource = resource_class.where(:email => hash[:email], :encrypted_password => '').first
if resource
#old_id = resource.id #saving the old id for use in reset_invitation_info
#invitation_info = Hash[resource.invitation_fields.map {|field|
[field, resource.send(field)]
}]
resource.destroy
end
end
end
def reset_invitation_info
# Restore info about the last invitation (for later reference)
# Reset the invitation_info only, if invited_by_id is still nil at this stage:
resource = resource_class.where(:email => params[resource_name][:email], :invited_by_id => nil).first
if resource && #invitation_info
resource.invitation_fields.each do |field|
resource.send("#{field}=", #invitation_info[field])
end
### Adding your code here
if #old_id
user_groups = UserGroup.find_all_by_user_id(#old_id)
for user_group in user_groups do
user_group.user_id = resource.id
user_group.save!
end
end
### End of your code
resource.save!
end
end

rails 3 + devise: how to modify the mailer method for confirmation emails to add user's second email address

Background: In our app, we often have a sales rep do the setup for our customer using the salesperson's computer (often customers don't have access to their email at the time we set them up). So we're thinking to add a field to the devise registration form for the sales rep's email address and have the confirm link ALSO go to that email address.
Question: Is there a way to tell devise to bcc (or cc) the initial confirmation email (only the initial confirmation email) to an (optional) "backup_email" email address that is also provided on the new user registration form?
Alternatively, is there a way to 'disable' the confirmation email process but ONLY when a certain code is entered into the registration field?
I know how to add another field to the devise registration form, but I don't see how/where to modify the devise mailer code so when a confirmation email is sent to the "email" address it ALSO goes to the "backup_email" address (if any, sometimes it's blank).
Thanks to Johnny Grass!
I did rails generate mailer CustomerUserMailer
and added
#config/initializers/devise.rb
config.mailer = "CustomUserMailer"
my custom mailer looks like:
# app/mailers/customer_user_mailer.rb
class CustomUserMailer < Devise::Mailer
def headers_for(action)
headers = {
:subject => translate(devise_mapping, action),
:from => mailer_sender(devise_mapping),
:to => resource.email,
:cc => resource.backup_user_email(action),
:template_path => template_paths
}
end
end
Then I moved the 3 mailer templates FROM views/devise/mailer to views/customer_user_mailer (otherwise the emails are empty)
Then I added a method to my User model called backup_user_email() that returns the 'backup' email address (if any) based on the data in the user record and the action. The only "trick" there is that when testing the action it is not action == "confirmation_instructions" it is action == :confirmation_instructions.
Just in case anyone got here through Google - in the latest version of Devise, header_for takes two parameters. So your code would need to be:
class MyMailer < Devise::Mailer
backup_email = "..."
def headers_for(action, opts)
headers = {
:subject => subject_for(action),
:to => resource.email,
:from => mailer_sender(devise_mapping),
:bcc => backup_email,
:reply_to => mailer_reply_to(devise_mapping),
:template_path => template_paths,
:template_name => action
}.merge(opts)
end
end
That might not be the best way to do it, but at least it avoids errors.
One way to do it would be to override the headers_for action in Devise::Mailer
class MyMailer < Devise::Mailer
backup_email = "..."
def headers_for(action)
headers = {
:subject => translate(devise_mapping, action),
:from => mailer_sender(devise_mapping),
:to => resource.email,
:bcc => backup_email
:template_path => template_paths
}
end
And tell devise to use your mailer:
#config/initializers/devise.rb
config.mailer = "MyMailer"
You can use this code which is much cleaner and simpler
# app/mailers/my_mailer.rb
class MyMailer < Devise::Mailer
def headers_for(action, opts)
backup_email = "..."
super.merge!({bcc: backup_email})
end
end
# config/initializers/devise.rb
...
config.mailer = MyMailer
...
With Hash passed to merge! method you can add or modify any email headers you'd like.
Your answer worked for me. Thank you so much.
I had a scenario wherein, i was required to customize devise to:
1) send signup confirmation emails in BCC to different emails based on environment.
2) emails should be added to BCC only for signup confirmation mails.
To achieve that, i compared values for action argument, like shown in the code snippet below:
def headers_for(action)
if action == :confirmation_instructions
if Rails.env.production?
recipient_email = "user1#example.com"
else
recipient_email = "user2#example.com"
end
headers = {
:subject => translate(devise_mapping, action),
:from => mailer_sender(devise_mapping),
:to => resource.email,
:bcc => recipient_email,
:template_path => template_paths
}
else
super
end
end

Devise Invitable : Optionally Send Email

in devise invitable, you can invite a new user by performing:
User.invite!(:email => "new_user#example.com", :name => "John Doe")
What I would like to do is (sometimes) prevent devise invitable from sending out an email. I found the following code in the library:
def invite!
if new_record? || invited?
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
generate_invitation_token if self.invitation_token.nil?
self.invitation_sent_at = Time.now.utc
save(:validate => false)
::Devise.mailer.invitation_instructions(self).deliver
end
end
Any ideas on how to best update that to not send out the email on the last line? I'm not familiar with the ::
thanks
you can use:
User.invite!(:email => "new_user#example.com", :name => "John Doe") do |u|
u.skip_invitation = true
end
or
User.invite!(:email => "new_user#example.com", :name => "John Doe", :skip_invitation => true)
this will skip invitation email.
In your invitations_controller (there should already be one that inherits from Devise::InvitationsController), you can add the following
# this is called when creating invitation
# should return an instance of resource class
def invite_resource
if new_record? || invited?
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
super
end
end
This will override Devise's method for inviting, and then call the original Devise method (super) only if the condition is met. Devise should then handle the token generation and send the invite. You may also want to setup what the app does if the condition is false, in my case that looks like this:
def invite_resource
if user_params[:is_free] == "true"
super
else
# skip sending emails on invite
super { |user| user.skip_invitation = true }
end
end
when params[:is_free] is set to ''true'' the invitation is sent, otherwise the resource is created, but no invitation is sent.
After some digging I found this solution here: https://github-wiki-see.page/m/thuy-econsys/rails_app/wiki/customize-DeviseInvitable

Resources