ApplicationMailer is making CC to all the receivers from the array. - ruby-on-rails

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.

Related

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

Is it possible to replace default from: with an email from database in Rails Action Mailer?

I am trying to pass on an email from a user to the default from: field so that it looks like it's coming directly from them. Here is what I have right now. Is there any way of bringing in a dynamic variable into the default from field?
class IntroMailer < ActionMailer::Base
default from: "Me#gmail.com"
def intro_email(intro)
#intro = intro
mail(to: #intro.person1_email, subject: 'Testing Intro Email')
end
end
You can override this in the Mailer action's mail method:
class IntroMailer < ActionMailer::Base
default from: "Me#gmail.com"
def intro_email(intro, current_user)
mail(to: intro.person1_email, subject: 'Testing Intro Email', from: current_user.email)
end
end
but a WARNING. Email clients, like Google, are pretty smart at detecting spam. If they see that a specific SMTP server is sending out emails with lots of different 'from' attributes, your spam rating will go up and your emails will be filtered out by spam filters. To get around this, choose one or two default from emails (e.g. support#mywebsite.com & jobs#mywebsite.com) that fit the email's type, and then add a dynamic reply_to attribute instead.
class IntroMailer < ActionMailer::Base
default from: "ourteam#oursite.com"
def intro_email(intro, current_user)
mail(to: intro.person1_email, subject: 'Testing Intro Email', reply_to: full_from(current_user))
end
private
def full_from(user)
address = Mail::Address.new user.email
address.display_name = user.full_name
address.format
end
end
Actually not, it doesn't work at all in Rails 5.XX

Choose specific email template with ActionMailer

I have a problem with choosing specific email template. I have the following mailer:
class NewsletterMailer < ActionMailer::Base
def confirmation_email(subscriber)
#subscriber = subscriber
mail(to: #subscriber.email,
subject: t('.confirmation_subject'))
end
end
And two emails templates that are stored in app/views/newsletter_mailer:
confirmation_email.html.erb
confirmation_email.en.html.erb
Is there any way to set in this mailer action to use this: "confirmation_email.en.html.erb"?
Thanks in advance for any help.
Try this:
mail(to: #subscriber.email,
template_name: 'confirmation_email.en.html.erb',
subject: t('.confirmation_subject'))
You may read more at Mailer Views

Rails Actionmailer Sending Multiple Recipients

I'm having trouble getting Rails to send an email to multiple users at once. I am trying to send a notification to multiple venues signed up to my site when an Enquiry that matches them is approved.
A pending Enquiry has to be approved by admin. The mailer is passed the #enquiry, which is when the email is triggered. Shown here in my Enquiries controller:
def approve
#enquiry.approve
redirect_to [:admin, #enquiry], notice: 'Enquiry is approved.'
SupplierMailer.new_enquiry(#enquiry).deliver
end
In my Supplier_mailer.rb, I have this method:
def new_enquiry(enquiry)
#enquiry = enquiry
#enquiry.venues.each do |venue|
mail(to: venue.supplier.user.email, subject: 'You have a new enquiry')
end
end
Currently, it is only sending to 1 email address, so not looping properly.
Models:
Enquiry
has_and_belongs_to_many :venues
Supplier
has_many :venues
has_one :user
What have I done wrong?
Thanks
The new_enquiry method is supposed to build one email, which is then being send with deliver method. The loop work correctly, however every time you're calling mail, you override its previous call, and the method returns the last call.
Instead, first get the list of recipients, and use it as a to attribute
emails = #enquiry.venues.map {|venue| venue.supplier.user.email}
mail(to: emails, subject: 'You have a new enquiry')
If you are not happy with sending other emails to each other, you will need place Mailer action inside the loop:
def approve
#enquiry.approve
redirect_to [:admin, #enquiry], notice: 'Enquiry is approved.'
#enquiry.venues.each do |venue|
SupplierMailer.new_enquiry(#enquiry, venue).deliver
end
end
def new_enquiry(enquiry, venue)
#enquiry = enquiry
mail(to: venue.supplier.user.email, subject: 'You have a new enquiry')
end
Final option is pretty hacky, but provides best interface:
class SupplierMailer << ActionMailer::Base
def self.new_enquiry(enquiry)
#enquiry = enquiry
mails = #enquiry.venues.map do |venue|
mail(to: venue.supplier.user.email, subject: 'You have a new enquiry')
end
class << mails
def deliver
each(&:deliver)
end
end
mails
end

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

Resources