Skip to content

Setup with 'devise' gem

Wojciech Rawicz - Kosiński edited this page Nov 10, 2015 · 1 revision

Initial setup

Considering You have configured Your User authentication with devise gem (similar to this example ). Similar because I'm using all modules from devise. Your next step is to setup MailyHerald and MailyHeraldWebui (optionally).

MailyHerald

Installation

# Gemfile

gem 'maily_herald'
gem 'maily_herald-webui'

Run following commands

Remember to specify Your mailer in environment settings (eg.: 'from', 'host')

bundle install
rake maily_herald:install:migrations
rake db:migrate
rails g maily_herald:install

Configuring basic setup

Devise initializer needs a bit of tweaking.

# config/initializers/devise.rb

...
config.mailer = 'UserMailer'
config.parent_mailer = 'MailyHerald::Mailer'
config.unlock_keys = [:unlock_token]
config.unlock_strategy = :email

confirmation_instructions is OneTimeMailing, because newly created User will always get one email after signing up. Rest of emails (beside weekly_newsletter, which is periodical mailing) are being sent as AdHocMailing, because You cannot define their sending time.

# config/initializers/maily_herald.rb

MailyHerald.setup do |config|
  config.context :all_users do |context|
    context.scope {User.all}
    context.destination {|user| user.email}
    context.attributes do |user| 
      attribute_group(:user) do
        attribute(:email) {user.email}
        attribute(:created_at) {user.created_at}
      end
    end
  end

  config.list :all_users do |list|
    list.context_name = :all_users
  end

  config.one_time_mailing :confirmation_instructions do |mailing|
    mailing.title = "Rails_Devise_Test_app - confirmation instructions"
    mailing.list = :all_users
    mailing.mailer_name = "UserMailer"
    mailing.start_at = Proc.new{|user| user.created_at}
    mailing.enable # mailings are disabled by default
  end

  config.ad_hoc_mailing :reset_password_instructions do |mailing|
    mailing.title = "Rails_Devise_Test_app - reset password instructions"
    mailing.list = :all_users
    mailing.mailer_name = "UserMailer"
    mailing.enable # mailings are disabled by default
  end

  config.ad_hoc_mailing :unlock_instructions do |mailing|
    mailing.title = "Rails_Devise_Test_app - unlock instructions"
    mailing.list = :all_users
    mailing.mailer_name = "UserMailer"
    mailing.enable # mailings are disabled by default
  end

  config.periodical_mailing :weekly_newsletter do |mailing|
    mailing.title = "Weekly newsletter"
    mailing.list = :all_users
    mailing.mailer_name = "UserMailer"
    mailing.start_at = Time.now + 5.minutes
    mailing.period = 1.week
    mailing.enable
  end
end

Create mailer methods.

# app/mailers/user_mailer.rb

class UserMailer < Devise::Mailer   
  helper :application # gives access to all helpers defined within `application_helper`.
  include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
  default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views

  def confirmation_instructions(record, opts={})
    @token = record.confirmation_token
    devise_mail(record, :confirmation_instructions, opts)
  end

  def reset_password_instructions(record, opts={})
    raw, enc = Devise.token_generator.generate(record.class, :reset_password_token)
    record.reset_password_token = enc
    record.save(:validate => false)
    @token = raw
    devise_mail(record, :reset_password_instructions, opts)
  end

  def unlock_instructions(record, opts={})
    raw, enc = Devise.token_generator.generate(record.class, :unlock_token)
    record.unlock_token = enc
    record.save(:validate => false)
    @token = raw
    devise_mail(record, :unlock_instructions, opts)
  end

  def weekly_newsletter
    mail :subject => "Hi there #{user.email}!"
  end
end

Add MailyHeraldWebui and opt-url for unsubscribing to Your routes.

# config/routes.rb

mount MailyHerald::Webui::Engine => "/maily_webui"
mount MailyHerald::Engine => "/unsubscribe", :as => "maily_herald_engine"

Tweak Your User model

# app/models/user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable

  after_create :subscribe_to_list

  def subscribe_to_list
    MailyHerald.subscribe(self, :all_users)
  end

  def to_s
    "#{self.name}"
  end
end

Create views for each email.

# app/views/devise/mailer/confirmation_instructions.html.erb

<p>Welcome <%= @email %>!</p>
<p>You can confirm your account email through the link below:</p>
<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>

# app/views/devise/mailer/reset_password_instructions.html.erb

<p>Hello <%= @resource.email %>!</p>
<p>Someone has requested a link to change your password. You can do this through the link below.</p>
<p><%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
<hr/>
<p><%= link_to 'Unsubscribe?', maily_herald_engine.maily_unsubscribe_url(MailyHerald::Subscription.where(entity_id: @resource.id).first) %></p>

# app/views/devise/mailer/unlock_instructions.html.erb

<p>Hello <%= @resource.email %>!</p>
<p>Your account has been locked due to an excessive number of unsuccessful sign in attempts.</p>
<p>Click the link below to unlock your account:</p>
<p><%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token) %></p>
<hr/>
<p><%= link_to 'Unsubscribe from notifications?', maily_herald_engine.maily_unsubscribe_url(MailyHerald::Subscription.where(entity_id: @resource.id).first) %></p>

# app/views/devise/mailer/weekly_newsletter.html.erb

<p>Weekly newsletter</p>
...

Running Your App

Remember to run background processing when running Your app.

maily_herald paperboy --start