class NotificationsMailer

Public Instance Methods

notify(notification_id, to=nil) click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 8
def notify(notification_id, to=nil)
  # Ensure that the notification exists
  self.notification(notification_id)

  to = notification.email if to.blank?

  # Safety checks
  return abort_cancelled if notification.cancelled?
  return abort_do_not_deliver if !notification.deliver_via_email?
  return abort_already_sent if notification.email_sent_at.present?
  return abort_no_recipient if to.blank?
  return abort_unsubscribed if unsubscribed?(to)
  return abort_whitelist_excluded if whitelist_excluded?(to)

  options = {
    to: to,
    from: notification.email_from,
    subject: notification.email_subject
  }
  options[:reply_to] = notification.email_reply_to if notification.email_reply_to.present?

  message = mail(options) do |format|
    format.html
    format.text
  end

  # Storing the rendered template might be a bit aggressive if you are
  # sending large batches of emails.
  notification.email_html = message.html_part.body.to_s
  notification.email_text = message.text_part.body.to_s
  notification.email_sent_at = Time.now
  notification.email_urls = urls.uniq.join("\n")
  notification.save

  message
end

Protected Instance Methods

abort_already_sent() click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 78
def abort_already_sent
  abort_delivery("already sent")
end
abort_cancelled() click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 70
def abort_cancelled
  abort_delivery("cancelled")
end
abort_delivery(reason) click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 61
def abort_delivery(reason)
  if notification.email_not_sent_at.blank?
    notification.email_not_sent_reason = reason
    notification.email_not_sent_at = Time.now
    notification.save
  end
  NullMail.new
end
abort_do_not_deliver() click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 74
def abort_do_not_deliver
  abort_delivery("do not deliver via email")
end
abort_no_recipient() click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 82
def abort_no_recipient
  abort_delivery("no recipient address")
end
abort_unsubscribed() click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 86
def abort_unsubscribed
  abort_delivery("recipient unsubscribed")
end
abort_whitelist_excluded() click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 90
def abort_whitelist_excluded
  abort_delivery("recipient not on whitelist")
end
append_tracking(url) click to toggle source

Return a URL that tracks clicks and that can be verified

# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 95
def append_tracking(url)
  urls << url
  return url if notification.do_not_track?
  notification_click_url(notification, r: url)
end
notification(notification_id=nil) click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 47
def notification(notification_id=nil)
  return @notification if defined?(@notification)
  @notification = Notification.find(notification_id) if notification_id.present?
  @notification || raise(ActiveRecord::RecordNotFound)
end
unsubscribed?(to) click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 53
def unsubscribed?(to)
  # TODO You can implement logic to ensure that the user (or to email) is not unsubscribed
end
urls() click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 101
def urls
  @urls ||= []
end
whitelist_excluded?(to) click to toggle source
# File lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb, line 57
def whitelist_excluded?(to)
  # TODO You can implement logic to ensure that business emails are not sent in development or test environments
end