Class: NotificationsMailer

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
lib/generators/notifykit/templates/app/mailers/notifications_mailer.rb

Instance Method Summary collapse

Instance Method Details

#notify(notification_id, to = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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