synbioz / custom_emails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CustomEmails

By Synbioz

Description

This gem contains some basic models that allows the end-user of your applications to customize the application email.

Example

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.

Installation

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!

Limitations

  • Only active record is supported

Licence

This project uses the MIT licence.

About

License:MIT License


Languages

Language:Ruby 96.8%Language:JavaScript 1.7%Language:CSS 1.5%