Class: EZ::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/ez/email.rb

Overview

The Email class encapsulates certain SMTP attributes, which are then used to send simple emails.

Constant Summary collapse

VERSION =

The version of the ez-email library

'0.4.0'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Email

Creates a new EZ::Email object. As a general rule you won’t use this method, but should use EZ::Email.deliver instead.

Raises:

  • (TypeError)


63
64
65
66
67
68
69
# File 'lib/ez/email.rb', line 63

def initialize(options = {})
  raise TypeError unless options.is_a?(Hash)
  options[:from] ||= "#{Etc.getlogin}@#{Socket.gethostname}"
  @attachments = []
  validate_options(options)
  @options = options
end

Class Attribute Details

.mail_hostObject

The name of the mail host to use when sending email. The default is whatever the address of your system’s ‘mailhost’ resolves to. If that cannot be determined, your localhost is used.



29
30
31
32
33
# File 'lib/ez/email.rb', line 29

def mail_host
  @mail_host ||= Resolv.getaddress('mailhost')
rescue Resolv::ResolvError
  @mail_host ||= 'localhost'
end

.mail_portObject

The port to use when sending email. The default is 25.



37
38
39
# File 'lib/ez/email.rb', line 37

def mail_port
  @mail_port ||= 25
end

Instance Attribute Details

#attachmentsObject

An array of file paths to attach to the email. Optional.



58
59
60
# File 'lib/ez/email.rb', line 58

def attachments
  @attachments
end

#bodyObject

The body of the email. Mandatory.



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

def body
  @body
end

#fromObject

A single email address from whom the email is being delivered. The default is your login + ‘@’ + your host name, though it is recommended that you specify it explicitly.



49
50
51
# File 'lib/ez/email.rb', line 49

def from
  @from
end

#subjectObject

The subject of the email. Mandatory.



52
53
54
# File 'lib/ez/email.rb', line 52

def subject
  @subject
end

#toObject

A single email address or an array of email addresses to whom the email should be sent. Mandatory.



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

def to
  @to
end

Class Method Details

.deliver(options) ⇒ Object

Delivers a simple text email message using these options:

  • to

  • from

  • subject

  • body

  • attachments (optional)

Examples:

# Send an email to a single user
EZ::Email.deliver(
   :to      => '[email protected]',
   :from    => '[email protected]',
   :subject => 'Hi',
   :body    => 'How are you?'
)

# Send an email to a multiple users
EZ::Email.deliver(
   :to      => ['[email protected]', '[email protected]'],
   :from    => '[email protected]',
   :subject => 'Hi',
   :body    => 'How are you?'
)

# Send an email with attachments
EZ::Email.deliver(
   :to          => '[email protected]',
   :from        => '[email protected]',
   :subject     => 'Files attached',
   :body        => 'Please find the attached files.',
   :attachments => ['/path/to/file1.pdf', '/path/to/file2.txt']
)

This is a shortcut for EZ::Email.new + EZ::Email#deliver.



158
159
160
# File 'lib/ez/email.rb', line 158

def self.deliver(options)
  new(options).deliver
end

Instance Method Details

#deliverObject

Sends the email based on the options passed to the constructor. As a general rule you won’t use this method directly, but should use EZ::Email.deliver instead.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
114
115
116
117
118
119
# File 'lib/ez/email.rb', line 75

def deliver
  host = EZ::Email.mail_host
  port = EZ::Email.mail_port

  to_list = to.is_a?(Array) ? to : [to]

  Net::SMTP.start(host, port, host) do |smtp|
    smtp.open_message_stream(from, to) do |stream|
      if attachments.empty?
        # Simple email without attachments
        stream.puts "From: #{from}"
        stream.puts "To: #{to_list.join(', ')}"
        stream.puts "Subject: #{subject}"
        stream.puts
        stream.puts body
      else
        # Email with attachments - use multipart MIME
        boundary = generate_boundary

        stream.puts "From: #{from}"
        stream.puts "To: #{to_list.join(', ')}"
        stream.puts "Subject: #{subject}"
        stream.puts "MIME-Version: 1.0"
        stream.puts "Content-Type: multipart/mixed; boundary=\"#{boundary}\""
        stream.puts

        # Text body part
        stream.puts "--#{boundary}"
        stream.puts "Content-Type: text/plain; charset=UTF-8"
        stream.puts "Content-Transfer-Encoding: 7bit"
        stream.puts
        stream.puts body
        stream.puts

        # Attachment parts
        attachments.each do |file_path|
          add_attachment(stream, file_path, boundary)
        end

        # End boundary
        stream.puts "--#{boundary}--"
      end
    end
  end
end