All of my users will be unapproved until they are approved by an admin, the admin will be logging into the site to mark the user as approved. I am following the Devise docs here which is working out great but how do I send an email to the admin once a new user has signed up so that the admin is aware and can approve the sign up?
How about in your User model, do something like this:
after_create :send_admin_mail
def send_admin_mail
###Send email stuff here
end
You may want to use ActionMailer.
There may be some built in Devise way, but I can't find anything. This basically just sends an alert to you.
As of 2020
The docs have been updated to answer this question here. Of course feel free to modify to your needs.
Summary:
After running rails g mailer AdminMailer. Modify app/mailers/admin_mailer.rb to the following:
class AdminMailer < Devise::Mailer
default from: 'from#example.com'
layout 'mailer'
def new_user_waiting_for_approval(email)
#email = email
mail(to: 'admin#email.com', subject: 'New User Awaiting Admin Approval')
end
end
Then update app/models/user.rb to:
after_create :send_admin_mail
def send_admin_mail
AdminMailer.new_user_waiting_for_approval(email).deliver
end
Lastly, modify the following to your needs views/admin_mailer/new_user_waiting_for_approval.erb.
Related
In my application I have users and sites (think of it like users and organizations). There's a lookup table the sits between them called SiteUser. In SiteUser:
belongs_to :site
belongs_to :user
before_validation :set_user_id, if: ->() { email != nil }
def set_user_id
existing_user = User.find_by(email: email)
self.user = if existing_user.present?
UserMailer.notify_existing_user(site, existing_user).deliver_now unless Rails.env.test?
existing_user
else
User.invite!(email: email)
end
end
I need the subject of the email that gets generated to include the site name. "Example Company has invite you to join their site!"
And in the email body I also need the site title.
I am confused on how to get the site parameter over to devise invitable. As you can see in the code above, if the user who is being invited to the site already exists in our system, I use my own mailer in which I pass in the site and the existing_user so I have access to it in my mailer view.
class UserMailer < ApplicationMailer
def notify_existing_user(site, user)
#site = site
#user = user
mail to: #user.email, subject: "You've been given access to #{#site.title} in the Dashboard."
end
end
I cannot figure out how to accomplish this similarly with the devise invitable mailer, which is used when the user doesn't already exist in the system.
Any help you could provide would be incredibly appreciated!
The way I went about accomplishing this was to add a temporary/virtual attribute on User called invite_site_name. Because the problem was that site wasn't an attribute of User, so I could not send site in to User.invite!.
class User < ActiveRecord::Base
attr_accessor :invite_site_name
and then in my CustomDeviseMailer I had access to it:
class CustomDeviseMailer < 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
def invitation_instructions(record, token, opts={})
opts[:subject] = "#{record.invite_site_name} has invited you to their Dashboard! Instructions inside!"
super
end
end
You could always override devise's subject_for method. Found this on the gems issues, that also suggests another way:
https://github.com/scambra/devise_invitable/issues/660#issuecomment-277242853
Hope this helps!
I have a Rails 4 app. It is working with devise 3.2.3. devise is properly integrated. At this point, users can register with email and password, sign in and perform CRUD operations.
Now here is what I would like to do: Instead of having any user to sign up by themselves, I want to create an admin. The admin would retain the responsibility of creating users. I don't want users to sign up by themselves. Basically the admin will create the user, issue them their log-in credentials, and email it to them.
I read this post and similar ones in SO and in devise wikis to no avail.
I have added a boolean field to users table to identify admin users.
class AddAdminToUser < ActiveRecord::Migration
def change
add_column :users, :admin, :boolean, :default => false
end
end
I have read about managing users using cancan but I don't know how to use it to achieve my objective
The solution i'm looking for would probably require a combination of devise and cancan.
I would appreciate any guidance on this matter.
Make sure that the boolean :admin is not in your params.permit() area for strong parameters.
Use the pundit gem, it is maintained and pretty much plain old ruby objects.
Then in your UserPolicy you would do something like this
class UserPolicy < ApplicationPolicy
def create?
user.admin?
end
end
And your model would look something like this
class User < ActiveRecord::Base
def admin?
admin
end
end
Last in your controller you make sure that the user is authorized to do the action
class UserController < ApplicationController
def create
#user = User.new(user_params)
authorize #user
end
end
You would probably also want to restrict the buttons that are shown that would give access to the admin user creation section. Those can be done with pundit as well.
I'm trying to follow this wiki to have the admin approve requests for registration.
https://github.com/plataformatec/devise/wiki/How-To%3a-Require-admin-to-activate-account-before-sign_in
When I try to complete the sign up form, I get this error when I press the sign up button:
NameError at /users
uninitialized constant User::AdminMailer
It refers to line 96 in my user model. That is where this method is:
def send_admin_mail
AdminMailer.new_user_waiting_for_approval(self).deliver
end
I have a after action for send_admin_email.
class UserMailer < ActionMailer::Base
default from: "hello#cr.com"
def send_admin_mail
mail(to: hello#cr.com, subject: 'Registration Request')
end
end
Any ideas as to what I'm doing wrong?
Thank you.
your class is called UserMailer, but you're creating an instance of AdminMailer. Maybe try renaming one or the other. The tutorial suggests the class should be called AdminMailer.
I am building an rails based e-commerce application first time. I want to send an email to user when he submits the order, like the way we get receipt on email when we place order on e.g. mealnut.com or fab.com. I was searching for tutorials but not getting related to order submit emails. Every where user sign up or reset etc.
Has any one implemented it? or know any resource/tutorial in rails?
Your guidance/help will be appreciated!
First generate the mailer for writing required actions.
rails g mailer UserMailer
then in app/mailers/user_mailer.rb file
class UserMailer < ActionMailer::Base
default from: 'notifications#example.com'
def order_confirmation(user, order)
#user = user
#order = order
mail(to: user.email, subject: 'Order has been received')
end
end
and the view content for the email would be like this app/views/user_mailer/order_confirmation.html.erb
Hi <%= #user.name %>
You have successfully placed an order with us.
Please find the details of order....
#.............
then in the controller action, where you will create a order, place the below line after creating the order to send an email
UserMailer.order_confirmation(user, order).deliver
Go through the action mailer tutorial for more information.
I'm afraid sending an email is a fairly standard procedure.. and the tutorials you've found are probably applicable. You need to understand that triggering a message to be sent can be done from any controller action.. in your case you'll want the order create action.
After reading the following:
http://railscasts.com/episodes/61-sending-email-revised?view=asciicast
You can make the necessary changes and call the mailer from your order create action:
class OrdersController < ApplicationController
def create
#order = Order.new(params[:order])
if #order.save
UserMailer.order_confirmation(#order, #user).deliver
redirect_to #user, notice: "Order Completed Successfully."
else
render :new
end
end
end
The reason I am using UserMailer in the above is because you will likely want to set up a mailer that sends messages to Users, but you could call it OrderMailer if you wanted.
You cannot/not suitable use Devise mailer (since devise mailer is for User authentication purposes). What you could do is, you could use a observer class to send e-mails
Ex:
class Order < ActiveRecord::Base
#your standard order code
end
class OrderObserver < ActiveRecord::Observer
def after_create(order)
#Email sending code
end
end
This OrderObserver sends an email when a Order#create is finished. Read more about observer class
Regarding sending email with rails3 check this, and its same as sending emails for forgotpassword / signup etc, it's just that content is different.
How can I send a welcoming email to the user when they sign up? I'm using the Devise gem for authentication. SMTP is already set up. I just need to understand how to extend devise to send emails.
NOTE - this is not confirmation email!
UPD Solution:
class User < ActiveRecord::Base
after_create :send_welcome_email
private
def send_welcome_email
UserMailer.deliver_welcome_email(self)
end
end
Add a callback (after_create ) in the model or observer to send the email using normal mailer methods.
FYI, in Rails 3 it's:
class User < ActiveRecord::Base
after_create :send_welcome_email
private
def send_welcome_email
UserMailer.welcome_email(self).deliver
end
end