I am iterating over an array of subscriber ids to send each subscriber an email, but for some reason not all subscribers are getting the email.
Here is the controller calling the model method:
class Admin::EmailDigestsController < ApplicationController
def send_digest
article_ids = params[:article_ids]
subject = params[:subject]
EmailDigest.send_email_digest(article_ids, subject)
redirect_to email_digests_path
end
end
And here is email_digest.rb:
class EmailDigest < ActiveRecord::Base
def self.send_email_digest(article_ids, subject)
subscribers = EmailDigest.where.not(status: "Inactive").ids
subscribers.each do |subscriber|
DigestMailer.weekly_digest(subscriber, article_ids, subject).deliver_later
end
end
end
And here is digest_mailer.rb:
class DigestMailer < ActionMailer::Base
default from: "secret#secret.com"
def weekly_digest(subscriber, article_ids, subject)
#subscriber = EmailDigest.find_by_id(subscriber)
#subject = subject
articles_arr = []
article_ids.each do |f|
articles_arr << Article.find_by_id(f)
end
#articles = articles_arr
mail(to: #subscriber.email,
subject: subject)
end
end
I also have sidekiq set up and it works sufficiently to send at least one email, but perhaps there is something wrong with the way errors are handled.
Thanks for any help
Related
good day!
I have an application mailer and I want to call the application record to put it on the mailer. Is it possible to do this?
class UserMailer < ApplicationMailer
def verification_email
#user = params[:user]
#token = #user.verification_token
email_template = EmailTemplate.where(category_id: 1)
#subject = email_template.subject
#greetings = email_template.greetings
#content = email_template.content
#closing = email_template.closing
mail(to: #user.email, subject: #subject)
end
end
EmailTemplate is the Application record that I want to call.
It should work but you should get a specific template, right now you are getting an ActiveRecord::Relation
change EmailTemplate.where(category_id: 1)
to
EmailTemplate.where(category_id: 1).first or EmailTemplate.find_by(category_id: 1)
Im getting an error when trying to send an email. Not to sure why but here is my code in my controller and mailer
Here is my controller code below
class Invitation::InvitesController < ApplicationController
def invite_provider
#patient = Patient.find_by_id(invite_params[:invitable_id])
recipient = params[:email]
InviteMailer.provider_invite(recipient).deliver_now
flash[:success] = "An email has been sent to"
redirect_back(fallback_location: root_path)
end
end
Here is my mailer code
class InviteMailer < ApplicationMailer
def provider_invite(recipient)
#recipient = recipient
mail(
to: recipient[:email],
subject: I18n.t('provider_invite_subject')
)
end
end
At the call to mail, in the to option, you're basically sending params[:email][:email]. I don't think that's what you want.
recipient = params[:email]
and then
to: recipient[:email],
You have called InviteMailer.provider_invite(recipient) where recipient is email inside controller.
You can change in controller,
InviteMailer.provider_invite(email: recipient)
and then,
class InviteMailer < ApplicationMailer
def provider_invite(attr)
#recipient = attr[:email]
mail(
to: #recipient,
subject: I18n.t('provider_invite_subject')
)
end
end
And error is due to your params o not have email key, so recipient is nil passed through controller
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 write mailer methods with define_method,because i need almost similar methods,like
notify_user_approved & notify_user_rejected
%w(approved rejected).each do|meth|
define_method("notify_user_#{meth}") do |user,subject|
#url = user.email
#user = user
mail(to: #url, subject: subject)
end
end
Notification is my mailer class name
Notification.notify_user_rejected(User.last,'approved').deliver_now
i having NoMethodError: undefined methodmail' for Notification:Class
when to try run my mailer
whole class
class Notification < ActionMailer::Base
%w(approved rejected).each do|meth|
define_method("notify_user_#{meth}") do |user,subject|
#url = user.email
#user = user
mail(to: #url, subject: subject)
end
end
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.