Class: Mail::Notify::Mailer
- Inherits:
-
ActionMailer::Base
- Object
- ActionMailer::Base
- Mail::Notify::Mailer
- Defined in:
- lib/mail/notify/mailer.rb
Overview
The Mail Notify base Mailer class, overridden in Rails applications to provide the additional Notify behaviour along with the application behaviour.
Instance Method Summary collapse
-
#blank_allowed(value) ⇒ Object
allows blank personalisation options.
-
#template_mail(template_id, options) ⇒ Object
Send an email where the content is managed in the Notify template.
-
#view_mail(template_id, options) ⇒ Object
Send an email where the content is managed in the Rails application.
Instance Method Details
#blank_allowed(value) ⇒ Object
allows blank personalisation options
100 101 102 |
# File 'lib/mail/notify/mailer.rb', line 100 def blank_allowed(value) value.to_s end |
#template_mail(template_id, options) ⇒ Object
Send an email where the content is managed in the Notify template.
The required arguments are:
-
template_id
-
to address
Can include personalisation.
Add any additional headers in the options hash.
A default subject is supplied as ActionMailer requires one, however it will never be used as the subject is assumed to be managed in the Notify template.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mail/notify/mailer.rb', line 31 def template_mail(template_id, ) raise ArgumentError, "You must specify a Notify template ID" if template_id.blank? raise ArgumentError, "You must specify a to address" if [:to].nil? || [:to].blank? .template_id = template_id .reply_to_id = [:reply_to_id] .reference = [:reference] .one_click_unsubscribe_url = [:one_click_unsubscribe_url] .personalisation = [:personalisation] || {} headers = .except(:personalisation, :reply_to_id, :reference) headers[:subject] = "Subject managed in Notify" unless [:subject] # We have to set the html and the plain text content to nil to prevent Rails from looking # for the content in the views. We replace nil with the content returned from Notify before # sending or previewing mail(headers) do |format| format.text { nil } format.html { nil } end end |
#view_mail(template_id, options) ⇒ Object
Send an email where the content is managed in the Rails application.
The required arguments are:
-
template_id
-
to address
-
subject
Personalisation will dropped as all content comes from the view provided by Rails.
Add any additional headers in the options hash.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mail/notify/mailer.rb', line 68 def view_mail(template_id, ) raise ArgumentError, "You must specify a Notify template ID" if template_id.blank? raise ArgumentError, "You must supply a to address" if [:to].blank? raise ArgumentError, "You must specify a subject" if [:subject].blank? .template_id = template_id .reply_to_id = [:reply_to_id] .reference = [:reference] subject = [:subject] headers = .except(:personalisation, :reply_to_id, :reference) # We have to render the view for the message and grab the raw source, then we set that as the # body in the personalisation for sending to the Notify API. # # We do not pass the headers as the call to `mail` will keep adding headers resulting in # duplication when we have to call it again later. body = mail.body.raw_source # The 'view mail' works by sending a subject and body as personalisation options, these are # then used in the Notify template to provide content. .personalisation = {subject: subject, body: body} mail(headers) do |format| format.text { nil } format.html { nil } end end |