Git Product home page Git Product logo

mailer's Introduction

Lotus::Mailer

Mail for Ruby applications.

Status

Gem Version Build Status Coverage Code Climate Dependencies Inline Docs

Contact

Rubies

Lotus::Mailer supports Ruby (MRI) 2+.

Installation

Add this line to your application's Gemfile:

gem 'lotus-mailer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install lotus-mailer

Usage

Conventions

  • Templates are searched under Lotus::Mailer.configuration.root, set this value according to your app structure (eg. "app/templates").
  • A mailer will look for a template with a file name that is composed by its full class name (eg. "articles/index").
  • A template must have two concatenated extensions: one for the format and one for the engine (eg. ".html.erb").
  • The framework must be loaded before rendering the first time: Lotus::Mailer.load!.

Mailers

A simple mailer looks like this:

require 'lotus/mailer'

class InvoiceMailer
  include Lotus::Mailer
end

A mailer with .to and .from addresses and mailer delivery:

require 'lotus/mailer'

Lotus::Mailer.configuration do
  delivery_method :smtp,
    address:              "smtp.gmail.com",
    port:                 587,
    domain:               "example.com",
    user_name:            ENV['SMTP_USERNAME'],
    password:             ENV['SMTP_PASSWORD'],
    authentication:       "plain",
    enable_starttls_auto: true
end.load!

class WelcomeMailer
  include Lotus::Mailer

  from    '[email protected]'
  to      '[email protected]'
  subject 'Welcome'
end

WelcomeMailer.deliver

Locals

The set of objects passed in the deliver call are called locals and are avaliable inside the mailer and the template.

require 'lotus/mailer'

User = Struct.new(:name, :username)
user = User.new('Luca', 'jodosha')

Lotus::Mailer.load!

class WelcomeMailer
  include Lotus::Mailer

  from    '[email protected]'
  subject 'Welcome'
  to      :recipient

  private

  def recipient
    user.email
  end
end

InvoiceMailer.deliver(user: luca)

The corresponding erb file:

Hello <%= user.name %>!

Scope

All public methods defined in the mailer are accessible from the template:

require 'lotus/mailer'

class WelcomeMailer
  include Lotus::Mailer

  from    '[email protected]'
  to      '[email protected]'
  subject 'Welcome'

  def greeting
    'Ahoy'
  end
end
<h2><%= greeting %></h2>

Template

The template file must be located under the relevant root and must match the inflected snake case of the mailer class name.

# Given this root
Lotus::Mailer.configuration.root      # => #<Pathname:app/templates>

# For InvoiceMailer, it looks for:
#  * app/templates/invoice_mailer.html.erb
#  * app/templates/invoice_mailer.txt.erb

If we want to specify a different template, we can do:

class InvoiceMailer
  include Lotus::Mailer

  template 'invoice'
end

# It will look for:
#  * app/templates/invoice.html.erb
#  * app/templates/invoice.txt.erb

Engines

The builtin rendering engine is ERb.

This is the list of the supported engines. They are listed in order of higher precedence, for a given extension. For instance, if ERubis is loaded, it will be preferred over ERb to render .erb templates.

Engine Extensions
Erubis erb, rhtml, erubis
ERb erb, rhtml
Redcarpet markdown, mkd, md
RDiscount markdown, mkd, md
Kramdown markdown, mkd, md
Maruku markdown, mkd, md
BlueCloth markdown, mkd, md
Asciidoctor ad, adoc, asciidoc
Builder builder
CSV rcsv
CoffeeScript coffee
WikiCloth wiki, mediawiki, mw
Creole wiki, creole
Etanni etn, etanni
Haml haml
Less less
Liquid liquid
Markaby mab
Nokogiri nokogiri
Plain html
RDoc rdoc
Radius radius
RedCloth textile
Sass sass
Scss scss
Slim slim
String str
Yajl yajl

Configuration

Lotus::Mailer can be configured with a DSL that determines its behavior. It supports a few options:

require 'lotus/mailer'

Lotus::Maler.configure do
  # Set the root path where to search for templates
  # Argument: String, Pathname, #to_pathname, defaults to the current directory
  #
  root '/path/to/root'

  # Set the default charset for emails
  # Argument: String, defaults to "UTF-8"
  #
  default_charset 'iso-8859'

  # Set the delivery method
  # Argument: Symbol
  # Argument: Hash, optional configurations
  delivery_method :stmp

Attachments

Attachments can be added with the following API:

class InvoiceMailer
  include Lotus::Mailer
  # ...

  def prepare
    mail.attachments['invoice.pdf'] = '/path/to/invoice.pdf'
    # or
    mail.attachments['invoice.pdf'] = File.read('/path/to/invoice.pdf')
  end
end

Delivery Method

The global delivery method is defined through the Lotus::Mailer configuration, as:

Lotus::Mailer.configuration do
  delivery_method :smtp
end
Lotus::Mailer.configuration do
  delivery_method :smtp, address: "localhost", port: 1025
end

Builtin options are:

  • Exim (:exim)
  • Sendmail (:sendmail)
  • SMTP (:smtp, for local installations)
  • SMTP Connection (:smtp_connection, via Net::SMTP - for remote installations)
  • Test (:test, for testing purposes)

Custom Delivery Method

Developers can specify their own custom delivery policy:

require 'lotus/mailer'

class MandrillDeliveryMethod
  def initialize(options)
    @options = options
  end

  def deliver!(mail)
    # ...
  end
end

Lotus::Mailer.configure do
  delivery_method MandrillDeliveryMethod,
    username: ENV['MANDRILL_USERNAME'],
    password: ENV['MANDRILL_API_KEY']
end.load!

The class passed to .delivery_method must accept an optional set of options with the constructor (#initialize) and respond to #deliver!.

Multipart Delivery

All the email are sent as multipart messages by default. For a given mailer, the framework looks up for associated text (.txt) and HTML (.html) templates and render them.

InvoiceMailer.deliver               # delivers both text and html templates
InvoiceMailer.deliver(format: :txt) # delivers only text template

Please note that they aren't both mandatory, but at least one of them MUST be present.

Versioning

Lotus::Mailer uses Semantic Versioning 2.0.0

Copyright

Copyright ยฉ 2015 Luca Guidi โ€“ Released under MIT License

mailer's People

Contributors

rosafaria avatar inescoelho avatar jodosha avatar timoschilling avatar

Watchers

James Cloos avatar thori avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.