Using Postmark with Sinatra

I usually use Postmark for outgoing transactional email, I find this to be better than expecting the underlying system to have a correctly configured sendmail and it helps with deliverability. Sinatra, though, doesn’t have a convention for handling email. The Sinatra FAQ lists an example using Pony, so going from there, here’s an example of using the Postmark Gem with Sinatra:

require 'sinatra'
require 'postmark'

configure do
  set :mailer,'')

get '/send_mail' do
  settings.mailer.deliver(from: '',
                          to: '',
                          subject: 'A Test Email',
                          text_body: 'A simple plain text test email.')

I’m just using the standard settings handling to keep hold of the Postmark client here. You could do it in any way. You can also render views (like you’d typically do with ActionMailer), like so:

get '/send_html_mail' do
  settings.mailer.deliver(from: '',
                          to: '',
                          subject: 'An HTML Test Email',
                          html_body: erb(:email))

…where, :email is email.erb in views/:

<p>A simple <i>HTML</i> test email.</p>

And then you have simple Postmark email support. The rest of the Postmark Gem documentation details everything else you’d be able to do with it.