I will preface this saying that I know almost nothing about rails. But I am trying to fix this issue involving rails so any help would be greatly appreciated (and also, if you could dumb it down for me that would be great!)
We have a rails email notification set up for two of our sites. If a user fills out an application on our English site, then a notification email is sent to person A. If a user fills out an application on our French site, then a notification email is sent to person B.
However, at the moment it seems like all emails are going to person A regardless of whether an application is filled out on the English or French site. How can I fix this?
Here is the code from the admin_mailer.rb file:
class AdminMailer < ActionMailer::Base
default from: 'noreply#canwise.com'
def contact_email
#contact = Contact.last
mail to: 'email1#1test.com'
end
def application_email
#application = Application.last
mail to: 'email1#test.com'
end
def eps_contact_email
#contact = Contact.last
mail to: "email2#test.com"
end
def eps_application_email
#application = Application.last
mail to: 'email2#test.com'
end
def salesforce_application_failure(application)
subject = "Application #{application.id} submitted by #{application.firstName} #{application.lastName} failed to submit to salesforce."
mail(to: 'test#test.com', subject: subject) do |format|
format.text { render text: '' }
end
end
end
And here is the code from the application.rb file:
def email_notification
if provider == 'www.frenchsite.com'
AdminMailer.eps_application_email.deliver
else
AdminMailer.application_email.deliver
end
end
HELP PLEASE!
If the emails are all going to email1#test.com, then it only means that your provider for french is not matching 'www.frenchsite.com'. The 'if' statement is always resulting in false.
Related
I am trying to send mail to different users based on the user role of who is logged in on my site. I cannot seem to figure out a proper way to do it. I am using letter_opener gem to test on a dev environment, it seems this logic is not working. Can anyone please help me figure out how to make this logic work?
class ContactFormsMailer < ActionMailer::Base
default from: ENV["DEFAULT_FROM_EMAIL"]
def send_mail(contact_form)
#name = contact_form.name
#email = contact_form.email
#phone = contact_form.phone
#message = contact_form.body
#postcode = contact_form.postcode
if User.find_by_role('key_account')
mail(to: ENV["CONTACT_FORM_KAM"], subject: build_subject(contact_form))
else
mail(to: ENV["CONTACT_FORM_EMAIL"], subject: build_subject(contact_form))
end
end
def build_subject(contact_form)
if contact_form.body.include?("Calibration Certificate request:")
title = "CERTIFICATE REQUEST"
else
title = "#{ENV['COUNTRY_CODE']}: CONTACT FORM"
end
"#{title} - #{contact_form.email}"
end
end
All sorted. I just passed in the current user to the controller and used that on my mailer to check the currently logged-in user.
I have managed to configure my ActionMailer to send emails to recipients based on a new 'submission'. However, the way my app works is that it takes the submissions 'Desired Location' field and matches it up to the 'Company Business location' field in another model called Agents to give an index view that is matched by location depending on user. i.e if i submit a submission with a location of london, then only agents with a location of london will be able to see it. Which brings me to my emails, is there anyway to create a mailer that works in the same way? So only send emails to agents who match the desired location of the submission?
Mailer
class NewSubmissionMailer < ApplicationMailer
def submission_email(submission)
#submission = submission
mail(to: #submission.Email, subject: 'Welcome to Ottom8')
end
end
Submissions Controller
respond_to do |format|
if #submission.save
# Tell the UserMailer to send a welcome email after save
NewSubmissionMailer.submission_email(#submission).deliver_now
Code to match both models
def index
#submissions = Submission.where(:Desired_Location => current_agent.Company_Business_Location)
end
Thanks
respond_to do |format|
if #submission.save
# Tell the UserMailer to send a welcome email after save
NewSubmissionMailer.submission_email(#submission).deliver_now
# Send emails to matching agents
NewSubmissionMailer.matching_agents_email(#submission).deliver_now
and then in the mailer ::matching_agents_email:
def matching_agents_email(submission)
#submission = submission
agents = Agent.where(:Company_Business_Location => #submission.Desired_Location)
mail(to: agents.pluck(:email) # ... Rest of email logic. )
I am on Ch 20 of the Learn Rails tutorial 'Send Mail'. There is a create contact form and a notification email is supposed to be sent to my email address. I have set up configuration as per the book and the email is being generated correctly i the terminal log. however the email never sends to my email inbox. I am getting an internal server error. see below for details.
Completed 500 Internal Server Error in 30831ms
Net::OpenTimeout (execution expired):
app/controllers/contacts_controller.rb:10:in `create'
contacts_controller.rb
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(secure_params)
if #contact.valid?
UserMailer.contact_email(#contact).deliver_now
flash[:notice] = "Message sent from #{#contact.name}."
redirect_to root_path
else
render :new
end
end
private
def secure_params
params.require(:contact).permit(:name, :email, :content)
end
end
user_mailer.rb
class UserMailer < ApplicationMailer
default from: "do-not-reply#example.com"
def contact_email(contact)
#contact = contact
mail(to: Rails.application.secrets.owner_email, from: #contact.email, :subject => "Website Contact")
end
end
i also have my email address set in the config/secrets.yml file like so :
owner_email: <%= ENV["my_email_address#*******.com"] %>
I have also added the following to my .bashrc file as per the early chater of the book about configuration:
export SENDGRID_USERNAME="user_name"
export SENDGRID_PASSWORD="password"
export MAILCHIMP_API_KEY="long_random_api_key_here"
export MAILCHIMP_LIST_ID="list_id_here"
export OWNER_EMAIL="**********#*********.com"
so as far as i see i have set everything up according to the tutorial but the mail is not sending. any ideas why?
From the above share code description and log trace, it seems like mailer settings is not configured properly.
Note: You need to set the mailer settings based on the environment on which you are working(eg: development.rb)
Follow the below given link for the configuration of mailer settings:
http://guides.rubyonrails.org/action_mailer_basics.html#example-action-mailer-configuration
I am trying to implement a feature which allows user to send one email to multiple recipients. I split the recipient email params by calling the .split method which turns it into an array. Then I loop through the array with the each method which should apply the mail method to each element in the array. The controller and mailer code is given below.
controller code
def create
#scoreboard = Scoreboard.find(params[:scoreboard_id])
#invitation = #scoreboard.sent_invitations.build(invitation_params)
if #invitation.save
UserMailer.registered_invitation_email(#scoreboard, #invitation).deliver_now
flash[:success] = "Invitation sent successfully"
redirect_to new_scoreboard_invitation_path
else
render 'new'
end
end
end
mailer code
def registered_invitation_email(scoreboard, invitation)
#scoreboard = scoreboard
#invitation = invitation
split_email = #invitation.recipient_email.split
split_email.each do |email|
mail(to: email, subject: "View scoreboard")
end
end
The problem is that it only sends the email to the last element in the array. For example, if the user types in "joe#example.com Mike#example.com" in the form it will only send an email to the last element in an array. In this case, Mike#exapmle.com. I am not sure why that is. I think I am looping through it correctly. I am not sure what's causing the problem. I am not sure if its the loop or maybe loops don't work in active mailer. Not exactly sure. Any help would be appreciated. Thanks!!
Try something like this
Create a model with fields
field :mail_to
field :subject
create a method in the same model
def deliver
UserMailer.send_invitation_mail(self).deliver
end
Mailer
def send_invitation_mail(email)
mail(to: email.mail_to, subject: email.subject)
email.destroy
end
Controller
def create
user_emails.each do |email|
email = Email.new(:mail_to => "xxx#yyy.com",:subject => "View Scoreboard") # Email is the model name.
email.deliver
sleep(1)
end
end
This way u can also log and also deliver all mails together in the model and also do various manipulations on the email.
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