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.
Related
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 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.
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
I am new to Rails and struggling with how to implement dynamic values in my mailer.
The below code all works fine apart from the reply_to which I want a dynamic value but I don't know how to do this.
The params #name, #email, #message are captured on a form and I want the reply_to values to be the same as the params passed from #email.
So, essentially the point of this is someone can book an event and then it will email their details to the event manager who can then just press "reply" and it will reply back to the email the user filled out on the form.
class BookingMailer < ActionMailer::Base
default from: "notifications#example.com"
default reply_to: #email
def contact_mailer(name,email,message)
#name = name
#email = email
#message = message
mail(to: 'info#example.com', subject: 'Event Booking', reply_to: #email)
end
end
I looked on the API docs but they seem to reference users in a database when using dynamic values.
Any help is much appreciated, thanks!
You only set a default value if you wanted to use something for methods that DON'T have an option set (for example, you've set a default from:, so you don't need to set mail(from: "notifications#example.com"...) each time, it'll use the default if not set).
Providing your controller is passing the email address as the second argument then your code should work, although usually I'd pass a booking:
class BookingController < ApplicationController
def create
#booking = Booking.new(safe_params)
if #booking.save
BookingMailer.contact_mailer(#booking).deliver
else
# alarums!
end
end
end
Then extract the info you want from that in your mailer:
class BookingMailer < ActionMailer::Base
default from: 'blah#blah.com'
def contact_mailer(booking)
#booking = booking
mail(to: 'info#example.com', subject: 'Event booking', reply_to: #booking.email)
end
end
Ideally you should remove the line default reply_to: #email as it's trying to use a class level instance variable #email, instead of the instance variable #email that you intended. For examples on the differences between class variables, instance variables and class instance variables, see here: http://www.railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/
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