Module: Sinatra::Mailer

Included in:
EventContext
Defined in:
lib/mailer.rb

Overview

Sinatra::Mailer

Adds an #email method to your email handlers, that receives a hash of values to create your email.

For example:

post "/signup" do
  # sign up the user, and then:
  email :to      => @user.email,
        :from    => "[email protected]",
        :subject => "Welcome to Awesomeness!",
        :body    => haml(:some_template)
end

Configuration

This plugin is very dirty yet :) Since it’s just a port to Sinatra of Merb::Mailer. So the configuration is not Sinatra-y, yet. But we’ll get to that.

Using SMTP

Sinatra::Mailer.config = {
  :host   => 'smtp.yourserver.com',
  :port   => '25',
  :user   => 'user',
  :pass   => 'pass',
  :auth   => :plain # :plain, :login, :cram_md5, the default is no auth
  :domain => "localhost.localdomain" # HELO domain provided by the client
}

Using Gmail SMTP

You need smtp-tls, a gem that improves Net::HTTP to add support for secure servers such as Gmail.

require "smtp-tls"

Sinatra::Mailer.config = {
  :host => 'smtp.gmail.com',
  :port => '587',
  :user => '[email protected]',
  :pass => 'pass',
  :auth => :plain
}

Make sure that when you call your #email method you pass the :text option and not :body.

Using sendmail

Sinatra::Mailer.config = {:sendmail_path => '/somewhere/odd'}
Sinatra::Mailer.delivery_method = :sendmail

Credits

This has been blatantly adapted from Merb::Mailer so all credit is theirs, I just ported it to Sinatra.

Defined Under Namespace

Classes: Email

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



77
78
79
# File 'lib/mailer.rb', line 77

def config
  @config
end

.delivery_methodObject

Returns the value of attribute delivery_method.



77
78
79
# File 'lib/mailer.rb', line 77

def delivery_method
  @delivery_method
end

Instance Method Details

#email(mail_options = {}) ⇒ Object



80
81
82
# File 'lib/mailer.rb', line 80

def email(mail_options={})
  Email.new(mail_options).deliver!
end