mailhandler 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/lib/mailhandler.rb +91 -0
- data/lib/mailhandler/receiver.rb +86 -0
- data/lib/mailhandler/receiving/checker.rb +60 -0
- data/lib/mailhandler/receiving/checker_folder.rb +126 -0
- data/lib/mailhandler/receiving/checker_imap.rb +90 -0
- data/lib/mailhandler/receiving/filter.rb +161 -0
- data/lib/mailhandler/receiving/notification/console.rb +44 -0
- data/lib/mailhandler/receiving/notification/email.rb +74 -0
- data/lib/mailhandler/receiving/notification/email/content.rb +43 -0
- data/lib/mailhandler/receiving/notification/email/states.rb +113 -0
- data/lib/mailhandler/receiving/observer.rb +37 -0
- data/lib/mailhandler/sender.rb +56 -0
- data/lib/mailhandler/sending/sender.rb +13 -0
- data/lib/mailhandler/sending/sender_api.rb +45 -0
- data/lib/mailhandler/sending/sender_api_batch.rb +20 -0
- data/lib/mailhandler/sending/sender_smtp.rb +76 -0
- data/lib/mailhandler/version.rb +5 -0
- data/mailhandler.gemspec +28 -0
- data/readme.md +151 -0
- metadata +98 -0
@@ -0,0 +1,56 @@
|
|
1
|
+
require_relative 'sending/sender_smtp'
|
2
|
+
require_relative 'sending/sender_api'
|
3
|
+
require_relative 'sending/sender_api_batch'
|
4
|
+
|
5
|
+
module MailHandler
|
6
|
+
|
7
|
+
class Sender
|
8
|
+
|
9
|
+
attr_accessor :dispatcher,
|
10
|
+
:sending
|
11
|
+
|
12
|
+
# @param [Time] - sending started at Time
|
13
|
+
# @param [Time] - sending finished at Time
|
14
|
+
# @param [int] - how long sending lasted
|
15
|
+
# @param [Object] - sending response message
|
16
|
+
# @param [Mail] - email/emails sent
|
17
|
+
Sending = Struct.new( :started_at, :finished_at, :duration, :response, :email)
|
18
|
+
|
19
|
+
def initialize(dispatcher)
|
20
|
+
|
21
|
+
@dispatcher = dispatcher
|
22
|
+
@sending = Sending.new
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def send_email(email)
|
27
|
+
|
28
|
+
init_sending_details(email)
|
29
|
+
response = dispatcher.send(email)
|
30
|
+
update_sending_details(response)
|
31
|
+
|
32
|
+
response
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def init_sending_details(email)
|
39
|
+
|
40
|
+
@sending = Sending.new
|
41
|
+
@sending.started_at = Time.now
|
42
|
+
@sending.email = email
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
def update_sending_details(response)
|
47
|
+
|
48
|
+
@sending.finished_at = Time.now
|
49
|
+
@sending.duration = @sending.finished_at - @sending.started_at
|
50
|
+
@sending.response = response
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'mail'
|
2
|
+
require 'postmark'
|
3
|
+
require_relative 'sender.rb'
|
4
|
+
|
5
|
+
module MailHandler
|
6
|
+
|
7
|
+
module Sending
|
8
|
+
|
9
|
+
class PostmarkAPISender < Sender
|
10
|
+
|
11
|
+
attr_accessor :host,
|
12
|
+
:api_token,
|
13
|
+
:use_ssl
|
14
|
+
|
15
|
+
def initialize(host = nil, api_token = nil, use_ssl = false)
|
16
|
+
|
17
|
+
@type = :postmark_api
|
18
|
+
@host = host
|
19
|
+
@api_token = api_token
|
20
|
+
@use_ssl = use_ssl
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def send(email)
|
25
|
+
|
26
|
+
client = setup_sending_client
|
27
|
+
client.deliver_message(email)
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def setup_sending_client
|
34
|
+
|
35
|
+
# clearing cache so valid host is accepted, and not the cached one
|
36
|
+
Postmark::HttpClient.instance_variable_set('@http', nil)
|
37
|
+
Postmark::ApiClient.new(api_token, http_open_timeout: 15, host: host, secure: @use_ssl)
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative 'sender_api.rb'
|
2
|
+
|
3
|
+
module MailHandler
|
4
|
+
|
5
|
+
module Sending
|
6
|
+
|
7
|
+
class PostmarkBatchAPISender < PostmarkAPISender
|
8
|
+
|
9
|
+
def send(emails)
|
10
|
+
|
11
|
+
client = setup_sending_client
|
12
|
+
client.deliver_messages(emails)
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'net/imap'
|
3
|
+
require_relative 'sender'
|
4
|
+
|
5
|
+
module MailHandler
|
6
|
+
|
7
|
+
module Sending
|
8
|
+
|
9
|
+
# class which describes methods to send and receive emails
|
10
|
+
class SMTPSender < Sender
|
11
|
+
|
12
|
+
# address - for smtp for example smtp.gmail.com
|
13
|
+
# port which would be used for sending
|
14
|
+
# domain for example for smtp.gmail.com it would be gmail.com
|
15
|
+
attr_accessor :address,
|
16
|
+
:port,
|
17
|
+
:domain,
|
18
|
+
:username,
|
19
|
+
:password,
|
20
|
+
:authentication,
|
21
|
+
:use_ssl
|
22
|
+
|
23
|
+
def initialize
|
24
|
+
|
25
|
+
@type = :smtp
|
26
|
+
@authentication = 'plain'
|
27
|
+
@use_ssl = false
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
def send(email)
|
32
|
+
|
33
|
+
email = configure_sending(email)
|
34
|
+
|
35
|
+
response = nil
|
36
|
+
|
37
|
+
begin
|
38
|
+
|
39
|
+
response = email.deliver
|
40
|
+
|
41
|
+
rescue Exception => e
|
42
|
+
|
43
|
+
response = e.to_s
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
response
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def configure_sending(email)
|
54
|
+
|
55
|
+
options = {
|
56
|
+
|
57
|
+
:address => address,
|
58
|
+
:port => port,
|
59
|
+
:domain => domain,
|
60
|
+
:user_name => username,
|
61
|
+
:password => password,
|
62
|
+
:authentication => @authentication,
|
63
|
+
:enable_starttls_auto => @use_ssl
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
email.delivery_method :smtp, options
|
68
|
+
email
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/mailhandler.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mailhandler/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
|
8
|
+
s.name = 'mailhandler'
|
9
|
+
s.version = MailHandler::VERSION
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.license = 'MIT'
|
12
|
+
|
13
|
+
s.authors = ["Igor Balos"]
|
14
|
+
s.email = "ibalosh@gmail.com"
|
15
|
+
|
16
|
+
s.summary = "Postmark email receiving and sending handler."
|
17
|
+
s.description = "Use this gem to send emails through SMTP and Postmark API. Check if email arrived by imap or folder check."
|
18
|
+
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split($/)
|
21
|
+
s.homepage = 'https://github.com/ibalosh'
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
s.required_rubygems_version = ">= 1.8.7"
|
24
|
+
|
25
|
+
s.add_dependency "mail"
|
26
|
+
s.add_dependency "postmark", '~> 1.7.0','>= 1.7.0'
|
27
|
+
|
28
|
+
end
|
data/readme.md
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# MailHandler Gem
|
2
|
+
|
3
|
+
This gem represents a simple library which allows you to send email and check email delivery.
|
4
|
+
The library supports sending SMTP, Postmark API or Postmark Batch API, checking email delivery by IMAP, or by folder,
|
5
|
+
if you have a local mailbox.
|
6
|
+
|
7
|
+
# Install the gem
|
8
|
+
|
9
|
+
With Bundler:
|
10
|
+
|
11
|
+
``` ruby
|
12
|
+
gem 'mailhandler'
|
13
|
+
```
|
14
|
+
|
15
|
+
Without Bundler:
|
16
|
+
|
17
|
+
``` bash
|
18
|
+
gem install mailhandler
|
19
|
+
```
|
20
|
+
|
21
|
+
# Email arrival testing
|
22
|
+
|
23
|
+
## Configure local mailbox email to check
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
email_receiver = MailHandler::Handler.receiver(:folder) do |checker|
|
27
|
+
checker.inbox_folder = folder_of_your_inbox
|
28
|
+
checker.archive_folder = folder_of_your_inbox_archive_folder
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
## Configure imap mailbox email to check
|
33
|
+
|
34
|
+
``` ruby
|
35
|
+
email_receiver = MailHandler::Handler.receiver(:imap) do |checker|
|
36
|
+
checker.imap_details(
|
37
|
+
address,
|
38
|
+
port,
|
39
|
+
username,
|
40
|
+
password,
|
41
|
+
use_ssl
|
42
|
+
)
|
43
|
+
end
|
44
|
+
```
|
45
|
+
Email receiving handler will be referenced below as `email_receiver`
|
46
|
+
|
47
|
+
## Searching for email
|
48
|
+
|
49
|
+
Once you have setup mailbox checking type, you can search for email like this:
|
50
|
+
|
51
|
+
``` ruby
|
52
|
+
email_receiver.find_email(:by_subject => email.subject, :archive => true)
|
53
|
+
```
|
54
|
+
|
55
|
+
You can search imap mailbox by following options:
|
56
|
+
|
57
|
+
* `:by_subject` - subject of the email
|
58
|
+
|
59
|
+
You can search local mailbox by following options:
|
60
|
+
|
61
|
+
* `:by_subject` - subject of the email
|
62
|
+
* `:by_recipient` - by email recipient
|
63
|
+
|
64
|
+
If you would like email to be archived after its read, use `:archive => true` option (recommended)
|
65
|
+
|
66
|
+
## Search results
|
67
|
+
|
68
|
+
Once email searching is finished, you can check search result by looking at: `email_receiver.search` object
|
69
|
+
|
70
|
+
* `:options` - search options which were used (described above)
|
71
|
+
* `:started_at` - time when search was initiated
|
72
|
+
* `:finished_at` - time when search was stopped
|
73
|
+
* `:duration` - time how long the search took
|
74
|
+
* `:max_duration` - maximum amount of time search can take in seconds (default is 240)
|
75
|
+
* `:result - result of search - `true/false`
|
76
|
+
* `:email` - email found in search
|
77
|
+
* `:emails` - array of emails found in search
|
78
|
+
|
79
|
+
# Email search notifications
|
80
|
+
|
81
|
+
While searching for an email, there is a possibility to get notification if emails sending is taking too long.
|
82
|
+
You can get an notification in console, by email or both.
|
83
|
+
|
84
|
+
To add notifications to email searching all you need to do is:
|
85
|
+
|
86
|
+
``` ruby
|
87
|
+
email_receiver.add_observer(MailHandler::Receiving::Notification::Email.new(email_sender, contacts))
|
88
|
+
email_receiver.add_observer(MailHandler::Receiving::Notification::Console.new)
|
89
|
+
```
|
90
|
+
|
91
|
+
the parameters you need are:
|
92
|
+
|
93
|
+
* `email_sender` - email sender you will use for sending an email (it should be one of senders described below)
|
94
|
+
* `contacts` - list of contacts to receive the notification (for example: `john@example.com, igor@example.com`
|
95
|
+
|
96
|
+
# Email sending
|
97
|
+
|
98
|
+
There are three ways you can send email.
|
99
|
+
|
100
|
+
## Send by postmark
|
101
|
+
|
102
|
+
To send email you can use SMTP protocol or Postmark.
|
103
|
+
|
104
|
+
### Send by Postmark API
|
105
|
+
|
106
|
+
To send email with Postmark, you need to choose type of sending, host, api token, and ssl options.
|
107
|
+
|
108
|
+
``` ruby
|
109
|
+
email_sender = MailHandler::Handler.sender(type) do |dispatcher|
|
110
|
+
dispatcher.host = host
|
111
|
+
dispatcher.api_token = api_token
|
112
|
+
dispatcher.use_ssl = use_ssl
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
* `:type` - type can be `:postmark_api` or `postmark_batch_api`
|
117
|
+
* `:host` - host is `api.postmarkapp.com`
|
118
|
+
* `:api_token` - api token of one of your Postmark sending servers
|
119
|
+
* `:use_ssl` - can be true/false
|
120
|
+
|
121
|
+
### Sending email by SMTP
|
122
|
+
|
123
|
+
To send email with SMTP you need to configure standard SMTP settings.
|
124
|
+
|
125
|
+
``` ruby
|
126
|
+
email_sender = MailHandler::Handler.sender(:smtp) do |dispatcher|
|
127
|
+
dispatcher.address = address
|
128
|
+
dispatcher.port = port
|
129
|
+
dispatcher.domain = domain
|
130
|
+
dispatcher.username = username
|
131
|
+
dispatcher.password = password
|
132
|
+
dispatcher.use_ssl = use_ssl
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
## Sending results
|
137
|
+
|
138
|
+
Once email sending is finished, you can check sending result by looking at: `email_receiver.sending` object
|
139
|
+
|
140
|
+
* `:started_at` - time when sending was initiated
|
141
|
+
* `:finished_at` - time when sending was finished
|
142
|
+
* `:duration` - time how long the sending took
|
143
|
+
* `:response` - response from sending
|
144
|
+
* `:email` - email sent
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mailhandler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Balos
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mail
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: postmark
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.7.0
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 1.7.0
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.7.0
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.7.0
|
47
|
+
description: Use this gem to send emails through SMTP and Postmark API. Check if email
|
48
|
+
arrived by imap or folder check.
|
49
|
+
email: ibalosh@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- ".gitignore"
|
55
|
+
- lib/mailhandler.rb
|
56
|
+
- lib/mailhandler/receiver.rb
|
57
|
+
- lib/mailhandler/receiving/checker.rb
|
58
|
+
- lib/mailhandler/receiving/checker_folder.rb
|
59
|
+
- lib/mailhandler/receiving/checker_imap.rb
|
60
|
+
- lib/mailhandler/receiving/filter.rb
|
61
|
+
- lib/mailhandler/receiving/notification/console.rb
|
62
|
+
- lib/mailhandler/receiving/notification/email.rb
|
63
|
+
- lib/mailhandler/receiving/notification/email/content.rb
|
64
|
+
- lib/mailhandler/receiving/notification/email/states.rb
|
65
|
+
- lib/mailhandler/receiving/observer.rb
|
66
|
+
- lib/mailhandler/sender.rb
|
67
|
+
- lib/mailhandler/sending/sender.rb
|
68
|
+
- lib/mailhandler/sending/sender_api.rb
|
69
|
+
- lib/mailhandler/sending/sender_api_batch.rb
|
70
|
+
- lib/mailhandler/sending/sender_smtp.rb
|
71
|
+
- lib/mailhandler/version.rb
|
72
|
+
- mailhandler.gemspec
|
73
|
+
- readme.md
|
74
|
+
homepage: https://github.com/ibalosh
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: 1.8.7
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.4.6
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Postmark email receiving and sending handler.
|
98
|
+
test_files: []
|