Padrino Mailer

This component creates an easy and intuitive interface for delivering email
within a Sinatra application. The mail
library is utilized to do the bulk of the work. There is full support for
rendering email templates, using a html content type and for file attachments.
The Padrino Mailer uses a familiar Sinatra syntax similar to that of defining
routes for a controller.

Once those have been defined, the default will become SMTP delivery unless
overwritten in an individual mail definition.

You can also configure the mailer to not send emails during development or
testing. This can be done with:

# app/app.rbset:delivery_method,:test

When set, messages are added to the test mailer and can be retrieved with:

Mail::TestMailer.deliveries

Quick Usage

Padrino supports sending any arbitrary email (using either sendmail or SMTP)
right from your controllers. This is ideal for 'one-off' emails where the 'full'
mailer object is simply unnecessary or too heavy for your simple task.

This is all you need to send simple emails. However, Padrino also supports a
more 'structured' mailer system as well.

Mailer Usage

To use the structured mailer syntax, we should define a custom mailer using the
mailer block:

# app/mailers/sample_mailer.rbMyAppName.mailer:sampledoemail:registration_emaildo|name,email|from'admin@site.com'toemailsubject'Welcome to the site!'locals:name=>name,:email=>emailrender'sample/registration_email'content_type:html# optional, defaults to :plainvia:sendmail,:location=>"/usr/bin/sendmail"# optional, to smtp if defined otherwise sendmailendend

Note that this can be created much easier by using the padrino-mailer generator
in the terminal:

$ padrino g mailer Sample registration_email

This mailer defines a mail route called registration_mail within the sample
mailer. The registration_email route accepts the name and email arguments.
Arguments are passed to the email body template via the locals method. The
render command renders the email body template with the local variables, which
should be defined in [views_path]/mailers/sample/registration_email.erb as
shown below:

# ./views/mailers/sample/registration_email.erb
This is the body of the email and can access the <%=name%> variable.
That‘s all there is to defining the body of the email which can be in plain text
or html.

Note that the mailer has full support for content type resolution and the email
could also be in the path ./views/mailers/sample/registration_email.html.erb
or ./views/mailers/sample/registration_email.plain.erb specifying the mime
type in the file name as well.

Once the mailer has been defined and the template written, the email route can
be invoked by the deliver method:

deliver(:sample,:registration_email,"Bob","bob@bobby.com")

And that will then deliver the email according the configured options.