fast-mailer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENCE +20 -0
- data/README.md +70 -0
- data/VERSION +1 -0
- data/lib/fast-mailer.rb +5 -0
- data/lib/fast-mailer/configuration.rb +29 -0
- data/lib/fast-mailer/smtp.rb +79 -0
- metadata +159 -0
data/LICENCE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Erik Hansson
|
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
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
FastMailer
|
2
|
+
==========
|
3
|
+
|
4
|
+
FastMailer is a collection of helpers that aims to help with sending email.
|
5
|
+
Specifically, it allows for convenient configuration and better throughput
|
6
|
+
when sending multiple mails.
|
7
|
+
|
8
|
+
|
9
|
+
Configuration
|
10
|
+
-------------
|
11
|
+
|
12
|
+
The FastMailer senders are configured with a regular options hash, pretty
|
13
|
+
much what you'd expect:
|
14
|
+
|
15
|
+
FastMailer::SMTP.new({
|
16
|
+
:host => 'mail.authsmtp.com',
|
17
|
+
:port => 2525,
|
18
|
+
:authentication => :cram_md5,
|
19
|
+
:domain => 'mydomain.com',
|
20
|
+
:username => 'username',
|
21
|
+
:password => 'password'
|
22
|
+
})
|
23
|
+
|
24
|
+
Something I've found very convenient, however, is to let the gem read the
|
25
|
+
default configuration from your home directory. For instance, let's assume
|
26
|
+
we put:
|
27
|
+
|
28
|
+
{
|
29
|
+
:host => 'mail.authsmtp.com',
|
30
|
+
:port => 2525,
|
31
|
+
:authentication => :cram_md5,
|
32
|
+
:domain => 'mydomain.com',
|
33
|
+
:username => 'username',
|
34
|
+
:password => 'password'
|
35
|
+
}
|
36
|
+
|
37
|
+
into **~/.config/smtp.rb**, you could then achieve the same result by simply
|
38
|
+
not providing any options at all. This makes it really easy to write small
|
39
|
+
scripts that send email without having to worry about configuration.
|
40
|
+
|
41
|
+
|
42
|
+
Sending mail
|
43
|
+
------------
|
44
|
+
|
45
|
+
When sending a lot of mail, every second counts - especially if you don't
|
46
|
+
have your SMTP server close by. To reduce the time it takes to send a lot of
|
47
|
+
email, we can send multiple emails using a single connection.
|
48
|
+
|
49
|
+
mail = build_mail_somehow # mail is a Mail instance (as from the mail gem)
|
50
|
+
|
51
|
+
FastMailer::SMTP.new.open do |smtp|
|
52
|
+
smtp.deliver mail
|
53
|
+
mail.to = 'another@recipient.com'
|
54
|
+
smtp.deliver mail
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
Coming features
|
59
|
+
---------------
|
60
|
+
|
61
|
+
* Parallelisation: given a list of mails, or a mail generator, we can spin a number
|
62
|
+
of threads up and let them send mail simultaneously. As most of the time is spent
|
63
|
+
waiting for the network, this speeds the process up significantly.
|
64
|
+
|
65
|
+
* Logging: When sending large batches, sometimes things go wrong. If we keep track
|
66
|
+
of which mails have been sent and which have failed, we reduce the risk of finding
|
67
|
+
ourselves wondering which of our users have received the message and which we need
|
68
|
+
to send to.
|
69
|
+
|
70
|
+
* Blacklisting: block addresses so they cannot be sent to.
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/fast-mailer.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module FastMailer
|
4
|
+
class Configuration
|
5
|
+
|
6
|
+
TEST_CONFIGURATION = {
|
7
|
+
:something => :useful
|
8
|
+
}
|
9
|
+
|
10
|
+
def self.enable_testing!
|
11
|
+
def self.smtp_configuration(options = nil)
|
12
|
+
TEST_CONFIGURATION
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.disable_testing!
|
17
|
+
def self.smtp_configuration(options = nil)
|
18
|
+
if options.is_a? Hash
|
19
|
+
options
|
20
|
+
else
|
21
|
+
eval(File.read(options || File.expand_path("~/.config/smtp.rb")))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
disable_testing!
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
|
2
|
+
module FastMailer
|
3
|
+
|
4
|
+
# Sends messages using an SMTP server. The SMTP options is passed through
|
5
|
+
# FastMailer::Configuration, so you can set your system up to automatically
|
6
|
+
# use your SMTP configuration. Also, FastMailer::SMTP supports that you send
|
7
|
+
# multiple emails over a single connection, allowing for faster delivery
|
8
|
+
# of multiple emails.
|
9
|
+
#
|
10
|
+
# Note that if an error is raised by Net::SMTP, the connection may not be
|
11
|
+
# reliable. As a best practice, always discard the current connection and
|
12
|
+
# reopen a new one in case you encounter an error. Do not attempt to continue
|
13
|
+
# sending mail on the same connection.
|
14
|
+
class SMTP
|
15
|
+
|
16
|
+
attr_accessor :configuration
|
17
|
+
|
18
|
+
def initialize(options = nil)
|
19
|
+
@configuration = FastMailer::Configuration.smtp_configuration options
|
20
|
+
end
|
21
|
+
|
22
|
+
def open
|
23
|
+
@opened = true
|
24
|
+
|
25
|
+
configuration[:host] ||= configuration[:address]
|
26
|
+
configuration[:username] ||= configuration[:user_name]
|
27
|
+
|
28
|
+
@smtp = Net::SMTP.new(configuration[:host], configuration[:port])
|
29
|
+
if configuration[:enable_starttls_auto]
|
30
|
+
smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto)
|
31
|
+
end
|
32
|
+
|
33
|
+
@smtp.start(configuration[:domain], configuration[:username], configuration[:password], configuration[:authentication]) do |smtp|
|
34
|
+
yield self
|
35
|
+
end
|
36
|
+
ensure
|
37
|
+
@smtp = nil
|
38
|
+
@opened = false
|
39
|
+
end
|
40
|
+
|
41
|
+
def deliver(mail)
|
42
|
+
if @opened
|
43
|
+
perform_delivery(mail)
|
44
|
+
else
|
45
|
+
open do
|
46
|
+
deliver mail
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
def perform_delivery(mail)
|
53
|
+
|
54
|
+
# Set the envelope from to be either the return-path, the sender or the first from address
|
55
|
+
envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
|
56
|
+
if envelope_from.blank?
|
57
|
+
raise ArgumentError.new('A sender (Return-Path, Sender or From) required to send a message')
|
58
|
+
end
|
59
|
+
|
60
|
+
destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
|
61
|
+
if destinations.blank?
|
62
|
+
raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message')
|
63
|
+
end
|
64
|
+
|
65
|
+
message ||= mail.encoded if mail.respond_to?(:encoded)
|
66
|
+
if message.blank?
|
67
|
+
raise ArgumentError.new('A encoded content is required to send a message')
|
68
|
+
end
|
69
|
+
|
70
|
+
begin
|
71
|
+
@smtp.sendmail(message, envelope_from, destinations)
|
72
|
+
rescue e
|
73
|
+
@smtp = nil
|
74
|
+
raise e
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fast-mailer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Erik Hansson
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-09-29 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: mail
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 2
|
31
|
+
- 5
|
32
|
+
version: 2.2.5
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: i18n
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: bundler
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 1
|
58
|
+
- 0
|
59
|
+
- 0
|
60
|
+
version: 1.0.0
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rspec
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ~>
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 2
|
73
|
+
- 0
|
74
|
+
- 0
|
75
|
+
- beta
|
76
|
+
- 22
|
77
|
+
version: 2.0.0.beta.22
|
78
|
+
type: :development
|
79
|
+
version_requirements: *id004
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: rake
|
82
|
+
prerelease: false
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ~>
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
- 8
|
91
|
+
- 7
|
92
|
+
version: 0.8.7
|
93
|
+
type: :development
|
94
|
+
version_requirements: *id005
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: autotest
|
97
|
+
prerelease: false
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
type: :development
|
107
|
+
version_requirements: *id006
|
108
|
+
description: Helps with sending emails fast, and with convenient configuration.
|
109
|
+
email:
|
110
|
+
- erik@bits2life.com
|
111
|
+
executables: []
|
112
|
+
|
113
|
+
extensions: []
|
114
|
+
|
115
|
+
extra_rdoc_files: []
|
116
|
+
|
117
|
+
files:
|
118
|
+
- lib/fast-mailer/configuration.rb
|
119
|
+
- lib/fast-mailer/smtp.rb
|
120
|
+
- lib/fast-mailer.rb
|
121
|
+
- LICENCE
|
122
|
+
- VERSION
|
123
|
+
- README.md
|
124
|
+
has_rdoc: true
|
125
|
+
homepage: http://github.com/erikhansson/fast-mailer
|
126
|
+
licenses: []
|
127
|
+
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
none: false
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
segments:
|
139
|
+
- 0
|
140
|
+
version: "0"
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
segments:
|
147
|
+
- 1
|
148
|
+
- 3
|
149
|
+
- 6
|
150
|
+
version: 1.3.6
|
151
|
+
requirements: []
|
152
|
+
|
153
|
+
rubyforge_project: nowarning
|
154
|
+
rubygems_version: 1.3.7
|
155
|
+
signing_key:
|
156
|
+
specification_version: 3
|
157
|
+
summary: Help with sending emails.
|
158
|
+
test_files: []
|
159
|
+
|