Allows your users to edit e-mail templates. With Devise and Active Admin support (but you don't need them to start using email_template).
Add this line to your application's Gemfile:
gem 'email_template'
And then execute:
$ bundle
Or just:
$ gem install email_template
Run installer:
$ rails g email_template:install
In order to use Devise templates you need to install devise wrapper:
$ rails g email_template:devise_install
and update configuration for devise
Devise.setup do |config|
config.mailer = "CustomDeviseMailer"
end
Then generate common devise templates for a specified scope:
$ rails g email_template:devise_templates <devise_scope>
This generator produces email templates with the names:
<devise_scope>_mailer:confirmation_instructions
<devise_scope>_mailer:reset_password_instructions
<devise_scope>_mailer:unlock_instructions
Run:
$ rake db:migrate
You can configure email_template at
config/initializers/email_template.rb
Pull template to the base :
MailTemplate.create(name: "template unique name",
subject: "Join request confirmation",
classes: ["activity_partner"],
body:
<<-TEXT
Dear \#{activity_partner.full_name} ...
....
TEXT
)
In Mailer:
class ActivityPartnerMailer < TemplateSendMailer
def join_confirmation_self(activity_partner)
#send_mail(template_name, mail_params = {}, template_params = {})
send_mail("template unique name", {to: "user@example.com"}, {activity_partner_: activity_partner})
end
end
If you need adding some model method to token list need create in model alias with prefix,
which you set in config(by default is 'et_').
For example if you need add method 'full_name' for 'activity_partner' to token list you need do next:
class ActivityPartner < ActiveRecord::Base
def full_name
[self.first_name, self.last_name].join(' ')
end
alias et_full_name full_name
end
In case you need additional customization :
In Mailer: Simply add 'template_path' and 'template_name'
class MyMailer < TemplateSendMailer
def result(tree)
send_mail('MyMailer:result',
{
to: my_email,
template_path: 'mailers',
template_name: 'mail'
}, {tree: tree})
end
end
In View: In view you will have compiled template in @data variable
= @data.html_safe
In your test_helper.rb add
EmailTemplate.test_mode = true
Test mode creates automatically templates by request and fills body and subject by using the field 'name'. Please notice this by testing email sending in test mode.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request