Effective Email Templates
Create email templates that an admin can edit and then send.
Rails 3.2.x through Rails 6
effective_email_templates 1.0
This is the 1.0 series of effective_email_templates.
This requires Twitter Bootstrap 4.
Please check out Effective Email Templates 0.x for more information using this gem with Bootstrap 3.
Getting Started
Add to your Gemfile:
gem 'haml-rails' # or try using gem 'hamlit-rails'
gem 'effective_email_templates'
Run the bundle command to install it:
bundle install
Then run the generator:
rails generate effective_email_templates:install
The generator will install an initializer which describes all configuration options and creates a database migration.
If you want to tweak the table name (to use something other than the default 'email_templates'), manually adjust both the configuration file and the migration now.
Then migrate the database:
rake db:migrate
And import the provided welcome email template:
rake effective_email_templates:import
Admin View
To manage the content of the email templates, navigate to /admin/email_templates
or add,
link_to 'Email Templates', effective_email_templates.admin_email_templates_path
to your menu.
Create Email Templates
The installer already created an app/mailers/email_templates_mailer.rb
. You can change this name. And additional mailers can be created, just make sure they extend from Effective::EmailTemplatesMailer
.
To create an email template:
- Create a method inside the mailer, like
def welcome
. The@assigns
instance variable should contain all the variables that are passed to the liquid view. Any variables that end in_url
likeroot_url
will be automatically expanded.
# app/mailers/email_templates_mailer.rb
class EmailTemplatesMailer < Effective::EmailTemplatesMailer
def welcome(user)
@assigns = {
user: {
first_name: user.first_name,
last_name: user.last_name
}
adjective: 'awesome'
}
mail(to: user.email)
end
end
- Create a .liquid file in
app/views/email_templates_mailer/welcome.liquid
.
---
subject: 'Welcome {{ user.first_name }}' # REQUIRED
from: '[email protected]' # REQUIRED
cc: '[email protected]' # optional
bcc: '[email protected]' # optional
---
Welcome {{ user.first_name }} {{ user.last_name }}!
I am a liquid template that is imported to the database, and is then editable.
Thanks for using our site at {{ root_url }}.
- Run
rake effective_email_templates:import
orrake effective_email_templates:overwrite
Remember to do this in your staging and production environments as well!
The import task can be run even when no new templates have been added and will not overwrite existing database templates. This allows you to run the rake task in a deploy script if you are adding new templates frequently.
- Send the emails like normal:
EmailTemplatesMailer.welcome(user).deliver
Authorization
All authorization checks are handled via the effective_resources gem found in the config/initializers/effective_resources.rb
file.
Permissions
To allow a user to see the admin area, using CanCan:
can [:index, :edit, :update, :destroy], Effective::EmailTemplate
can :admin, :effective_email_templates
License
MIT License. Copyright Code and Effect Inc.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Bonus points for test coverage
- Create new Pull Request