-
Notifications
You must be signed in to change notification settings - Fork 57
Setup with 'sorcery' gem
Considering You have configured Your User
and UserSession
models and controllers (similar to Simple Password Authentication via Sorcery), Your next step is to setup MailyHerald
and MailyHeraldWebui
(optionally).
# Gemfile
gem 'maily_herald'
gem 'maily_herald-webui'
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
activation_needed_email
is OneTimeMailing
, because newly created User
will always get one email after signing up. Rest of emails 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 :activation_needed_email do |mailing|
mailing.title = "Your account needs activation"
mailing.list = :all_users
mailing.mailer_name = "UserMailer"
mailing.start_at = Proc.new{|user| user.created_at + 5.seconds}
mailing.enable # mailings are disabled by default
end
config.ad_hoc_mailing :activation_success_email do |mailing|
mailing.title = "Activation succeeded"
mailing.list = :all_users
mailing.mailer_name = "UserMailer"
mailing.enable # mailings are disabled by default
end
config.ad_hoc_mailing :reset_password_email do |mailing|
mailing.title = "Reset password"
mailing.list = :all_users
mailing.mailer_name = "UserMailer"
mailing.enable # mailings are disabled by default
end
end
Create mailer methods.
# app/mailers/user_mailer.rb
class UserMailer < MailyHerald::Mailer
def activation_needed_email user
@user = user
@url = activate_user_url(user.activation_token)
@password = user.password
mail(:to => user.email,
:subject => "Welcome to App")
end
def activation_success_email user
@user = user
@url = login_url
mail(:to => user.email,
:subject => "Your account is now activated")
end
def reset_password_email user
@user = user
@url = reset_password_user_url(user.reset_password_token)
mail(:to => user.email,
:subject => "Reset Your password")
end
end
Add MailyHeraldWebui
to Your routes.
# config/routes.rb
mount MailyHerald::Webui::Engine => "/maily_webui"
Add method to_s
, which is needed for proper display in MailyHeraldWebui
.
# app/models/user/rb
...
def to_s
"#{self.email} created at #{self.created_at}"
end
Create views for each email.
# app/views/user_mailer/activation_needed_email.html.haml
%h2 Please activate your account
%br/
%p= link_to "Click here to activate Your account", @url
# app/views/user_mailer/activation_success_email.html.haml
%h1 Welcome to App
%br/
%p Now You have fully activated account. Go ahead and log into Your account!
# app/views/user_mailer/reset_password_email.html.haml
%h2 You requested a new password.
%br/
%p= link_to "Click here to reset password for Your account", @url
Remember to run background processing when running Your app.
maily_herald paperboy --start
bundle exec sidekiq -v
The simple way to do this it to use foreman
gem. Your Procfile
should look like this:
web: bundle exec rails s
worker: bundle exec sidekiq -v
paperboy: bundle exec maily_herald paperboy