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
Related
In my Rails 5 app I am trying to send emails with ActiveJob and Sucker Punch:
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def activate
user = User.find_by(:activation_token => params[:id])
user.activate
SendFollowUpEmailJob.perform_in(30, user)
sign_in user
end
end
# app/jobs/send_follow_up_email.rb
class SendFollowUpEmailJob < ActiveJob::Base
include SuckerPunch::Job
queue_as :default
def perform(user)
SupportMailer.follow_up(user).deliver_later
end
end
# app/mailers/support_mailer.rb
class SupportMailer < ApplicationMailer
layout nil
default :from => "admin#app.com"
def follow_up(user)
#user = user
mail :to => #user.email, :subject => t('.subject')
end
end
# app/views/user_mailer/activation.html.erb
<%= link_to(t('views.activate'), activate_user_url(#user.activation_token, :locale => "#{I18n.locale}")) %>
Everything works except that the email is always sent in the default locale rather than the locale that was passed into the activate action.
All emails throughout the application are always sent in the correct locale, so it must down to either ActiveJob or SuckerPunch.
If you can help, please let me know. Thanks.
I'm familiar with I18n within the application, but not so much in mailers. Remember that the mailer is being sent outside the request (ActiveJob), so it has no knowledge of anything that happened there. It looks like you haven't done anything to tell the mailer that it should be sending in a specific locale....
OK maybe this has the answer for you (and me!) https://niallburkley.com/blog/localize-rails-emails/
I18n.with_locale(#user.locale) do
mail(
to: #user.email,
subject: I18n.t('user_mailer.new_follower.subject')
)
end
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
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.
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
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