3

I'm trying to figure out how to setup my Rails 4 app so that the devise mailer sends through Postmark, using Postmark Templates.

I have postmark-rails gem in my gem file.

I have everything working, except I can't figure out how to give the confirmation token to postmark.

In Postmark I have this template:

To get started, please confirm your account below:

action_url_Value

I can't figure out how to put the following line into the template instead of action url value:

<%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></div>

I have added the following to my initializer/devise.rb:

config.mailer = 'PostmarkDeviseMailer'

In postmark_devise_mailer.rb, I have:

class PostmarkDeviseMailer < ActionMailer::Base
  include Devise::Mailers::Helpers

  def message
    mail(
      :subject => 'Hello from Postmark',
      :to  => 'sender@address.com',
      :from => 'sender@address.comm',
      :html_body => '<strong>Hello</strong> dear Postmark user.',
      :track_opens => 'true')
  end
end


  default from: "sender@address.com"

  def confirmation_instructions(record)
    devise_mail(record, :confirmation_instructions)
  end

  def reset_password_instructions(record)
    devise_mail(record, :reset_password_instructions)
  end

  def unlock_instructions(record)
    devise_mail(record, :unlock_instructions)
  end

The next steps are less clear to me. Has anyone figured out how to use Postmark Templates as the mailer for devise transaction emails?

Mel
  • 3,699
  • 13
  • 71
  • 194

2 Answers2

3

Response from Postmark help desk is: Unfortunately, Postmark templates don’t fit nicely into the traditional Rails ecosystem. See my reply here for more details:

https://github.com/wildbit/postmark-rails/issues/53

To sum it up, by using the Postmark API with Postmark templates you could potentially replace ActionMailer, but mixing them together presents a significant challenge. Unfortunately, we’ve never tried the "ActionMailer-free" approach with Devise, so I can’t help you here. Unless you have a good reason not to, I’d suggest going with ActionMailer templates and using the postmark-rails gem as a delivery adapter. This approach is known to work well with Devise. Please let me know if you have any questions.

Mel
  • 3,699
  • 13
  • 71
  • 194
2

The first thing you need to do is create a custom mailer in app/mailers in this example it is user_mailer.rb If you are reading this answer, I am assuming you both have a postmark account if not grab one here: Postmark Registration and I am assuming you have a template and layout selected.

One important item to remember is to make sure you assign an alias to your template. This can be found in a small link found above the template title in small gray text.

The items within self.template_model are 100% customizable and are shown in your layout / template editors as so: {{product_name}}.

class UserMailer < Devise::Mailer
  include PostmarkRails::TemplatedMailerMixin

  helper :application
  include Rails.application.routes.url_helpers
  include Devise::Controllers::UrlHelpers

  default from: 'from_you@something.com'
  default reply_to: 'support@hammer-lane-industries.com' # Optional 

  def confirmation_instructions(record, token, opts={})
    @admin = record
    @token = token
    # this will pass the data via the postmark-api to your template / layout these fields are 100% customizable and can be cahnged in your template / layout
    self.template_model = { product_name: 'your product',
                            username: @user.username,
                            email: @user.email,
                            confirmation_url: user_confirmation_url(@resource, confirmation_token: @token, subdomain: 'add subdomain here if needed'),
                            login_url: login_root_url(subdomain: 'add subdomain here if needed'),
                            trial_length: '14-days',
                            sender_name: 'What ever you want',
                            action_url: user_confirmation_url(@resource, confirmation_token: @token, subdomain: 'add subdomain here if needed'),
                            parent_company_name: 'Your company name',
                            company_address: 'Your address'}
    mail to: @admin.email, postmark_template_alias: 'devise_user_confirmation'
  end
end

Rinse and repeat with unlocks and password mailers, all you will need to know is the urls that you'll be sending the tokens to.

To finish it all off:

You will need to add a small block to your devise resource model, in this example I've used user.rb

class User < ApplicationRecord
  devise :database_authenticatable, :confirmable,
         :recoverable, :rememberable, :validatable,
         :timeoutable, :trackable

  def devise_mailer
    UserMailer # Must match your mailer class
  end
end

Once this is done, restart your rails server, and give it a go!

I hope this helps you with hooking Postmark Templates (API) into Devise mailers

Shawn Wilson
  • 1,180
  • 10
  • 27