Class: MandrillMailer::TemplateMailer

Inherits:
CoreMailer
  • Object
show all
Defined in:
lib/mandrill_mailer/offline.rb,
lib/mandrill_mailer/template_mailer.rb

Instance Attribute Summary collapse

Attributes inherited from CoreMailer

#async, #ip_pool, #message, #send_at

Instance Method Summary collapse

Methods inherited from CoreMailer

#bcc, #check_required_options, default, #from, super_defaults, test, test_setup_for, #to, #to=

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MandrillMailer::CoreMailer

Instance Attribute Details

#template_contentObject

Public: Template content



111
112
113
# File 'lib/mandrill_mailer/template_mailer.rb', line 111

def template_content
  @template_content
end

#template_nameObject

Public: The name of the template to use



108
109
110
# File 'lib/mandrill_mailer/template_mailer.rb', line 108

def template_name
  @template_name
end

Instance Method Details

#dataObject

Public: Data hash (deprecated)



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/mandrill_mailer/template_mailer.rb', line 206

def data
  {
    "key" => api_key,
    "template_name" => template_name,
    "template_content" => template_content,
    "message" => message,
    "async" => async,
    "ip_pool" => ip_pool,
    "send_at" => send_at
  }
end

#deliverObject

Public: Triggers the stored Mandrill params to be sent to the Mandrill api



114
115
116
117
118
119
120
121
122
123
# File 'lib/mandrill_mailer/template_mailer.rb', line 114

def deliver
  MandrillMailer.deliveries << MandrillMailer::Mock.new({
    :template_name    => template_name,
    :template_content => template_content,
    :message          => message,
    :async            => async,
    :ip_pool          => ip_pool,
    :send_at          => send_at
  })
end

#mandrill_mail(args) ⇒ Object

Public: Build the hash needed to send to the mandrill api

args - The Hash options used to refine the selection:

:template - Template name in Mandrill
:subject - Subject of the email
:to - Email to send the mandrill email to
:vars - Global merge vars used in the email for dynamic data
:recipient_vars - Merge vars used in the email for recipient-specific dynamic data
:bcc - bcc email for the mandrill email
:tags - Tags for the email
:google_analytics_domains - Google analytics domains
:google_analytics_campaign - Google analytics campaign
:inline_css - whether or not to automatically inline all CSS styles provided in the message HTML
:important - whether or not this message is important
:async - whether or not this message should be sent asynchronously
:ip_pool - name of the dedicated IP pool that should be used to send the message
:send_at - when this message should be sent

Examples

mandrill_mail template: 'Group Invite',
            subject: I18n.t('invitation_mailer.invite.subject'),
            to: invitation.email,
            vars: {
              'OWNER_NAME' => invitation.owner_name,
              'INVITATION_URL' => new_invitation_url(email: invitation.email, secret: invitation.secret)
            }

Returns the the mandrill mailer class (this is so you can chain #deliver like a normal mailer)



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/mandrill_mailer/template_mailer.rb', line 148

def mandrill_mail(args)

  # Mandrill requires template content to be there
  args[:template_content] = {"blank" => ""} if args[:template_content].blank?

  # format the :to param to what Mandrill expects if a string or array is passed
  args[:to] = format_to_params(args[:to])

  # Set the template name
  self.template_name = args.delete(:template)

  # Set the template content
  self.template_content = mandrill_args(args.delete(:template_content))
  self.async = args.delete(:async)
  self.ip_pool = args.delete(:ip_pool)

  if args.has_key?(:send_at)
    self.send_at = args.delete(:send_at).getutc.strftime('%Y-%m-%d %H:%M:%S')
  end

  # Construct message hash
  self.message = {
    "subject" => args[:subject],
    "from_email" => args[:from] || self.class.defaults[:from],
    "from_name" => args[:from_name] || self.class.defaults[:from_name] || self.class.defaults[:from],
    "to" => args[:to],
    "headers" => args[:headers],
    "important" => args[:important],
    "track_opens" => args.fetch(:track_opens, true),
    "track_clicks" => args.fetch(:track_clicks, true),
    "auto_text" => true,
    "inline_css" => args[:inline_css],
    "url_strip_qs" => args.fetch(:url_strip_qs, true),
    "preserve_recipients" => args[:preserve_recipients],
    "bcc_address" => args[:bcc],
    "merge_language" => args[:merge_language],
    "global_merge_vars" => mandrill_args(args[:vars]),
    "merge_vars" => mandrill_rcpt_args(args[:recipient_vars]),
    "tags" => args[:tags],
    "subaccount" => args[:subaccount],
    "google_analytics_domains" => args[:google_analytics_domains],
    "google_analytics_campaign" => args[:google_analytics_campaign],
    "metadata" => args[:metadata],
    "attachments" => mandrill_attachment_args(args[:attachments]),
    "images" => mandrill_images_args(args[:images])
  }
  unless MandrillMailer.config.interceptor_params.nil?
    unless MandrillMailer.config.interceptor_params.is_a?(Hash)
      raise InvalidInterceptorParams.new "The interceptor_params config must be a Hash"
    end
    self.message.merge!(MandrillMailer.config.interceptor_params.stringify_keys)
  end

  # return self so we can chain deliver after the method call, like a normal mailer.
  return self
end