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.4.0'
Class Attribute Summary collapse
-
.mail_host ⇒ Object
The name of the mail host to use when sending email.
-
.mail_port ⇒ Object
The port to use when sending email.
Instance Attribute Summary collapse
-
#attachments ⇒ Object
An array of file paths to attach to the email.
-
#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 these options:.
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.
63 64 65 66 67 68 69 |
# File 'lib/ez/email.rb', line 63 def initialize( = {}) raise TypeError unless .is_a?(Hash) [:from] ||= "#{Etc.getlogin}@#{Socket.gethostname}" = [] () = end |
Class Attribute Details
.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.
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_port ⇒ Object
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
#attachments ⇒ Object
An array of file paths to attach to the email. Optional.
58 59 60 |
# File 'lib/ez/email.rb', line 58 def end |
#body ⇒ Object
The body of the email. Mandatory.
55 56 57 |
# File 'lib/ez/email.rb', line 55 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.
49 50 51 |
# File 'lib/ez/email.rb', line 49 def from @from end |
#subject ⇒ Object
The subject of the email. Mandatory.
52 53 54 |
# File 'lib/ez/email.rb', line 52 def subject @subject end |
#to ⇒ Object
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() new().deliver 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 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.(from, to) do |stream| if .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 .each do |file_path| (stream, file_path, boundary) end # End boundary stream.puts "--#{boundary}--" end end end end |