Hertz::Email
This is a Hertz courier for sending email notifications to your users through ActionMailer.
Installation
Add this line to your application's Gemfile:
gem 'hertz-email'And then execute:
$ bundleOr install it yourself as:
$ gem install hertz-emailThen, run the installer generator:
$ rails g hertz:email:installYou will also need to expose the hertz_email method in your receiver class. This can be either a
single email or an array of emails:
class User < ActiveRecord::Base
include Hertz::Notifiable
def hertz_email
email
end
endIf #hertz_email returns an empty value (i.e. false, nil, an empty string or an empty array) at
the time the job is executed, the notification will not be delivered. This allows you to
programmatically enable/disable email notifications for a user:
class User
include Hertz::Notifiable
def hertz_email
email if email_verified?
end
endOr even to choose what addresses they can receive emails to:
class User
include Hertz::Notifiable
def hertz_email
emails.select(&:verified?)
end
endUsage
In order to use this courier, add :email to #deliver_by in the notification model(s):
class CommentNotification < Hertz::Notification
deliver_by :email
endNow, add the #email_subject method in your notification class:
class CommentNotification < Hertz::Notification
def email_subject
'You have a new comment!'
end
endYou may also pass more options to the #mail method of the mailer by defining a #email_options
method:
class CommentNotification < Hertz::Notification
def email_options
{
# generate a custom Reply-To address for the receiver
reply_to: "replies+#{receiver.id}@example.com"
}
end
endFinally, you should create a template for every notification you send by email. For
CommentNotification you'd create a template at
app/views/hertz/email/notification_mailer/comment_notification.html.erb:
<p>Hey <%= @notification.receiver.hertz_email %>,</p>
<p>you've got a new comment!</p>As you can see, templates have access to the @notification instance variable.
NOTE: This courier uses the deliveries API to prevent double deliveries.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/aldesantis/hertz-email.
License
The gem is available as open source under the terms of the MIT License.