I'm trying to put some logic into my mailer. Basically, I have a dozen boolean fields in one of my models and for each field that is true, I want an email to be sent to a specific address. The model with the boolean fields is called Ecn. This is my notifier file:
class EcnNotifier < ActionMailer::Base
default from: "engineering_notices#wilfley.com"
def submitted(ecn)
#ecn = ecn
#email_list = EmailList.all
if #ecn.distribute_engineering?
mail to: "abc#gmail.com", subject: 'ECN approvald'
else
end
if #ecn.distribute_purchasing?
mail to: "123#gmail.com", subject: 'ECN approval'
else
end
mail to: "aaa#test.com", subject: 'ECN approval'
end
end
And the action I've created in my controller looks like this:
def submit
#ecn = Ecn.find(params[:id])
respond_to do |format|
EcnNotifier.submitted(#ecn).deliver
format.html { redirect_to ecns_url, alert: "Ecn has been submitted for approval." }
format.json { render json: #ecns }
end
end
An email gets sent automatically to aaa#test.com, but regardless of whether or not the distribute_engineering and distribute_purchasing are true, an email is not sent to the other addresses. I'm assuming I'm incorrectly accessing my object instance, but I'm not sure where I'm going wrong.
I thought that you only can send one mail in one mailer action, and not multiple mails. You can send one mail and add a few cc or bcc, but only can send one mail within the scope of one action.
I would change the controller to following:
def submit
#ecn = Ecn.find(params[:id])
respond_to do |format|
EcnNotifier.notify_default(#ecn).deliver
EcnNotifier.distribute_engineering(#ecn).delifer if #ecn.distribute_engineering?
EcnNotifier.distribute_purchasing(#ecn).deliver if #ecn.distribute_purchasing?
# and so on...
format.html { redirect_to ecns_url, alert: "Ecn has been submitted for approval." }
format.json { render json: #ecns }
end
end
And your EcnNotifier mailer:
class EcnNotifier < ActionMailer::Base
default from: "engineering_notices#wilfley.com"
def notify_default(ecn)
#ecn = ecn
#email_list = EmailList.all
mail to: "aaa#test.com", subject: 'ECN approval'
end
def distribute_engineering(ecn)
#ecn = ecn
#email_list = EmailList.all
mail to: "abc#gmail.com", subject: 'ECN approval'
end
def distribute_purchasing(ecn)
#ecn = ecn
#email_list = EmailList.all
mail to: "abc#gmail.com", subject: 'ECN approval'
end
end
Btw: if you are sending multiple mails, you should do that via delayed job gem or something like that. Otherwise the user has to wait quite a long time, because the ruby process will be blocked during sending all the mails. the delayed job gem is available on github: https://github.com/collectiveidea/delayed_job
You can only send one mail with one call of deliver. You have to move your logic outside the mailer or use cc or bcc to deliver the mail to multiple recipients.
Related
Hi I'm making user register application by ruby on rails.
But ActionMailer process ignore my code.
When I try to use
UserMailer::confirmation_email.deliver
on rails s, I could send email.
But when I use bellow process, UserMailer::confirmation_email is ignored.
Do you know how to resolve it?
user_controller.rb
class UsersController < ApplicationController
skip_before_action :verify_authenticity_token
def create
user = User.new(user_params)
result = Users::RegisterService.register_user(user)
render result
end
end
register_service.rb
module Users
module RegisterService
module_function
def register_user(user = {})
if user.save
UserMailer::confirmation_email(user)
result = { json: {status: 'User created successfully'}, status: :created }
else
result = { json: { errors: user.errors.full_messages }, status: :bad_request }
end
return result
end
end
end
user_mailer.rb
class UserMailer < ApplicationMailer
default from: 'email#gmail.com'
def confirmation_email(user)
#user = user
#url = 'http://192.16.8.33.10/user/confirm?confirmation_token=' + #user.confirmation_token
mail(
to: #user.email,
subject: 'Welcome to My Awesome Site',
template_name: 'user_confirm'
)
end
end
Call deliver_now to send email right away and deliver_later to send email asynchronously.
UserMailer::confirmation_email(user).deliver_now
I am very much a rails novice!
I am trying to write a method for a kind of on-line committee meeting. There are a fixed number(9) of users. When a user proposes a topic for discussion and/or voting the submit button needs to send an email to all members.
in app/mailers/user_mailer.rb I have:-
class UserMailer < ApplicationMailer
def new_topic_alert(topic)
#users = User.all
#users.each do |user|
mail to: user.email, subject: "New topic alert"
end
end
end
as part of app/controllers/topics_controller.rb I have:-
def send_alert
#topic = Topic.new(topic_params)
UserMailer.new_topic_alert(#topic).deliver_now
end
and:-
def create
#topic = Topic.new(topic_params)
if #topic.save
send_alert
flash[:info] = "New Topic alert emails sent."
redirect_to root_url
else
render 'new'
end
end
Please, why does the loop in user_mailer only send an email to the final person of the list. By incorporating "byebug" I have shown that it goes through all the user emails.
Try like below:
def send_alert
#topic = Topic.new(topic_params)
users = User.all
users.each do |u|
UserMailer.new_topic_alert(#topic, u).deliver_now
end
end
and update the mailer like
class UserMailer < ApplicationMailer
def new_topic_alert(topic,user)
mail to: user.email, subject: "New topic alert"
end
end
I am trying to development this app that has a landing page where i can collect email addresses, log in as an admin and send mail to all the subscribed users, i am using Action mailer and my gmail account smtp configuration.
When i do send the mail, everyone gets cc'ed, and seeing as i am testing with my own google mail accounts, i can see the other people cc'ed.
Mailforsubcriber Controller
def create
#mailforsubscriber = Mailforsubscriber.new(mailforsubscriber_params)
respond_to do |format|
if #mailforsubscriber.save
RecipientMailer.newsletter(#mailforsubscriber).deliver_now
format.html { redirect_to #mailforsubscriber, notice: 'Mail for subscriber was successfully sent.' }
format.json { render :show, status: :created, location: #mailforsubscriber }
else
format.html { render :new }
format.json { render json: #mailforsubscriber.errors, status: :unprocessable_entity }
end
end
end
This is the recipient mailer code
class RecipientMailer < ApplicationMailer
require 'digest/sha2'
default from: "notification#example.com"
default to: Proc.new {Subscribeduser.pluck(:email) }
default "Message-ID" => "#{Digest::SHA2.hexdigest(Time.now.to_i.to_s)}#domain.com"
def newsletter(mailforsubscriber)
#mailforsubscriber = mailforsubscriber
mail(subject: "Newsletter")
end
end
how do i get around this?.
try use the CCO field, it's works like CC field, but don't show the other e-mails for everyone.
You can use something like this in your action mailer,
def newsletter(mailforsubscriber)
#mailforsubscriber = mailforsubscriber
mail(:to => #mailforsubscriber.email, :subject => "Newsletter")
end
Here i have called the method send_mail_persons and passed the recipients info as a parameter.This logic will make you loop across all the email ids and then send individually. If you want to send mail to all the users at once then you can use :bcc, say
mail(:to => "#mailforsubscriber.email" , :subject => "Example Subject",
:bcc => ["bcc1#abc.com", "bcc2#abc.com"])
If you are new to Rails, i would suggest to read this Action Mailer Basics. This covers end to end flow of a basic mailer template integration from controller to view.
i have a rails model registrations that has the following fields
attr_accessible :address, :company, :name, :phone, :email
i have successfully been able to send a mail to the user via the email fielded in by the user and that works succesfully using action mailer
def create
#registration = Registration.new(params[:registration])
respond_to do |format|
if #registration.save
UserMailer.registration_confirmation(#registration).deliver
format.html { redirect_to root_path, notice:" Thanks! #{#registration.name}, Your registration have been
confirmed & your seat reserved" }
format.json { render :show, status: :created, location: #registration }
else
format.html { render action: "new" }
format.json { render json: #registration.errors, status: :unprocessable_entity }
end
end
end
and the registration mailer is as thus
def registration_confirmation(registration)
#registration = registration
#greeting = "Hi"
mail(to: #registration.email, subject: 'Welcome')
end
which works very well...
All i want to achieve is to be able to send a mail to another email address e.g (admin#gmail.com) stating that a user as registered and also showing the registration details ... thanks
I would generate a new mailer specifically for notifications that should be sent to the administrator.
rails g mailer AdminMailer registration_notice
You could then edit the AdminMailer registration_notice to be similar to your UserMailer, but with a different recipient:
def registration_notice(registration)
#registration = registration
mail(to: 'admin#gmail.com', subject: 'A new user has registered')
end
Put whatever registration details you would like to include into the views for registration_notice.html.erb (or text).
Then just add the call to the mailer in the create action of the controller, right after the call to the UserMailer:
def create
#registration = Registration.new(params[:registration])
respond_to do |format|
if #registration.save
UserMailer.registration_confirmation(#registration).deliver
AdminMailer.registration_notice(#registration).deliver
# etc
end
end
end
You'd probably also want to consider sending the mails in the background instead of making the user wait for the create request to finish, but that's beyond the scope of this question.
I am attempting to send a confirmation email to a newly registered user using Rails 4.0.1 and Ruby 2.0.0.
I am not getting any errors but the mail just is not sending. Here is the relevant code:
config/environments/development.rb
...
config.action_mailer.smtp_settings = {
:authenication=>:plain,
:address=>"smpt.mailgun.org",
:port=>587,
:domain=>"sandboxf4f4c96ebc7b4eb1b6c7475ad4de048c.mailgun.org",
:user_name=>"postmaster#sandboxf4f4c96ebc7b4eb1b6c7475ad4de048c.mailgun.org",
:password=>"6j3c9l35tu33"
}
app/model/user.rb
...
def create
#user=User.new(user_params)
if #user.save
ModelMailer.account_activation(#user).deliver
redirect_to lessons_url
else
render :new
end
end
mailers/model_mailer.rb
class ModelMailer < ActionMailer::Base
default from: "me#sandboxf4f4c96ebc7b4eb1b6c7475ad4de048c.mailgun.org"
def account_activation(user)
#user = user
mail to: "myemail#gmail.com", subject: "Account Activation"
end
end
Any help would be appreciated.