ez-email 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data/CHANGES +2 -0
  2. data/MANIFEST +6 -0
  3. data/README +37 -0
  4. data/lib/ez/email.rb +135 -0
  5. data/test/test_ez_email.rb +76 -0
  6. metadata +62 -0
data/CHANGES ADDED
@@ -0,0 +1,2 @@
1
+ == 0.1.0 - 23-Jan-2009
2
+ * Initial release
data/MANIFEST ADDED
@@ -0,0 +1,6 @@
1
+ * CHANGES
2
+ * MANIFEST
3
+ * Rakefile
4
+ * ez-email.gemspec
5
+ * lib/ez/email.rb
6
+ * test/test_ez_email.rb
data/README ADDED
@@ -0,0 +1,37 @@
1
+ = Description
2
+ A very easy interface for sending simple text based emails.
3
+
4
+ = Synopsis
5
+ require 'ez/email'
6
+
7
+ EZ::Email.deliver(
8
+ :to => 'your_friend@hotstuff.com',
9
+ :from => 'you@blah.com',
10
+ :subject => 'Hello',
11
+ :body => 'How are you?'
12
+ )
13
+
14
+ = Installation
15
+ gem install ez-email (rubygems)
16
+ rake install (manual installation)
17
+
18
+ = Rationale
19
+ The present batch of email handling libraries are either not designed
20
+ for sending email, or are extremely cumbersome, have lousy interfaces,
21
+ or are no longer maintained.
22
+
23
+ I just want to sent a flippin' email! This library scratched that itch.
24
+ Hopefully you will find its simplicity useful, too.
25
+
26
+ = Bugs
27
+ None that I'm aware of. Please log any bug reports on the project page at
28
+ http://www.rubyforge.org/projects/shards.
29
+
30
+ = License
31
+ Ruby's
32
+
33
+ = Copyright
34
+ (C) 2009, Daniel J. Berger, All Rights Reserved
35
+
36
+ = Author
37
+ Daniel Berger
data/lib/ez/email.rb ADDED
@@ -0,0 +1,135 @@
1
+ require 'resolv'
2
+ require 'etc'
3
+ require 'socket'
4
+ require 'net/smtp'
5
+
6
+ # The EZ module serves as a namespace only.
7
+ module EZ
8
+
9
+ # The Email class encapsulates certain SMTP attributes, which are then
10
+ # used to send simple emails.
11
+ class Email
12
+ # The version of this library
13
+ VERSION = '0.1.0'
14
+
15
+ @@mail_host = Resolv.getaddress('mailhost')
16
+ @@mail_port = 25
17
+
18
+ # The name of the mail host to use when sending email. The default
19
+ # is whatever the address of your system's 'mailhost' resolves to.
20
+ def self.mail_host
21
+ @@mail_host
22
+ end
23
+
24
+ # Sets the mail host.
25
+ def self.mail_host=(host)
26
+ @@mail_host = host
27
+ end
28
+
29
+ # The port to use when sending email. The default is 25.
30
+ def self.mail_port
31
+ @@mail_port
32
+ end
33
+
34
+ # Sets the mail port.
35
+ def self.mail_port=(port)
36
+ @@mail_port = 25
37
+ end
38
+
39
+ # A single email address or an array of email addresses to whom
40
+ # the email should be sent. Mandatory.
41
+ attr_accessor :to
42
+
43
+ # A single email address from whom the email is being delivered. The
44
+ # default is your login + '@' + your host name, though it is recommended
45
+ # that you specify it explicitly.
46
+ attr_accessor :from
47
+
48
+ # The subject of the email. Mandatory.
49
+ attr_accessor :subject
50
+
51
+ # The body of the email. Mandatory.
52
+ attr_accessor :body
53
+
54
+ # Creates a new EZ::Email object. As a general rule you won't use
55
+ # this method, but should use EZ::Email.deliver instead.
56
+ #
57
+ def initialize(options={})
58
+ raise TypeError unless options.is_a?(Hash)
59
+ validate_options(options)
60
+ @options = options
61
+ end
62
+
63
+ # Sends the email based on the options passed to the constructor.
64
+ # As a general rule you won't use this method directly, but should
65
+ # use EZ::Email.deliver instead.
66
+ #
67
+ def deliver
68
+ host = EZ::Email.mail_host
69
+ port = EZ::Email.mail_port
70
+
71
+ Net::SMTP.start(host, port, host){ |smtp|
72
+ smtp.open_message_stream(self.from, self.to){ |stream|
73
+ stream.puts "From: #{self.from}"
74
+ stream.puts "To: " + self.to.to_a.join(', ')
75
+ stream.puts "Subject: #{self.subject}"
76
+ stream.puts
77
+ stream.puts self.body
78
+ }
79
+ }
80
+ end
81
+
82
+ # Delivers a simple text email message using four options:
83
+ #
84
+ # * to
85
+ # * from
86
+ # * subject
87
+ # * body
88
+ #
89
+ # Examples:
90
+ #
91
+ # # Send an email to a single user
92
+ # EZ::Email.deliver(
93
+ # :to => 'some_user@hotmail.com',
94
+ # :from => 'me@hotmail.com',
95
+ # :subject => 'Hi',
96
+ # :body => 'How are you?'
97
+ # )
98
+ #
99
+ # # Send an email to a multiple users
100
+ # EZ::Email.deliver(
101
+ # :to => ['jon@hotmail.com', 'mary@hotmail.com'],
102
+ # :from => 'me@hotmail.com',
103
+ # :subject => 'Hi',
104
+ # :body => 'How are you?'
105
+ # )
106
+ #
107
+ # This is a shortcut for EZ::Email.new + EZ::Email.deliver.
108
+ #
109
+ def self.deliver(options)
110
+ new(options).deliver
111
+ end
112
+
113
+ private
114
+
115
+ # Private method that both validates the hash options, and sets
116
+ # the attribute values.
117
+ #
118
+ def validate_options(hash)
119
+ valid = %w/to from subject body/
120
+ hash.each{ |key, value|
121
+ key = key.to_s.downcase
122
+ raise ArgumentError unless valid.include?(key)
123
+ send("#{key}=", value)
124
+ }
125
+
126
+ if to.nil? || subject.nil? || body.nil?
127
+ raise ArgumentError, "Missing :to, :subject or :body"
128
+ end
129
+
130
+ if from.nil?
131
+ from = Etc.getlogin + '@' + Socket.gethostname
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,76 @@
1
+ ########################################################################
2
+ # test_ez_email.rb
3
+ #
4
+ # Test suite for the EZ::Email library.
5
+ ########################################################################
6
+ require 'test/unit'
7
+ require 'ez/email'
8
+
9
+ class TC_EZ_Email < Test::Unit::TestCase
10
+ def setup
11
+ @to = 'foo@some_mail_service.com'
12
+ @from = 'bar@some_mail_service.com'
13
+ @subj = 'This is a test'
14
+ @body = 'How are you?'
15
+
16
+ @opts = {:to => @to, :from => @from, :subject => @subj, :body => @body }
17
+ @email = EZ::Email.new(@opts)
18
+ end
19
+
20
+ def test_version
21
+ assert_equal('0.1.0', EZ::Email::VERSION)
22
+ end
23
+
24
+ def test_to
25
+ assert_respond_to(@email, :to)
26
+ assert_respond_to(@email, :to=)
27
+ assert_equal(@to, @email.to)
28
+ end
29
+
30
+ def test_from
31
+ assert_respond_to(@email, :from)
32
+ assert_respond_to(@email, :from=)
33
+ assert_equal(@from, @email.from)
34
+ end
35
+
36
+ def test_subject
37
+ assert_respond_to(@email, :subject)
38
+ assert_respond_to(@email, :subject=)
39
+ assert_equal(@subj, @email.subject)
40
+ end
41
+
42
+ def test_body
43
+ assert_respond_to(@email, :body)
44
+ assert_respond_to(@email, :body=)
45
+ assert_equal(@body, @email.body)
46
+ end
47
+
48
+ def test_mail_host
49
+ assert_respond_to(EZ::Email, :mail_host)
50
+ assert_respond_to(EZ::Email, :mail_host=)
51
+ assert_kind_of(String, EZ::Email.mail_host)
52
+ end
53
+
54
+ def test_mail_port
55
+ assert_respond_to(EZ::Email, :mail_port)
56
+ assert_respond_to(EZ::Email, :mail_port=)
57
+ assert_equal(25, EZ::Email.mail_port)
58
+ end
59
+
60
+ def test_deliver
61
+ assert_respond_to(EZ::Email, :deliver)
62
+ end
63
+
64
+ def test_invalid_option_raises_error
65
+ assert_raise(ArgumentError){ EZ::Email.send(:new, {:bogus => 77}) }
66
+ end
67
+
68
+ def teardown
69
+ @to = nil
70
+ @from = nil
71
+ @subj = nil
72
+ @body = nil
73
+ @opts = nil
74
+ @email = nil
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ez-email
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Berger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-23 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A very simple interface for sending email
17
+ email: djberg96@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - CHANGES
25
+ - MANIFEST
26
+ files:
27
+ - lib/ez
28
+ - lib/ez/email.rb
29
+ - test/test_ez_email.rb
30
+ - README
31
+ - CHANGES
32
+ - MANIFEST
33
+ has_rdoc: true
34
+ homepage: http://www.rubyforge.org/projects/shards
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project: shards
57
+ rubygems_version: 1.3.1
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Really easy emails
61
+ test_files:
62
+ - test/test_ez_email.rb