Pass current_user.id into actionmailer - ruby-on-rails

Im trying to pass the current_employer.id into action mailer. So i cans end an email to users that belong to the Employer.
my mailer looks like this.
class ScheduleMailer < ActionMailer::Base
default to: Proc.new { Employee.where(:employer_id => current_employer.id
).pluck(:email) },
from: 'noreply#scheduled.com'
def schedule_post_reg(employ)
mail( :subject => "Your schedule has been posted.")
end
end
I get this error
NameError (undefined local variable or method `current_employer' for #<ScheduleMailer:0x0000010cec0880>):
app/mailers/schedule_mailer.rb:3:in `block in <class:ScheduleMailer>'
app/mailers/schedule_mailer.rb:10:in `schedule_post_reg'
app/controllers/schedules_controller.rb:235:in `approve_shift'
Any suggestion on how to pass the current_employer.id not the actionmailer would be greatly appreciated.

The Mailer has no direct access to the request context. The reason is because an email is not necessarily a result of an HTTP request. If you deliver the email from the CLI, for instance, there is no HTTP request.
You need to pass the employer as argument of the mailer.
class ScheduleMailer < ActionMailer::Base
default from: 'noreply#usescheduled.com'
def schedule_post_reg(employee, employ)
mail({
:to => employee.email
:subject => "Your schedule has been posted.")
})
end
end
If you need to send the same email to different employees, given a single employer, you can either pass the employer and pluck all the emails into the "to:" field (but all the recipients will see all the other emails)
def schedule_post_reg(employer, employ)
mail({
:to => Employee.where(:employer_id => employer.id).pluck(:email)
:subject => "Your schedule has been posted.")
})
end
or use bcc, or loop all the emails and call the mailer once for every employee email to generate a different email for each employee.

In Controller write somethigng like this
#employer = Employer.find_by_id(params[:id])
ScheduleMailer.schedule_post_reg(employ, #employer).deliver
In Mailer
def schedule_post_reg(employ, employer)
employees_emails = Employee.where(:employer_id => employer.id
).map(&:email).join(",")
mail(:to => employees_emails, :subject => "Your schedule has been posted.")
end

Related

Issue with ActionMailer Mandrill in Rails

I have a cab booking platform created in Rails. I am using the Mandrill smtp settings in production to send booking confirmation mails to users of my platform. Then I generated a mailer called user_mailer with the following code:
class UserMailer < ActionMailer::Base
default :from => "my_company_email"
def booking_confirmation(user)
#user = user
mail(:to => user.email, :subject => "Booking Confirmation")
end
end
Then I created booking_confirmation.html.erb page in user_mailer views with some generic content inside. Finally I called the user_mailer in one of my controllers as follows:
UserMailer.booking_confirmation(current_user).deliver
My problem is that when I want to include more details (such as Travel date, Travel time, etc.) within the mail delivered to my customer. I am trying this within my booking_confirmation.html.erb page: <%= #user.bookings.last.date %> to display the Travel Date to the customer. But this doesn't get displayed. Why is it so?
I would pass in the booking like
UserMailer.booking_confirmation(current_user, booking.id).deliver
then in the class UserMailer I would do
class UserMailer < ActionMailer::Base
default :from => "my_company_email"
def booking_confirmation(user, booking)
#user = user
#booking = Booking.find(booking)
mail(:to => user.email, :subject => "Booking Confirmation")
end
end
now on your erb you should have #booking.date to use
Its not a mandrill issue.something wrong with application code.
Check whether your following code has the date value present for last bookings.
#user.bookings.last.date

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.

Mailer result as String?

In our custom sales app our users are able to send emails based on text partials they choose. We need to record the sent mails in an activity model. How do I get the mailer result as a string in order to save it?
Instead of calling the deliver method to send the mail, you can capture the email message by calling to_s. For example, if you have a mailer:
class MyMailer < ActionMailer::Base
default :from => "sender#me.com"
def my_email
mail(:to => "destination#you.com", :subject => "Mail Subject")
end
end
you would do
mail_content = MyMailer.my_email.to_s
May be you can using a mail observer like in the following example :
class MailObserver
def self.delivered_email(message)
test = Activty.create do |activity|
# etc.
end
end
end
Find here

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

How can I access #user or user from a mailer using Devise and Rails 3?

class JobMailer < ActionMailer::Base
default :from => "emailer#email.com"
def new_job_email_for_client
#
#
#url = "http://simplesite.com/users/login"
mail(:to => #???,
:subject => "You have created a new case on simplesite.")
end
end
I would like each user to receive an email each and every time he/she creates a "job." In other parts of the application, I can access #user and user.email and such, but in the mailer I'm getting "undefined errors."
How can I access the current users email address in the mailer (taking into consideration that Devise is in control of Users)?
I'm not sure if this is a great way of doing it, but this is how I got it working:
def new_job_email_for_client(user_email)
#url = "http://simplesite.com/users/login"
mail(:to => user_email,
:subject => "You have created a new case.")
end

Resources