Class: EZ::Email
- Inherits:
-
Object
- Object
- EZ::Email
- 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
-
#body ⇒ Object
The body of the email.
-
#from ⇒ Object
A single email address from whom the email is being delivered.
-
#subject ⇒ Object
The subject of the email.
-
#to ⇒ Object
A single email address or an array of email addresses to whom the email should be sent.
Class Method Summary collapse
-
.deliver(options) ⇒ Object
Delivers a simple text email message using four options:.
-
.mail_host ⇒ Object
The name of the mail host to use when sending email.
-
.mail_host=(host) ⇒ Object
Sets the mail host.
-
.mail_port ⇒ Object
The port to use when sending email.
-
.mail_port=(port) ⇒ Object
Sets the mail port.
Instance Method Summary collapse
-
#deliver ⇒ Object
Sends the email based on the options passed to the constructor.
-
#initialize(options = {}) ⇒ Email
constructor
Creates a new EZ::Email object.
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.
64 65 66 67 68 69 |
# File 'lib/ez/email.rb', line 64 def initialize(={}) raise TypeError unless .is_a?(Hash) [:from] ||= Etc.getlogin + '@' + Socket.gethostname () @options = end |
Instance Attribute Details
#body ⇒ Object
The body of the email. Mandatory.
59 60 61 |
# File 'lib/ez/email.rb', line 59 def body @body end |
#from ⇒ Object
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 |
#subject ⇒ Object
The subject of the email. Mandatory.
56 57 58 |
# File 'lib/ez/email.rb', line 56 def subject @subject end |
#to ⇒ Object
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() new().deliver end |
.mail_host ⇒ Object
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_port ⇒ Object
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
#deliver ⇒ Object
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.(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 |