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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#smtp_serverObject

The smtp server to sent email to



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

def smtp_server
  @smtp_server
end

Class Method Details

.emails_sentObject

The array of emails sent



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

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.



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

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)


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

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.



44
45
46
# File 'lib/simple_mailer.rb', line 44

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.



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

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