Module: SimpleMailer

Extended by:
SimpleMailer
Included in:
SimpleMailer
Defined in:
lib/simple_mailer.rb

Overview

SimpleMailer is a very small class that uses net/smtp to sent email messages to a given smtp server (localhost by default). Like the name implies, it is very simple, you provide a from address, to address, subject, body and an optional hash of headers.

The main advantage of this over just using Net::SMTP directly is that it handles headers and it has a test mode that just records the number of emails sent instead of actually sending the messages.

Constant Summary collapse

DEFAULT_SMTP_HOST =
'localhost'.freeze
DEFAULT_SMTP_DOMAIN =
'localhost'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#smtp_serverObject

The smtp server to sent email to



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

def smtp_server
  @smtp_server
end

Class Method Details

.emails_sentObject

The array of emails sent



21
22
23
# File 'lib/simple_mailer.rb', line 21

def self.emails_sent
  @emails_sent
end

.test_mode!Object

Turn on the test mode. There is no method given to turn it off. While in test mode, messages will not be sent, but the messages that would have been sent are available via emails_sent. This method also resets the emails_sent variable to the empty array.



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

def self.test_mode!
  @emails_sent.clear
  @test_mode = true
end

.test_mode?Boolean

Whether we are in simple mailer’s test mode

Returns:

  • (Boolean)


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

def self.test_mode?
  @test_mode
end

Instance Method Details

#emails_sentObject

The emails sent in test mode. Is an array of arrays. Each element array is a array of three elements, the message, from address, and to address.



55
56
57
# File 'lib/simple_mailer.rb', line 55

def emails_sent
  SimpleMailer.emails_sent
end

#send_email(from, to, subject, message, headers = {}) ⇒ Object

Formats email message using from address, to address, subject, message, and header hash. Arguments:

  • from - From address for the message

  • to - To address for the message (string or array of strings)

  • subject - Subject of the message

  • message - Body of the message

  • headers - Headers for the message. Also, handles the following keys specially:

    • :smtp_from - the SMTP MAIL FROM address to use. Uses the value of of the from argument by default.

    • :smtp_to - the SMTP RCPT TO address to use. Uses the value of the to argument by default.

The caller is responsible for ensuring that the from, to, subject, and headers do not have an carriage returns or line endings. Otherwise, it’s possible to inject arbitrary headers or body content.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/simple_mailer.rb', line 75

def send_email(from, to, subject, message, headers={})
  headers = headers.dup
  smtp_from = headers.delete(:smtp_from) || from
  smtp_to = headers.delete(:smtp_to) || to
  if cc = headers.delete(:cc)
    headers['CC'] = cc
    smtp_to = Array(smtp_to) + Array(cc)
  end
  if bcc = headers.delete(:bcc)
    smtp_to = Array(smtp_to) + Array(bcc)
  end

  to = to.join(", ") if to.is_a?(Array)
  _send_email(<<END_OF_MESSAGE, smtp_from, smtp_to)
From: #{from}
To: #{to}
Subject: #{subject}
#{headers.sort.map{|k,v| "#{k}: #{v}"}.join("\n")}#{"\n" unless headers.empty?}
#{message}
END_OF_MESSAGE
end

#smtp_settingsObject

Custom SMTP configuration for any service (taken from the “mail” gem)



43
44
45
46
47
48
49
50
# File 'lib/simple_mailer.rb', line 43

def smtp_settings
  @smtp_settings ||= {
    :address              => smtp_server || DEFAULT_SMTP_HOST,
    :port                 => 25,
    :enable_starttls_auto => true,
    :domain               => DEFAULT_SMTP_DOMAIN,
  }
end