huginn_mailgun_agent 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +7 -0
- data/lib/huginn_mailgun_agent.rb +4 -0
- data/lib/huginn_mailgun_agent/mailgun_agent.rb +152 -0
- data/spec/mailgun_agent_spec.rb +18 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c15b29390a244efd8ec88a386a01d3e67a4870eefd95ee7a0734c1a009f17d5b
|
4
|
+
data.tar.gz: 407f6a6154369597b1f05461d7efe8bce098754c925705560f2213e4aefc6587
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 381cf58105d792338d0cb6be5fd7d72b13b69d72488299e467977a1c3f7021ccfa90d5520e631b6afadf68950f2f44755c91c2ae463715adb4001acae9387461
|
7
|
+
data.tar.gz: 5cc3a88af54597b41276580b57fa9b8bde758a8f3f6a78142f155413a6d366dba7b680f88927954cb401516a37554fb66787cf2b621e9b6faae0201ca0da2107
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2018 Justin Hammond
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'mailgun-ruby'
|
2
|
+
|
3
|
+
module Agents
|
4
|
+
|
5
|
+
class Mysql2AgentConnection < ActiveRecord::Base
|
6
|
+
def self.abstract_class?
|
7
|
+
true # So it gets its own connection
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class MailgunAgent < Agent
|
12
|
+
|
13
|
+
include FormConfigurable
|
14
|
+
|
15
|
+
can_dry_run!
|
16
|
+
no_bulk_receive!
|
17
|
+
default_schedule "never"
|
18
|
+
|
19
|
+
description <<-MD
|
20
|
+
Sends bulk transactional emails via MailGun. Uses a Database connection to retrieve the recipients
|
21
|
+
|
22
|
+
To send emails to users, first create a Database and table to hold your recipients.
|
23
|
+
|
24
|
+
`connection_url` is the database connection you want to use
|
25
|
+
|
26
|
+
`sql` should be the Select Query to select your users. It must return a `name` and `email` field. eg: `select name, email from users`
|
27
|
+
|
28
|
+
`mailgun_apikey` is your MailGun Private Key
|
29
|
+
|
30
|
+
`mailgun_domain` is the domain you wish to use with Mailgun
|
31
|
+
|
32
|
+
`mailgun_tracking` will enable both Open and Click Tracking in your emails (if enabled on your Mailgun Domain Account)
|
33
|
+
|
34
|
+
`testing_mode` will submit the messages to Mailgun, but not actually send the emails to the recipients
|
35
|
+
|
36
|
+
`from_address` is the address you want to the emails to come from (can be overridden in a event payload)
|
37
|
+
|
38
|
+
If `merge_event` is true, then the Mailgun Message ID is merged with the original payload
|
39
|
+
|
40
|
+
Payload Fields:
|
41
|
+
|
42
|
+
* `from_address` allows you to override the default from message. Should be in this format: `name <email@address.com>`
|
43
|
+
|
44
|
+
* `subject` is the email Subject
|
45
|
+
|
46
|
+
* `message` is the Email Body (can be HTML if you want to enable tracking)
|
47
|
+
|
48
|
+
* `tags` is either a single Tag or a array or Tags that you can use for Analytics on the MailGun Site
|
49
|
+
MD
|
50
|
+
|
51
|
+
def default_options
|
52
|
+
{
|
53
|
+
'connection_url' => 'mysql2://user:pass@localhost/database',
|
54
|
+
'sql' => 'select email, name from emails order by id desc',
|
55
|
+
'merge_event' => 'false',
|
56
|
+
'mailgun_apikey' => '',
|
57
|
+
'from_address' => 'test@example.com',
|
58
|
+
'testing_mode' => 'false',
|
59
|
+
'mailgun_domain' => '',
|
60
|
+
'mailgun_tracking' => 'false',
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
form_configurable :mailgun_apikey, type: :text
|
66
|
+
form_configurable :mailgun_domain, type: :text
|
67
|
+
form_configurable :from_address, type: :text
|
68
|
+
form_configurable :connection_url
|
69
|
+
form_configurable :sql, type: :text, ace: {:mode =>'sql', :theme => ''}
|
70
|
+
form_configurable :merge_event, type: :boolean
|
71
|
+
form_configurable :mailgun_tracking, type: :boolean
|
72
|
+
form_configurable :testing_mode, type: :boolean
|
73
|
+
|
74
|
+
def validate_options
|
75
|
+
|
76
|
+
if options['merge_event'].present? && !%[true false].include?(options['merge_event'].to_s)
|
77
|
+
errors.add(:base, "Oh no!!! if provided, merge_event must be 'true' or 'false'")
|
78
|
+
end
|
79
|
+
errors.add(:base, "Mailgun API Key Missing") unless options['mailgun_apikey'].present?
|
80
|
+
errors.add(:base, "Missing From Address") unless options['from_address'].present?
|
81
|
+
errors.add(:base, "Missing Mailgun Domain") unless options['mailgun_domain'].present?
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
def working?
|
86
|
+
!recent_error_logs?
|
87
|
+
end
|
88
|
+
|
89
|
+
def check
|
90
|
+
handle(interpolated)
|
91
|
+
end
|
92
|
+
|
93
|
+
def receive(incoming_events)
|
94
|
+
incoming_events.each do |event|
|
95
|
+
handle(interpolated(event), event.payload)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def handle(opts, event = Event.new)
|
102
|
+
t1 = Time.now
|
103
|
+
connection_url = opts["connection_url"]
|
104
|
+
sql = opts["sql"]
|
105
|
+
|
106
|
+
begin
|
107
|
+
conn = Mysql2AgentConnection.establish_connection(connection_url).connection
|
108
|
+
mg_client = Mailgun::Client.new(opts['mailgun_apikey'])
|
109
|
+
bm_obj = Mailgun::BatchMessage.new(mg_client, opts['mailgun_domain'])
|
110
|
+
bm_obj.test_mode(opts['testing_mode'])
|
111
|
+
bm_obj.track_opens(opts['mailgun_tracking'])
|
112
|
+
bm_obj.track_clicks(opts['mailgun_tracking'])
|
113
|
+
bm_obj.subject(event['subject'])
|
114
|
+
if event['msg-tag'].respond_to?('each')
|
115
|
+
event['msg-tag'].each do | tag |
|
116
|
+
bm_obj.add_tag(tag)
|
117
|
+
end
|
118
|
+
else
|
119
|
+
bm_obj.add_tag(event['msg-tag'])
|
120
|
+
end
|
121
|
+
if event['from_address'].present?
|
122
|
+
bm_obj.from(event['from_address'])
|
123
|
+
else
|
124
|
+
bm_obj.from(opts['from_address'])
|
125
|
+
end
|
126
|
+
bm_obj.body_html(event['message'])
|
127
|
+
results = conn.exec_query(sql)
|
128
|
+
results.each do |row|
|
129
|
+
toaddr = row['name'] + " <" + row['email'] + ">"
|
130
|
+
log("Sending to #{toaddr}")
|
131
|
+
bm_obj.add_recipient(:to, toaddr)
|
132
|
+
end
|
133
|
+
if results.present?
|
134
|
+
conn.close
|
135
|
+
end
|
136
|
+
message_ids = bm_obj.finalize
|
137
|
+
if (opts['merge_event'] == 'true')
|
138
|
+
event['message_ids'] = message_ids
|
139
|
+
create_event payload: event
|
140
|
+
else
|
141
|
+
create_event payload: message_ids
|
142
|
+
end
|
143
|
+
log("Time: #{(Time.now - t1).round(2)}s, Sent: #{results.length if results.present?}, #{pp message_ids}")
|
144
|
+
|
145
|
+
rescue => error
|
146
|
+
error "Error connection: #{error.inspect} - #{error.backtrace}"
|
147
|
+
return
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
require 'huginn_agent/spec_helper'
|
3
|
+
|
4
|
+
describe Agents::MailgunAgent do
|
5
|
+
before(:each) do
|
6
|
+
@valid_options = Agents::MailgunAgent.new.default_options
|
7
|
+
@checker = Agents::MailgunAgent.new(:name => "MailgunAgent", :options => @valid_options)
|
8
|
+
@checker.user = users(:bob)
|
9
|
+
@checker.save!
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#working?' do
|
13
|
+
it 'checks if events have error' do
|
14
|
+
@checker.error "oh no!"
|
15
|
+
expect(@checker.reload).not_to be_working # There is a recent error
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: huginn_mailgun_agent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin Hammond
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: huginn_agent
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mailgun-ruby
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.1.6
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.1.6
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- justin@devthendo.io
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- LICENSE.txt
|
77
|
+
- lib/huginn_mailgun_agent.rb
|
78
|
+
- lib/huginn_mailgun_agent/mailgun_agent.rb
|
79
|
+
- spec/mailgun_agent_spec.rb
|
80
|
+
homepage: https://github.com/DevThenDo/huginn_mailgun_agent
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.7.6
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Sends bulk transactional emails via MailGun. Uses a Database connection to
|
104
|
+
retrieve the recipients
|
105
|
+
test_files:
|
106
|
+
- spec/mailgun_agent_spec.rb
|