By Synbioz
This gem contains some basic models that allows the end-user of your applications to customize the application email.
This is a basic use case:
target = 'foo@bar.org'
ctx = { 'name' => 'Luc' }
# Create default kinds
%w(request new_event).each do |kind_name|
CustomEmails::EmailKind.find_or_create_by!(name: kind_name)
end
# Create an email
kind = CustomEmails::EmailKind.find_by!(name: 'request')
email = CustomEmails::Email.create(
locale: :fr,
kind: kind,
subject: 'Bonjour {{ name }} !',
content_text: 'Bonne chance {{ name }}...'
)
# Send via the Email object
email.to(target, ctx).deliver
# Use the mailer to send an email if you don't have the Email objet
CustomEmails::Mailer.custom_email_to(target, 'request', nil, context: ctx).deliver
This is a basic use case with a scope object:
# Declare a class with customizable emails
class Account
...
has_custom_emails
...
end
acc = Account.first
# Create some email related to the account
email = account.emails.create(
locale: :fr,
kind: kind,
subject: 'Bonjour {{ name }} !',
content_text: 'Bonne chance {{ name }}...',
content_html: '<b>Bonne change...</b>' # Optional
)
# Deliver the email to somebody
email.to(target, ctx).deliver
# Or do it direclty with the mailer
CustomEmails::Mailer.custom_email_to(target, 'request', acc, ctx).deliver
This scope object allows you to have multiple email per kind.
Add this to your gemfile:
gem 'custom_emails'
Add the tables to your database with:
rake custom_emails:install:migrations
rake db:migrate
Add the initializer to configure the gem with:
rails generate custom_emails:install
That's all!
- Only active record is supported
This project uses the MIT licence.