Mailer result as String? - ruby-on-rails

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

Related

ApplicationMailer is making CC to all the receivers from the array.

I am trying to send bulk emails to a bunch of receivers. The email is being delivered to the expected receivers but it is CCing all the receivers. I dont't want receivers to be able to see other receivers emails. I might be doing it wrong. Below is my ruby method in ApplicationMailer.
class WantedEquipmentMailer < ApplicationMailer
def sendmail
#receiver = WantedEquipment.where(sub_category_id: "#{a}", status: 2).pluck(:email)
mail(to: #receiver, subject: #subject)
end
end
Equipment.rb
def email_newequip_matches_wanted
WantedEquipmentMailer.sendmail.deliver
end
What changes should I make so that it wont cc all the receivers stored in that array (#receiver). ?
You can refer to something like this which i just pulled from the docs. Action Mailer classes
class NotifierMailer < ApplicationMailer
default from: 'no-reply#example.com',
return_path: 'system#example.com'
def welcome(recipient)
#account = recipient
mail(to: recipient.email_address_with_name,
bcc: ["bcc#example.com", "Order Watcher <watcher#example.com>"])
end
end
Send across the mails with :bcc the way it is done in basic mail clients.

Sending Email Messages exposes the email addresses of every user to the recipients. How to fix?

I like to send mails such that my JobNotifier/Mailer iterates through the Subscriber's Email List and call deliver "n" times, if that could be the solution to my problem.
Unfortunately, all I have done sends Emails Messages and expose the email addresses of every user to the recipients, which is not suppose to be.
Here are my codes
create method right inside my jobs_controller.rb
def create
#job = Job.new(job_params)
if #job.save
# Deliver the Posted Job
JobNotifier.send_post_email(#job).deliver
redirect_to preview_job_path(#job)
else
render :new
end
end
app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default to: Proc.new { User.pluck(:email).uniq },
from: 'FarFlungJobs <no-reply#farflungjobs.com>'
layout 'mailer'
end
app/mailers/job_notifier.rb
class JobNotifier < ApplicationMailer
def send_post_email(job)
#jobs = job
mail( :subject => 'New job posted on FarFlungJobs'
)
end
end
test/mailers/preview/job_notifier_preview.rb
# Preview all emails at http://localhost:3000/rails/mailers/job_notifier
class JobNotifierPreview < ActionMailer::Preview
def send_post_email
user = User.all
JobNotifier.send_post_email(user)
end
end
Tried to hop on my browser to test my Mailer using the URL shown below to preview/test my mailer:
http://localhost:3000/rails/mailers/job_notifier/send_post_email
Outcome of my test is this image below (at least if needed to help me with my problem):
Am using Rails
4.2.1
You have to send the email to each user separately. It will take much longer but it won't show other user's emails.
So in your controller, you will have something like this:
def create
#job = Job.new(job_params)
if #job.save
User.pluck(:email).uniq.each do |email|
# Deliver the Posted Job
JobNotifier.send_post_email(#job, email).deliver
end
redirect_to preview_job_path(#job)
else
render :new
end
end
Or you could put the loop inside the mailer
Edit:
You'll need to change your mailer to be able to handle extra argument:
class JobNotifier < ApplicationMailer
def send_post_email(job, email)
#jobs = job
mail(:to => email :subject => 'New job posted on FarFlungJobs')
end
end

Pass current_user.id into actionmailer

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

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.

Resources