simpleemail 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ === 1.0.0 / 2008-08-05
2
+ Initial release of SimpleEmail.
3
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Designing Patterns
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ 'Software'), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
1
+ Manifest.txt
2
+ LICENSE
3
+ History.txt
4
+ README.txt
5
+ Rakefile
6
+ lib/simpleemail.rb
7
+ lib/simpleemail/action_mailer_ssl.rb
8
+ lib/simpleemail/email.rb
9
+ test/emailsettings.rb
10
+ test/test_simpleemail.rb
@@ -0,0 +1,173 @@
1
+ = simpleemail
2
+ * Project Page: http://rubyforge.org/projects/simpleemail/
3
+ * Documentation: http://simpleemail.rubyforge.org/
4
+
5
+ == DESCRIPTION:
6
+
7
+ This package simplifies sending emails outside of the Rails
8
+ environment. It is a wrapper around the ActionMailer package.
9
+ Support for smtp over tls is included if you are using Ruby 1.8.7 or
10
+ above. The API provided is very bare, but can be easily extended.
11
+ The email configuration is provided through a user-specified
12
+ configuration file (identical to the ActionMailer configuration in
13
+ environment.rb in Rails except for the added tls option). This
14
+ package is most useful in the situation that a user has a number of
15
+ scripts (outside of the Rails environment) that all send very basic
16
+ emails (to, from, body, subject).
17
+
18
+ == PROBLEMS:
19
+
20
+ None (known).
21
+
22
+ == SYNOPSIS:
23
+
24
+ Lets walk through some example invocations of the library.
25
+
26
+ Here is an example configuration file that specifies sendmail as the delivery method.
27
+ ======<tt>examples/email_settings_sendmail.rb</tt>:
28
+ ActionMailer::Base.delivery_method = :sendmail
29
+
30
+
31
+ This script sends a basic email to two receivers via sendmail.
32
+ ======<tt>examples/email_results_1.rb</tt>:
33
+ #!/usr/bin/env ruby
34
+
35
+ require 'rubygems'
36
+ require 'simpleemail'
37
+
38
+
39
+ SimpleEmail::Email.email_settings_file = "email_settings_sendmail.rb"
40
+
41
+ subject = "Re: This is a test!"
42
+ body = "..."
43
+
44
+ SimpleEmail::Email.send_email("tony",
45
+ "janet, tony",
46
+ subject,
47
+ body)
48
+
49
+
50
+
51
+ Here is an example configuration file that specifies smtp as the delivery method.
52
+ ======<tt>examples/email_settings_smtp.rb</tt>:
53
+ ActionMailer::Base.delivery_method = :smtp
54
+
55
+ ActionMailer::Base.smtp_settings = {
56
+ :address => 'smtp.gmail.com',
57
+ :port => 587,
58
+ :domain => "gmail.com",
59
+ :authentication => "login",
60
+ :user_name => "sender",
61
+ :password => "zepassword",
62
+ :tls => :auto
63
+ }
64
+
65
+ SimpleEmail::Email.default_from = 'sender@gmail.com'
66
+ SimpleEmail::Email.default_to = 'receiver@gmail.com'
67
+
68
+
69
+
70
+ This script sends a basic email via smtp with tls to be enabled if the
71
+ smtp server supports it. The default to and from fields from the
72
+ configuration file are used.
73
+ ======<tt>examples/email_results_2.rb</tt>:
74
+ #!/usr/bin/env ruby
75
+
76
+ require 'rubygems'
77
+ require 'simpleemail'
78
+
79
+
80
+ SimpleEmail::Email.email_settings_file = "email_settings_smtp.rb"
81
+
82
+ subject = "Re: This is a test!"
83
+ body = "..."
84
+
85
+ SimpleEmail::Email.send_email(nil, nil, subject, body)
86
+
87
+
88
+ == CONFIGURATION FILE:
89
+
90
+ The configuration file allows the user to configure the
91
+ ActionMailer::Base class, in the same fashion that it is configured in
92
+ environment.rb in the Rails environment. This library includes one
93
+ additional option
94
+
95
+ [<tt>ActionMailer::Base.smtp_settings[:tls]</tt>]
96
+ Whether tls will be enabled. Possible values are <tt>:always,
97
+ :never, :auto</tt>. If <tt>:auto</tt> is specified, tls will be
98
+ enabled if the smtp server offers tls AND the version of ruby is
99
+ such that the <tt>net/smtp</tt> library supports tls. If not
100
+ specified, this setting will default to <tt>:auto</tt>.
101
+
102
+ The tls option will only work with Ruby version 1.8.7 and above. This
103
+ is due to changes to the ruby standard library (as of 1.8.7) to
104
+ include tls support in the net/smtp library. For a detailed
105
+ explanation of the ActionMailer configuration, see the {Rails
106
+ ActionMailer
107
+ documentation}[http://api.rubyonrails.org/classes/ActionMailer/Base.html]
108
+ in the {Ruby On Rails API}[http://api.rubyonrails.org].
109
+
110
+ The configuration file also allows the user to specify a default from
111
+ email address and default to email address(es).
112
+ * <tt>SimpleEmail::Email.default_from</tt> - Optional - The default from address.
113
+ * <tt>SimpleEmail::Email.default_to</tt> - Optional - The default to address(es).
114
+
115
+ The user can also specify the to/from fields as parameters in each
116
+ call to SimpleEmail::Email.send_email. If specified (the parameters
117
+ passed are non-nil), they will override the default to/from fields.
118
+
119
+ == REQUIREMENTS:
120
+
121
+ This package requires actionmailer, hoe, and relative. Hoe and
122
+ relative are required only for running the tests.
123
+
124
+ == INSTALL:
125
+
126
+ sudo gem install simpleemail
127
+
128
+ == AUTHORS:
129
+ === Designing Patterns
130
+ * Homepage: http://www.designingpatterns.com
131
+ * Blogs: http://blogs.designingpatterns.com
132
+
133
+ == SUPPORT:
134
+ Please post questions, concerns, or requests for enhancement to the forums on
135
+ the project page. Alternatively, direct contact information for
136
+ Designing Patterns can be found on the project page for this gem.
137
+
138
+ == ENHANCEMENTS:
139
+ Please feel free to contact us with any ideas; we will try our best to
140
+ enhance the software and respond to user requests. Of course, we are more
141
+ likely to work on a particular enhancement if we know that there are users
142
+ who want it. Designing Patterns provides contracting and consulting services,
143
+ so if there is an enhancement that *must* get done (and in a specified time
144
+ frame), please inquire about retaining our services!
145
+
146
+ == LICENSE:
147
+ The license text can be found in the +LICENSE+ file at the root of the
148
+ distribution.
149
+
150
+ This package is licensed with an MIT license:
151
+
152
+ Copyright (c) 2008 Designing Patterns
153
+
154
+ Permission is hereby granted, free of charge, to any person obtaining
155
+ a copy of this software and associated documentation files (the
156
+ 'Software'), to deal in the Software without restriction, including
157
+ without limitation the rights to use, copy, modify, merge, publish,
158
+ distribute, sublicense, and/or sell copies of the Software, and to
159
+ permit persons to whom the Software is furnished to do so, subject to
160
+ the following conditions:
161
+
162
+ The above copyright notice and this permission notice shall be
163
+ included in all copies or substantial portions of the Software.
164
+
165
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
166
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
167
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
168
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
169
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
170
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
171
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
172
+
173
+ == SHARE AND ENJOY!
@@ -0,0 +1,13 @@
1
+ # -*- ruby -*-
2
+ require 'rubygems'
3
+ require 'hoe'
4
+
5
+ Hoe.new('simpleemail', "1.0.0") do |p|
6
+ p.remote_rdoc_dir = ''
7
+ p.developer('DesigningPatterns', 'technical.inquiries@designingpatterns.com')
8
+
9
+ p.extra_deps << ['actionmailer']
10
+ p.extra_deps << ['relative']
11
+ end
12
+
13
+ # vim: syntax=Ruby
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ require 'simpleemail/email'
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'action_mailer'
3
+
4
+ #
5
+ # This file enhances the ActionMailer::Base class's smtp code to read
6
+ # a new ActionMailer::Base.smtp_settings configuration parameter (tls)
7
+ # and enable tls on the smtp connection based on this parameter.
8
+ #
9
+
10
+ module ActionMailer #:nodoc:
11
+
12
+ class Base
13
+
14
+ undef perform_delivery_smtp
15
+
16
+ def perform_delivery_smtp(mail)
17
+ destinations = mail.destinations
18
+ mail.ready_to_send
19
+ sender = mail['return-path'] || mail.from
20
+
21
+ smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
22
+
23
+ if (logger && logger.level <= Logger::DEBUG)
24
+ smtp.debug_output = logger
25
+ end
26
+
27
+ # The tls methods should be available in the net/smtp library
28
+ # for ruby 1.8.7 and above.
29
+ case smtp_settings[:tls]
30
+ when :always
31
+ smtp.enable_starttls()
32
+ when :auto
33
+ smtp.enable_starttls_auto()
34
+ when nil
35
+ # if no tls setting was specified and the net/smtp library
36
+ # supports tls, enable tls to automatically be enabled if the
37
+ # smtp server supports it.
38
+ if (smtp.respond_to?(:enable_starttls_auto))
39
+ smtp.enable_starttls_auto()
40
+ end
41
+ when :never
42
+ else
43
+ raise Exception "Unexpected value for smtp setting (tls): Available values: (:always, :auto, :never)"
44
+ end
45
+
46
+ begin
47
+ smtp.start(smtp_settings[:domain], smtp_settings[:user_name],
48
+ smtp_settings[:password], smtp_settings[:authentication])
49
+
50
+ smtp.sendmail(mail.encoded, sender, destinations)
51
+ ensure
52
+ if (smtp.started?()) then smtp.finish() end
53
+ end
54
+
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,85 @@
1
+ require 'simpleemail/action_mailer_ssl'
2
+
3
+ module SimpleEmail
4
+
5
+ # This class provides basic email functionality. It is a wrapper
6
+ # around the ActionMailer package.
7
+ class Email
8
+ @@email_settings_file = nil
9
+ @@default_to = nil
10
+ @@default_from = nil
11
+
12
+ class << self
13
+ undef_method :new
14
+ end
15
+
16
+ def self.email_settings_file(email_settings_file)
17
+ @@email_settings_file
18
+ end
19
+
20
+ def self.email_settings_file=(email_settings_file)
21
+ @@email_settings_file = email_settings_file
22
+
23
+ eval(IO.read(@@email_settings_file))
24
+
25
+ # Warn the user if they're specifying the tls smtp setting and
26
+ # their ruby version does not support this.
27
+ if ((ActionMailer::Base.smtp_settings['tls'] == :always || ActionMailer::Base.smtp_settings['tls'] == :auto) &&
28
+ RUBY_VERSION < "1.8.7" &&
29
+ logger = ActionMalier::Base.logger)
30
+ logger.warn("Tls is not supported by the net/smtp library for versions earlier than 1.8.7.")
31
+ end
32
+ end
33
+
34
+ def self.default_to()
35
+ @@default_to
36
+ end
37
+
38
+ def self.default_to=(default_to)
39
+ @@default_to = default_to
40
+ end
41
+
42
+ def self.default_from()
43
+ @@default_from
44
+ end
45
+
46
+ def self.default_from=(default_from)
47
+ @@default_from = default_from
48
+ end
49
+
50
+ #
51
+ # ====Description:
52
+ # This method sends an email to the specified to address.
53
+ #
54
+ # ====Parameters:
55
+ # [from]
56
+ # The from address. If this parameter's value is nil and a
57
+ # default from address was specified, the default from
58
+ # address will be used.
59
+ # [to]
60
+ # The to address. If this parameter's value is nil and a
61
+ # default to address was specified, the default to address
62
+ # will be used.
63
+ #
64
+ def self.send_email(from, to, subject, body)
65
+ if !from then from = @@default_from end
66
+ if !to then to = @@default_to end
67
+
68
+ GeneralMailer.deliver_simple_email(from, to, subject, body)
69
+ end
70
+ end
71
+
72
+ class GeneralMailer < ActionMailer::Base #:nodoc:
73
+
74
+ def simple_email(the_sender, the_recipients, the_subject, the_body)
75
+ recipients(the_recipients)
76
+ from(the_sender)
77
+ subject(the_subject)
78
+
79
+ #setting the body explicitly means we don't have to provide a
80
+ #separate template file
81
+ body(the_body)
82
+ end
83
+ end
84
+ end
85
+
@@ -0,0 +1,15 @@
1
+ ActionMailer::Base.delivery_method = :smtp
2
+
3
+ ActionMailer::Base.smtp_settings = {
4
+ :address => 'smtp.gmail.com',
5
+ :port => 587,
6
+ :domain => "gmail.com",
7
+ :authentication => "login",
8
+ :user_name => "sender",
9
+ :password => "zepassword",
10
+ :tls => :auto
11
+ }
12
+
13
+ SimpleEmail::Email.default_from = 'sender@gmail.com'
14
+ SimpleEmail::Email.default_to = 'receiver@gmail.com'
15
+
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'simpleemail'
4
+
5
+ require 'rubygems'
6
+ require 'relative'
7
+
8
+ require 'test/unit'
9
+
10
+ class TestSimpleEmail < Test::Unit::TestCase
11
+
12
+ def test_email()
13
+ send_and_verify()
14
+
15
+ from = "override_sender@gmail.com"
16
+ to = "override_receiver1@gmail.com, override_receiver2@gmail.com"
17
+ send_and_verify(from, to)
18
+ end
19
+
20
+ def send_and_verify(from = nil, to = nil)
21
+ SimpleEmail::Email.email_settings_file =
22
+ Pathname.new("emailsettings.rb").expand_path_relative_to_caller()
23
+
24
+ ActionMailer::Base.delivery_method = :test
25
+ ActionMailer::Base.deliveries = []
26
+
27
+ subject = "Subject"
28
+ body = "Testing\n"
29
+ body << "1\n"
30
+ body << "2\n"
31
+ body << "3\n"
32
+
33
+ SimpleEmail::Email.send_email(from, to, subject, body)
34
+
35
+ deliveries = ActionMailer::Base.deliveries
36
+ assert_equal(deliveries.length, 1)
37
+
38
+ delivery = deliveries[0]
39
+ assert_equal(delivery.subject, subject)
40
+ assert_equal(delivery.body, body)
41
+
42
+ actual_to = (to ? to : SimpleEmail::Email.default_to).split(/, +/)
43
+ actual_from = (from ? from : SimpleEmail::Email.default_from).split(/, +/)
44
+
45
+ assert_equal(delivery.to, actual_to)
46
+ assert_equal(delivery.from, actual_from)
47
+
48
+
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simpleemail
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - DesigningPatterns
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-05 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: actionmailer
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: relative
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.7.0
44
+ version:
45
+ description: This package simplifies sending emails outside of the Rails environment. It is a wrapper around the ActionMailer package. Support for smtp over tls is included if you are using Ruby 1.8.7 or above. The API provided is very bare, but can be easily extended. The email configuration is provided through a user-specified configuration file (identical to the ActionMailer configuration in environment.rb in Rails except for the added tls option). This package is most useful in the situation that a user has a number of scripts (outside of the Rails environment) that all send very basic emails (to, from, body, subject).
46
+ email:
47
+ - technical.inquiries@designingpatterns.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - Manifest.txt
54
+ - History.txt
55
+ - README.txt
56
+ files:
57
+ - Manifest.txt
58
+ - LICENSE
59
+ - History.txt
60
+ - README.txt
61
+ - Rakefile
62
+ - lib/simpleemail.rb
63
+ - lib/simpleemail/action_mailer_ssl.rb
64
+ - lib/simpleemail/email.rb
65
+ - test/emailsettings.rb
66
+ - test/test_simpleemail.rb
67
+ has_rdoc: true
68
+ homepage: "Project Page: http://rubyforge.org/projects/simpleemail/"
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --main
72
+ - README.txt
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ requirements: []
88
+
89
+ rubyforge_project: simpleemail
90
+ rubygems_version: 1.2.0
91
+ signing_key:
92
+ specification_version: 2
93
+ summary: This package simplifies sending emails outside of the Rails environment
94
+ test_files:
95
+ - test/test_simpleemail.rb