EmailTemplate
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).
Installation
Add this line to your application's Gemfile:
gem 'email_template'And then execute:
$ bundleOr just:
$ gem install email_templateUsage
Run installer:
$ rails g email_template:installIn order to use Devise templates you need to install devise wrapper:
$ rails g email_template:devise_installand update configuration for devise
Devise.setup do |config|
config.mailer = "CustomDeviseMailer"
endThen 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_instructionsRun:
$ rake db:migrateYou 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
endConfiguration
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
endCustomization
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
endIn View: In view you will have compiled template in @data variable
= @data.html_safeTesting
In your test_helper.rb add
EmailTemplate.test_mode = trueTest 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.
Contributing
- 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