Class: Mail::MadMimi

Inherits:
Object
  • Object
show all
Defined in:
lib/mail/mad_mimi.rb,
lib/mail/mad_mimi/version.rb

Overview

Mail::MadMimi is a delivery method for Mail. It uses the MadMimi library to send mail via Mad Mimi.

Defined Under Namespace

Modules: SetMailerAction Classes: Error

Constant Summary collapse

VERSION =
"1.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ MadMimi

Any settings given here will be passed to Mad Mimi.

:email and :api_key are required.



14
15
16
17
18
19
20
21
# File 'lib/mail/mad_mimi.rb', line 14

def initialize(settings = {})
  unless settings[:email] && settings[:api_key]
    raise Error, "Missing :email and :api_key settings"
  end

  self.settings = settings
  self.mimi = ::MadMimi.new settings[:email], settings[:api_key]
end

Instance Attribute Details

#mimiObject

Returns the value of attribute mimi.



9
10
11
# File 'lib/mail/mad_mimi.rb', line 9

def mimi
  @mimi
end

#settingsObject

Returns the value of attribute settings.



9
10
11
# File 'lib/mail/mad_mimi.rb', line 9

def settings
  @settings
end

Instance Method Details

#body(mail, mime_type) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/mail/mad_mimi.rb', line 50

def body(mail, mime_type)
  if part = mail.find_first_mime_type(mime_type)
    part.body.to_s
  elsif mail.mime_type == mime_type
    mail.body.to_s
  end
end

#deliver!(mail) ⇒ Object



58
59
60
61
62
# File 'lib/mail/mad_mimi.rb', line 58

def deliver!(mail)
  mimi.send_mail(options_from_mail(mail), {}).tap do |response|
    raise Error, response if response.to_i.zero?  # no transaction id
  end
end

#html(mail) ⇒ Object



42
43
44
# File 'lib/mail/mad_mimi.rb', line 42

def html(mail)
  body(mail, "text/html")
end

#options_from_mail(mail) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mail/mad_mimi.rb', line 23

def options_from_mail(mail)
  settings.merge(
    :recipients     => mail[:to].to_s,
    :from           => mail[:from].to_s,
    :bcc            => mail[:bcc].to_s,
    :subject        => mail.subject,
    :raw_html       => html(mail),
    :raw_plain_text => text(mail)
  ).tap do |options|
    if mail.respond_to? :mailer_action
      options[:promotion_name] = mail.mailer_action
    end

    options.merge!(mail[:mad_mimi].value) if mail[:mad_mimi]

    options.reject! {|k,v| v.nil? }
  end
end

#text(mail) ⇒ Object



46
47
48
# File 'lib/mail/mad_mimi.rb', line 46

def text(mail)
  body(mail, "text/plain") || mail.body.to_s
end