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.1.5'
@@mail_port =
25

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)


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

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

Instance Attribute Details

#bodyObject

The body of the email. Mandatory.



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

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.



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

def from
  @from
end

#subjectObject

The subject of the email. Mandatory.



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

def subject
  @subject
end

#toObject

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



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

def to
  @to
end

Class Method Details

.deliver(options) ⇒ Object

Delivers a simple text email message using four options:

  • to

  • from

  • subject

  • body

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?'
)

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



117
118
119
# File 'lib/ez/email.rb', line 117

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

.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.



27
28
29
# File 'lib/ez/email.rb', line 27

def self.mail_host
  @@mail_host
end

.mail_host=(host) ⇒ Object

Sets the mail host.



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

def self.mail_host=(host)
  @@mail_host = host
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 self.mail_port
  @@mail_port
end

.mail_port=(port) ⇒ Object

Sets the mail port.



42
43
44
# File 'lib/ez/email.rb', line 42

def self.mail_port=(port)
  @@mail_port = 25
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
# File 'lib/ez/email.rb', line 75

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

  Net::SMTP.start(host, port, host){ |smtp|
    smtp.open_message_stream(self.from, self.to){ |stream|
      stream.puts "From: #{self.from}"
      stream.puts "To: " + self.to.to_a.join(', ')
      stream.puts "Subject: #{self.subject}"
      stream.puts
      stream.puts self.body
    }
  }
end