I have small organizatoric issue, in my application I have 3 mailer User_mailer, prduct_mailer, some_other_mailer and all of them store their views in app/views/user_mailer ...
I will want to have a subdirectory in /app/views/ called mailers and put all in the folders user_mailer, product_mailer and some_other_mailer.
Thanks,
Mailer views are located in the app/views/name_of_mailer_class directory. The specific mailer view is known to the class because its name is the same as the mailer method. In our example from above, our mailer view for the welcome_email method will be in app/views/user_mailer/welcome_email. html.
Action Mailer is the Rails component that enables applications to send and receive emails. In this chapter, we will see how to send an email using Rails. Let's start creating an emails project using the following command. tp> rails new mailtest. This will create the required framework to proceed.
You should really create an ApplicationMailer
class with your defaults and inherit from that in your mailers:
# app/mailers/application_mailer.rb class ApplicationMailer < ActionMailer::Base append_view_path Rails.root.join('app', 'views', 'mailers') default from: "Whatever HQ <[email protected]>" end # app/mailers/user_mailer.rb class UserMailer < ApplicationMailer def say_hi(user) # ... end end # app/views/mailers/user_mailer/say_hi.html.erb <b>Hi @user.name!</b>
This lovely pattern uses the same inheritance scheme as controllers (e.g. ApplicationController < ActionController::Base
).
I so agree with this organization strategy!
And from Nobita's example, I achieved it by doing:
class UserMailer < ActionMailer::Base default :from => "[email protected]" default :template_path => '**your_path**' def whatever_email(user) @user = user @url = "http://whatever.com" mail(:to => user.email, :subject => "Welcome to Whatever", ) end end
It is Mailer-specific but not too bad!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With