Class: MandrillMailer::MessageMailer

Inherits:
CoreMailer show all
Defined in:
lib/mandrill_mailer/offline.rb,
lib/mandrill_mailer/message_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, 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

#htmlObject

Public: The name of the template to use



102
103
104
# File 'lib/mandrill_mailer/message_mailer.rb', line 102

def html
  @html
end

#textObject

Public: Template content



105
106
107
# File 'lib/mandrill_mailer/message_mailer.rb', line 105

def text
  @text
end

Instance Method Details

#check_required_options(options) ⇒ Object



205
206
207
208
209
210
# File 'lib/mandrill_mailer/message_mailer.rb', line 205

def check_required_options(options)
  names = ['text', 'html', 'from', 'subject', 'to']
  names.each do |name|
    warn("Mandrill Mailer Warn: missing required option: #{name}") unless options.has_key?(name)
  end
end

#dataObject

Public: Data hash (deprecated)



194
195
196
197
198
199
200
201
202
# File 'lib/mandrill_mailer/message_mailer.rb', line 194

def data
  {
    "key" => api_key,
    "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



108
109
110
111
112
113
114
115
116
# File 'lib/mandrill_mailer/message_mailer.rb', line 108

def deliver

  MandrillMailer.deliveries << MandrillMailer::Mock.new({
    :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)



143
144
145
146
147
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
# File 'lib/mandrill_mailer/message_mailer.rb', line 143

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

  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 = {
    "text" => args[:text], 
    "html" => args[:html], 
    "view_content_link" => args[:view_content_link],
    "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],
    "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