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
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)
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
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
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 trying to let a user submit a form that sends an email message to another user. Everything is working fine, except I am trying to store the body of the email in a variable to display in the view for the mailer and am getting 'nil'. I set up a user_mailer.rb:
class UserMailer < ActionMailer::Base
default :from => "myemail#gmail.com"
def message_user(user, recipient, subject, body)
#user = user
#recipient = recipient
#body = "#{body}"
mail(:to => "#{recipient.name} <#{recipient.email}>", :subject => "Reelify message: #{subject}" )
end
end
and here is my messages_controller.rb :
def new
#user = current_user
if !params[:receiver].blank? and !params[:subject].blank? and !params[:body].blank?
#recipient = User.find_by_name(params[:receiver])
UserMailer.message_user(#user, #recipient, params[:subject], params[:body]).deliver
flash[:notice] = "This flash means it should work.. #{#user.name} #{#recipient.name}"
end
end
When I call #body in the view, it is blank (#body.inspect) results in 'nil'. However, if I do something like: #{body} in place of the subject line, the text will render fine.
Anyone have any clue whats going on?
how about # user, is it displayed correctly? May be #body is used by rails, try rename.