Skip to content

Instantly share code, notes, and snippets.

@TheMindlessDevCanada
Created July 18, 2020 02:57
Show Gist options
  • Save TheMindlessDevCanada/9a18c4f1711f49207af3a3ce01f9a71b to your computer and use it in GitHub Desktop.
Save TheMindlessDevCanada/9a18c4f1711f49207af3a3ce01f9a71b to your computer and use it in GitHub Desktop.
How to use Postmark Templates & Api with Devise
# 1 - Create a custom mailer in mailers/my_cutsom_mailer.rb and inherit from Devise::Mailer add the following - replacing to suit your needs
class UserMailer < Devise::Mailer
include PostmarkRails::TemplatedMailerMixin
helper :application
include Rails.application.routes.url_helpers
include Devise::Controllers::UrlHelpers
default from: '[email protected]'
default reply_to: '[email protected]' # 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'
# Rinse and Repeat for Password resets and Unlocks --- Ill update this gist with passowrd and unlocks in the near future
end
end
# Note the postmark_template_alias: '' - This is important to configure! otherwize your mailers will fail.
# You can configure it by heading to your Template / Latyouts click on your template and right above the name field you
# will find Alias: some_clickable_link, Click it and set the name to your preference and add it to your mailer file above.
# Next step is to add a custom function to your devise user model.
class Admin < ApplicationRecord
devise :database_authenticatable, :confirmable,
:recoverable, :rememberable, :validatable,
:timeoutable, :trackable
def devise_mailer
UserMailer
end
end
# And thats literally all there is to it! reset your server and reload your console and have at it. (yes this will send on a valid user create from your user forms too.
# Feel free to add any questions or comments below!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment