Module: Mailer

Defined in:
lib/mailer/deliveries.rb,
lib/mailer.rb,
lib/mailer/email.rb,
lib/mailer/config.rb,
lib/mailer/mailbox.rb,
lib/mailer/version.rb,
lib/mailer/exceptions.rb,
lib/mailer/file_cache.rb,
lib/mailer/test_helpers.rb,
lib/mailer/shoulda_macros/test_unit.rb

Overview

This is just an array of sent tmail Mail objs that Mailer puts sent mails into in test mode

Defined Under Namespace

Modules: ShouldaMacros, TestHelpers, Version Classes: Config, ConfigError, Deliveries, Email, FileCache, Mailbox, MailerError, SendError

Constant Summary collapse

REQUIRED_FIELDS =
[:from, :subject]
ADDRESS_FIELDS =
[:to, :cc, :bcc]
DEFAULT_CONTENT_TYPE =
"text/plain"
DEFAULT_CHARSET =
"UTF-8"
ENVIRONMENT =
{
  :development => 'development',
  :test => 'test',
  :production => 'production'
}

Class Method Summary collapse

Class Method Details

.build_tmail(some_settings) ⇒ Object

Returns a tmail Mail obj based on a hash of settings

> same settings that the .send macro accepts



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mailer.rb', line 63

def self.build_tmail(some_settings)
  settings = some_settings.dup
  settings[:from] ||= @@config.default_from
  mail = TMail::Mail.new

  # Defaulted settings
  mail.date = Time.now
  mail.content_type = DEFAULT_CONTENT_TYPE
  mail.charset = DEFAULT_CHARSET

  # Required settings
  REQUIRED_FIELDS.each {|field| mail.send("#{field}=", settings.delete(field))}

  # Optional settings
  # => settings "pass thru" to the tmail Mail obj
  settings.each do |field, value|
    mail.send("#{field}=", value) if mail.respond_to?("#{field}=")
  end

  mail
end

.configObject



43
44
45
# File 'lib/mailer.rb', line 43

def self.config
  @@config
end

.configure {|@@config| ... } ⇒ Object

Yields:



40
41
42
# File 'lib/mailer.rb', line 40

def self.configure
  yield @@config
end

.deliver_tmail(mail) ⇒ Object

Deliver a tmail Mail obj depending on configured environment

> production?: using Net::SMTP

> test?: add to deliveries cache

> development?: log mail



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mailer.rb', line 89

def self.deliver_tmail(mail)
  check_mail(mail)
  @@config.check
  if @@config.production?
    smtp_start_args = [
      @@config.smtp_server,
      @@config.smtp_port,
      @@config.smtp_helo_domain,
      @@config.smtp_username,
      @@config.smtp_password,
      @@config.smtp_auth_type
    ]
    Net::SMTP.start(*smtp_start_args) do |smtp|
      ADDRESS_FIELDS.each do |field|
        if (recipients = mail.send(field))
          recipients.each {|recipient| smtp.send_message(mail.to_s, mail.from, recipient) }
        end
      end
    end
  elsif @@config.test?
    @@deliveries << mail
  end
  log(:info, "Sent '#{mail.subject}' to #{mail.to ? mail.to.join(', ') : "''"} (#{@@config.environment})")
  log_tmail(mail)
end

.deliveriesObject



48
49
50
# File 'lib/mailer.rb', line 48

def self.deliveries
  @@deliveries
end

.developmentObject



29
30
31
# File 'lib/mailer.rb', line 29

def self.development
  ENVIRONMENT[:development]
end

.log_tmail(mail) ⇒ Object

Logs a tmail Mail obj delivery



116
117
118
119
120
121
122
123
124
# File 'lib/mailer.rb', line 116

def self.log_tmail(mail)
  log(:debug, [
    "",
    "====================================================================",
    mail.to_s,
    "====================================================================",
    ""
  ].join("\n"))
end

.productionObject



35
36
37
# File 'lib/mailer.rb', line 35

def self.production
  ENVIRONMENT[:production]
end

.send(settings = {}) ⇒ Object

Macro style helper for sending email based on the Mailer configuration



53
54
55
56
57
58
59
# File 'lib/mailer.rb', line 53

def self.send(settings={})
  mail = build_tmail(settings)
  mail.body = yield(mail) if block_given?
  mail.body ||= ''
  deliver_tmail(mail)
  mail
end

.testObject



32
33
34
# File 'lib/mailer.rb', line 32

def self.test
  ENVIRONMENT[:test]
end