mailhandler 1.0.11 → 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mailhandler.rb +14 -6
- data/lib/mailhandler/errors.rb +1 -0
- data/lib/mailhandler/receiver.rb +7 -3
- data/lib/mailhandler/receiving/base.rb +5 -5
- data/lib/mailhandler/receiving/imap.rb +0 -9
- data/lib/mailhandler/receiving/notification/email.rb +1 -1
- data/lib/mailhandler/receiving/notification/email/states.rb +1 -1
- data/lib/mailhandler/sender.rb +3 -1
- data/lib/mailhandler/sending/api_batch.rb +7 -1
- data/lib/mailhandler/sending/base.rb +7 -1
- data/lib/mailhandler/sending/smtp.rb +1 -14
- data/lib/mailhandler/version.rb +1 -1
- data/readme.md +27 -24
- data/spec/unit/mailhandler/receiving/base_spec.rb +1 -1
- data/spec/unit/mailhandler_spec.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78b7d4ffbbc6c366ff66a3a05676942692fa781e
|
4
|
+
data.tar.gz: ebf72708529af128d0d5fd51b3c36a7d77fd5057
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5abcd88963015fca9cde692ab4f722b0a5f3c8f62a3606ab944120bb00c3632aaed28b4340098a7272414476c01d597865a81dbdc6a604b48350c95736a5c000
|
7
|
+
data.tar.gz: e438a633e4318d70f23dc8ea075b4c690d4eb8f04b72f3f21bb571f0826ab1c9ad32050cdafa9a6fa4fe22f8f2b5d9b2cd3031a35c8671283a7afbf678e5bd27
|
data/lib/mailhandler.rb
CHANGED
@@ -5,14 +5,14 @@ require_relative 'mailhandler/receiving/notification/email'
|
|
5
5
|
require_relative 'mailhandler/receiving/notification/console'
|
6
6
|
require_relative 'mailhandler/errors'
|
7
7
|
|
8
|
-
# Main MailHandler class,
|
9
|
-
# Sender objects for sending emails, receiver objects for receiving emails
|
10
|
-
|
8
|
+
# Main MailHandler class, for creating sender and receiver objects.
|
9
|
+
# Sender objects for sending emails, receiver objects for searching and receiving emails.
|
11
10
|
module MailHandler
|
12
11
|
|
13
12
|
extend self
|
14
13
|
|
15
14
|
# sending accessor
|
15
|
+
# @return [Object] - sender for sending emails
|
16
16
|
def sender(type = :postmark_api)
|
17
17
|
|
18
18
|
handler = Handler.new
|
@@ -24,6 +24,7 @@ module MailHandler
|
|
24
24
|
end
|
25
25
|
|
26
26
|
# receiving accessor
|
27
|
+
# @return [Object] - receiver for searching emails
|
27
28
|
def receiver(type = :folder, notifications = [])
|
28
29
|
|
29
30
|
handler = Handler.new
|
@@ -34,7 +35,10 @@ module MailHandler
|
|
34
35
|
|
35
36
|
end
|
36
37
|
|
37
|
-
#
|
38
|
+
# Holder for receiving and sending handlers
|
39
|
+
#
|
40
|
+
# @param [Receiving::Class] receiver
|
41
|
+
# @param [Sending::Class] sender
|
38
42
|
def handler(receiver, sender)
|
39
43
|
|
40
44
|
handler = Handler.new
|
@@ -76,7 +80,11 @@ module MailHandler
|
|
76
80
|
|
77
81
|
private
|
78
82
|
|
79
|
-
#
|
83
|
+
# Add notifications, in case email receiving is delayed.
|
84
|
+
# When email is delayed, email notification can be sent or console status update.
|
85
|
+
#
|
86
|
+
# @param [Receiving::Object] receiver
|
87
|
+
# @param [Array<Receiving::Notification::Class>] notifications
|
80
88
|
def add_receiving_notifications(receiver, notifications)
|
81
89
|
|
82
90
|
if (notifications - NOTIFICATION_TYPES.keys).empty?
|
@@ -89,7 +97,7 @@ module MailHandler
|
|
89
97
|
|
90
98
|
def verify_type(type, types)
|
91
99
|
|
92
|
-
raise MailHandler::TypeError, "Unknown type - #{type}, possible options: #{types.keys}" unless types.keys.include? type
|
100
|
+
raise MailHandler::TypeError, "Unknown type - #{type}, possible options: #{types.keys}." unless types.keys.include? type
|
93
101
|
|
94
102
|
end
|
95
103
|
|
data/lib/mailhandler/errors.rb
CHANGED
data/lib/mailhandler/receiver.rb
CHANGED
@@ -10,16 +10,19 @@ module MailHandler
|
|
10
10
|
|
11
11
|
attr_accessor :checker,
|
12
12
|
:search,
|
13
|
-
:max_search_duration
|
13
|
+
:max_search_duration,
|
14
|
+
:search_frequency
|
14
15
|
|
15
16
|
module DEFAULTS
|
16
17
|
|
17
18
|
MAX_SEARCH_DURATION = 240 # maximum time for search to last in [seconds]
|
18
|
-
SEARCH_FREQUENCY = 1 # how frequently to check for email in inbox
|
19
|
+
SEARCH_FREQUENCY = 1 # how frequently to check for email in inbox [seconds]
|
19
20
|
|
20
21
|
end
|
21
22
|
|
22
23
|
# @param [Hash] - search options
|
24
|
+
# @see MailHandler::Receiving::Checker::AVAILABLE_SEARCH_OPTIONS for available options
|
25
|
+
#
|
23
26
|
# @param [Time] - search started at Time
|
24
27
|
# @param [Time] - search finished at Time
|
25
28
|
# @param [int] - how long search lasted
|
@@ -33,6 +36,7 @@ module MailHandler
|
|
33
36
|
|
34
37
|
@checker = checker
|
35
38
|
@max_search_duration = DEFAULTS::MAX_SEARCH_DURATION
|
39
|
+
@search_frequency = DEFAULTS::SEARCH_FREQUENCY
|
36
40
|
|
37
41
|
end
|
38
42
|
|
@@ -47,7 +51,7 @@ module MailHandler
|
|
47
51
|
notify_observers(search)
|
48
52
|
|
49
53
|
break if received
|
50
|
-
sleep
|
54
|
+
sleep search_frequency
|
51
55
|
|
52
56
|
end
|
53
57
|
|
@@ -33,7 +33,7 @@ module MailHandler
|
|
33
33
|
|
34
34
|
def find(options)
|
35
35
|
|
36
|
-
raise MailHandler::
|
36
|
+
raise MailHandler::InterfaceError, 'Find interface not implemented.'
|
37
37
|
|
38
38
|
end
|
39
39
|
|
@@ -66,20 +66,20 @@ module MailHandler
|
|
66
66
|
|
67
67
|
if options[:by_date]
|
68
68
|
|
69
|
-
raise MailHandler::Error, "Incorrect option options[:by_date]=#{options[:date]}" unless options[:by_date].is_a? Time
|
69
|
+
raise MailHandler::Error, "Incorrect option options[:by_date]=#{options[:date]}." unless options[:by_date].is_a? Time
|
70
70
|
|
71
71
|
end
|
72
72
|
|
73
73
|
unless options[:count].nil?
|
74
74
|
|
75
75
|
count = options[:count]
|
76
|
-
raise MailHandler::Error, "Incorrect option options[:count]=#{options[:count]}" if count < 0 or count > 2000
|
76
|
+
raise MailHandler::Error, "Incorrect option options[:count]=#{options[:count]}." if count < 0 or count > 2000
|
77
77
|
|
78
78
|
end
|
79
79
|
|
80
80
|
if options[:archive]
|
81
81
|
|
82
|
-
raise MailHandler::Error, "Incorrect option options[:archive]=#{options[:archive]}" unless options[:archive] == true or options[:archive] == false
|
82
|
+
raise MailHandler::Error, "Incorrect option options[:archive]=#{options[:archive]}." unless options[:archive] == true or options[:archive] == false
|
83
83
|
|
84
84
|
end
|
85
85
|
|
@@ -88,7 +88,7 @@ module MailHandler
|
|
88
88
|
def validate_used_options(options)
|
89
89
|
|
90
90
|
unless (options.keys - AVAILABLE_SEARCH_OPTIONS).empty?
|
91
|
-
raise MailHandler::Error, "#{(options.keys - AVAILABLE_SEARCH_OPTIONS)} - Incorrect search option values, options are #{AVAILABLE_SEARCH_OPTIONS}"
|
91
|
+
raise MailHandler::Error, "#{(options.keys - AVAILABLE_SEARCH_OPTIONS)} - Incorrect search option values, options are #{AVAILABLE_SEARCH_OPTIONS}."
|
92
92
|
end
|
93
93
|
|
94
94
|
end
|
@@ -130,15 +130,6 @@ module MailHandler
|
|
130
130
|
|
131
131
|
(keys.empty?)? nil : keys
|
132
132
|
|
133
|
-
|
134
|
-
end
|
135
|
-
|
136
|
-
def validate_options(options)
|
137
|
-
|
138
|
-
unless (options.keys - AVAILABLE_SEARCH_OPTIONS).empty?
|
139
|
-
raise MailHandler::Error, "#{(options.keys - AVAILABLE_SEARCH_OPTIONS)} - Not supported search option values for imap, options are #{AVAILABLE_SEARCH_OPTIONS}"
|
140
|
-
end
|
141
|
-
|
142
133
|
end
|
143
134
|
|
144
135
|
end
|
@@ -59,7 +59,7 @@ module MailHandler
|
|
59
59
|
|
60
60
|
def verify_email_type(type)
|
61
61
|
|
62
|
-
raise MailHandler::TypeError, "Incorrect type: #{type}, allowed types: #{EMAIL_TYPES}" unless EMAIL_TYPES.include? type
|
62
|
+
raise MailHandler::TypeError, "Incorrect type: #{type}, allowed types: #{EMAIL_TYPES}." unless EMAIL_TYPES.include? type
|
63
63
|
|
64
64
|
end
|
65
65
|
|
data/lib/mailhandler/sender.rb
CHANGED
@@ -2,6 +2,7 @@ require_relative 'sending/smtp'
|
|
2
2
|
require_relative 'sending/api'
|
3
3
|
require_relative 'sending/api_batch'
|
4
4
|
|
5
|
+
# Class for sending email, and storing details about the sending.
|
5
6
|
module MailHandler
|
6
7
|
|
7
8
|
class Sender
|
@@ -11,11 +12,12 @@ module MailHandler
|
|
11
12
|
|
12
13
|
# @param [Time] - sending started at Time
|
13
14
|
# @param [Time] - sending finished at Time
|
14
|
-
# @param [int] - how long sending lasted
|
15
|
+
# @param [int] - how long sending lasted, seconds
|
15
16
|
# @param [Object] - sending response message
|
16
17
|
# @param [Mail] - email/emails sent
|
17
18
|
Sending = Struct.new( :started_at, :finished_at, :duration, :response, :email)
|
18
19
|
|
20
|
+
# @param [Sending::Oblect] dispatcher - sender type used for sending email
|
19
21
|
def initialize(dispatcher)
|
20
22
|
|
21
23
|
@dispatcher = dispatcher
|
@@ -7,6 +7,12 @@ module MailHandler
|
|
7
7
|
|
8
8
|
class PostmarkBatchAPISender < PostmarkAPISender
|
9
9
|
|
10
|
+
def initialize(api_token = nil)
|
11
|
+
|
12
|
+
super(api_token)
|
13
|
+
|
14
|
+
end
|
15
|
+
|
10
16
|
def send(emails)
|
11
17
|
|
12
18
|
verify_email(emails)
|
@@ -19,7 +25,7 @@ module MailHandler
|
|
19
25
|
|
20
26
|
def verify_email(emails)
|
21
27
|
|
22
|
-
raise MailHandler::TypeError, "Invalid type error, only Array of Mail::Message object types for sending allowed" unless emails.is_a?(Array) && emails.all? { |e| e.is_a?
|
28
|
+
raise MailHandler::TypeError, "Invalid type error, only Array of Mail::Message object types for sending allowed" unless emails.is_a?(Array) && emails.all? { |e| e.is_a? allowed_email_type }
|
23
29
|
|
24
30
|
end
|
25
31
|
|
@@ -8,11 +8,17 @@ module MailHandler
|
|
8
8
|
|
9
9
|
attr_reader :type
|
10
10
|
|
11
|
+
def send(email)
|
12
|
+
|
13
|
+
raise MailHandler::InterfaceError, 'Send interface not implemented.'
|
14
|
+
|
15
|
+
end
|
16
|
+
|
11
17
|
protected
|
12
18
|
|
13
19
|
def verify_email(email)
|
14
20
|
|
15
|
-
raise MailHandler::TypeError, "Invalid type error, only #{allowed_email_type} object type for sending allowed" unless email.is_a? allowed_email_type
|
21
|
+
raise MailHandler::TypeError, "Invalid type error, only #{allowed_email_type} object type for sending allowed." unless email.is_a? allowed_email_type
|
16
22
|
|
17
23
|
end
|
18
24
|
|
@@ -29,20 +29,7 @@ module MailHandler
|
|
29
29
|
|
30
30
|
verify_email(email)
|
31
31
|
email = configure_sending(email)
|
32
|
-
|
33
|
-
response = nil
|
34
|
-
|
35
|
-
begin
|
36
|
-
|
37
|
-
response = email.deliver
|
38
|
-
|
39
|
-
rescue Exception => e
|
40
|
-
|
41
|
-
response = e.to_s
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
response
|
32
|
+
email.deliver
|
46
33
|
|
47
34
|
end
|
48
35
|
|
data/lib/mailhandler/version.rb
CHANGED
data/readme.md
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/wildbit/mailhandler.svg?branch=master)](https://travis-ci.org/wildbit/mailhandler)
|
4
4
|
|
5
|
-
MailHandler is a simple wrapper
|
6
|
-
|
5
|
+
MailHandler is a simple wrapper on top of [mail gem](https://github.com/mikel/mail) and [postmark gem](https://github.com/wildbit/postmark-gem) libraries which allows you to send and retrieve emails and get details on how long these operations took.
|
6
|
+
Main purpose of the gem is easier email sending/delivery testing with notifications if sending or retrieving email is taking too long.
|
7
7
|
|
8
|
-
The library supports sending by SMTP
|
8
|
+
The library supports sending email by SMTP and Postmark API and checking email delivery by IMAP protocol, or by folder, if you have a local mailbox.
|
9
9
|
|
10
10
|
# Install the gem
|
11
11
|
|
@@ -21,12 +21,14 @@ Without Bundler:
|
|
21
21
|
gem install mailhandler
|
22
22
|
```
|
23
23
|
|
24
|
-
# Email
|
24
|
+
# Email retrieval
|
25
25
|
|
26
|
-
## Configure local mailbox
|
26
|
+
## Configure local folder as your mailbox check for emails
|
27
27
|
|
28
|
-
|
29
|
-
|
28
|
+
Searching emails locally is an option that can be used when you have emails stored in certain local path on your test machine.
|
29
|
+
In order to search for email, all you need to do is setup inbox folder and archive folder.
|
30
|
+
|
31
|
+
Folders can be the same if you don't plan to archive found emails. Retrieving emails from a folder would look like following:
|
30
32
|
|
31
33
|
``` ruby
|
32
34
|
email_receiver = MailHandler.receiver(:folder) do |checker|
|
@@ -37,7 +39,7 @@ end
|
|
37
39
|
|
38
40
|
## Configure imap mailbox email to check
|
39
41
|
|
40
|
-
If you plan to
|
42
|
+
If you plan to search for emails in your remote inbox which support IMAP, you can use Mailhandler with provided IMAP settings:
|
41
43
|
|
42
44
|
``` ruby
|
43
45
|
email_receiver = MailHandler.receiver(:imap) do |checker|
|
@@ -48,11 +50,12 @@ email_receiver = MailHandler.receiver(:imap) do |checker|
|
|
48
50
|
checker.use_ssl = true
|
49
51
|
end
|
50
52
|
```
|
51
|
-
|
53
|
+
|
54
|
+
Email receiving handler will be referenced in examples below as `email_receiver`.
|
52
55
|
|
53
56
|
## Searching for email
|
54
57
|
|
55
|
-
Once you have setup mailbox
|
58
|
+
Once you have setup mailbox searching type, you can search for email like this:
|
56
59
|
|
57
60
|
``` ruby
|
58
61
|
email_receiver.find_email(:by_subject => subject, :archive => true)
|
@@ -70,29 +73,32 @@ You can search local mailbox by following options:
|
|
70
73
|
* `:by_recipient` - by email recipient
|
71
74
|
* `:by_content` - search email content by keywords
|
72
75
|
|
73
|
-
Recipient to search by needs to by added in following form: `by_recipient => { :to => 'igor@example.com' }`.
|
76
|
+
Recipient to search by needs to by added in the following form: `by_recipient => { :to => 'igor@example.com' }`.
|
77
|
+
Library supports searching by :to, :cc recipients. At the moment, only searching by a single recipient email address is supported.
|
74
78
|
|
75
79
|
If you would like email to be archived after its read, use `:archive => true` option (recommended)
|
76
80
|
|
77
81
|
## Search results
|
78
82
|
|
79
|
-
Once email searching is finished, you can check search
|
83
|
+
Once email searching is finished, you can check search results by checking: `email_receiver.search` object, which has following information:
|
80
84
|
|
81
85
|
* `:options` - search options which were used (described above)
|
82
86
|
* `:started_at` - time when search was initiated
|
83
87
|
* `:finished_at` - time when search was stopped
|
84
88
|
* `:duration` - time how long the search took
|
85
89
|
* `:max_duration` - maximum amount of time search can take in seconds (default is 240)
|
86
|
-
* `:result - result of search - `true/false`
|
87
|
-
* `:email` - email found in search
|
88
|
-
* `:emails` - array of emails found in search
|
90
|
+
* `:result` - result of search - `true/false`
|
91
|
+
* `:email` - first email found in search
|
92
|
+
* `:emails` - array of all emails found in search
|
89
93
|
|
90
94
|
# Email search notifications
|
91
95
|
|
92
|
-
While searching for an email, there is a possibility to get notification if emails
|
93
|
-
You can get an notification in console, by email or both.
|
96
|
+
While searching for an email, there is a possibility to get notification if emails searching is taking too long.
|
97
|
+
You can get an notification in console, by email or both.
|
98
|
+
|
99
|
+
Console notification is a good option if you are testing email delivery and want to see console output on how is the progress of search going.
|
94
100
|
|
95
|
-
To add notifications to email searching all you need to do is:
|
101
|
+
To add console or email notifications, to your email searching all you need to do is:
|
96
102
|
|
97
103
|
``` ruby
|
98
104
|
email_receiver.add_observer(MailHandler::Receiving::Notification::Email.new(email_sender, contacts))
|
@@ -106,11 +112,7 @@ For email notifications, the parameters you need are:
|
|
106
112
|
|
107
113
|
# Email sending
|
108
114
|
|
109
|
-
There are three ways you can send email.
|
110
|
-
|
111
|
-
## Send by postmark
|
112
|
-
|
113
|
-
To send email you can use SMTP protocol or Postmark.
|
115
|
+
There are three ways you can send email, which we will describe below. To send email you can use SMTP protocol or Postmark API.
|
114
116
|
|
115
117
|
### Send by Postmark API
|
116
118
|
|
@@ -148,7 +150,8 @@ Once you have setup your email sender, all you need to do is to send an email:
|
|
148
150
|
email_sender.send_email(email)
|
149
151
|
```
|
150
152
|
|
151
|
-
|
153
|
+
Email you plan to send has to be an email created with Mail gem.
|
154
|
+
In order to send emails by Postmark batch, you will need to provide an Array of emails.
|
152
155
|
|
153
156
|
## Sending results
|
154
157
|
|
@@ -22,7 +22,7 @@ describe MailHandler::Receiving::Checker do
|
|
22
22
|
|
23
23
|
it '.find' do
|
24
24
|
|
25
|
-
expect { subject.find(:by_subject => 'test') }.to raise_error(MailHandler::
|
25
|
+
expect { subject.find(:by_subject => 'test') }.to raise_error(MailHandler::InterfaceError, 'Find interface not implemented.')
|
26
26
|
|
27
27
|
end
|
28
28
|
|
@@ -9,7 +9,7 @@ describe MailHandler::Handler do
|
|
9
9
|
it 'create - invalid type' do
|
10
10
|
|
11
11
|
expect { subject.init_receiver(:test) }.
|
12
|
-
to raise_error(MailHandler::TypeError, 'Unknown type - test, possible options: [:folder, :imap]')
|
12
|
+
to raise_error(MailHandler::TypeError, 'Unknown type - test, possible options: [:folder, :imap].')
|
13
13
|
|
14
14
|
end
|
15
15
|
|
@@ -32,7 +32,7 @@ describe MailHandler::Handler do
|
|
32
32
|
it 'create - invalid type' do
|
33
33
|
|
34
34
|
expect { subject.init_sender(:test) }.
|
35
|
-
to raise_error(MailHandler::TypeError, 'Unknown type - test, possible options: [:postmark_api, :postmark_batch_api, :smtp]')
|
35
|
+
to raise_error(MailHandler::TypeError, 'Unknown type - test, possible options: [:postmark_api, :postmark_batch_api, :smtp].')
|
36
36
|
|
37
37
|
end
|
38
38
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mailhandler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Balos
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mail
|