1

I'm trying to figure out how to send transactional email from my Rails 4 app.

I have found tutorials for the postmark gem, but I'm struggling to close the gaps between what's assumed in the tutorials (where to do the suggested steps!) and what I know.

I have installed both the ruby and the rails gems in my gemfile:

gem 'postmark-rails', '~> 0.13.0'
gem 'postmark'

I have added the postmark config to my config/application.rb:

config.action_mailer.delivery_method = :postmark
    config.action_mailer.postmark_settings = { :api_token => ENV['POSTMARKKEY'] }

I want to try to make and use email templates in postmark.

The instructions in the postmark gem docs say I need to:

Create an instance of Postmark::ApiClient to start sending emails.

your_api_token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
client = Postmark::ApiClient.new(your_api_token)

I don't know how to do this step? Where do I write the second line? I have my api token stored in my config. I don't know how to make an instance of the postmark api client.

Can anyone point me to next steps (or a more detailed tutorial)?

Mel
  • 2,481
  • 26
  • 113
  • 273

1 Answers1

3

After you have installed the gems, you need to create a Mailer. I presume you have already configured the API keys etc. in the correct manner, so I will concentrate on actually sending out a templated / static email.

Lets create app/mailers/postmark_mailer.rb file with the following contents.

class PostmarkMailer < ActionMailer::Base
  default :from => "your@senderapprovedemail.com>"
  def invite(current_user)
    @user = current_user
    mail(
      :subject => 'Subject',
      :to      => @user.email,
      :return => '74f3829ad07c5cffb@inbound.postmarkapp.com',
      :track_opens => 'true'
    )
  end
end

We can then template this mailer in the file app/views/postmark_mailer/invite.html.erb Let's use the following markup to get you started.

<p>Simple email</p>
<p>Content goes here</p>

You can write it like any other .html.erb template using tags, HTML and alike.

To actually send this email out you need to place an action in your controller, in the following manner.

PostmarkMailer.invite(current_user)

Alternatively, if you want this email to be sent upon visiting homepage, it would most likely look like this:

app/controllers/home_controller.rb with content

class HomeController < ApplicationController

  # GET /
  def index
    PostmarkMailer.invite(current_user)
  end
end

and corresponsing route

config/routes.rb with content

root :to => 'home#index'

I hope this answers your question.

Cninroh
  • 1,796
  • 2
  • 21
  • 37