sparkpost_rails5 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/lib/sparkpost_rails/custom_payload.rb +17 -0
- data/lib/sparkpost_rails/mailer.rb +84 -0
- data/lib/sparkpost_rails/railtie.rb +10 -0
- data/lib/sparkpost_rails/version.rb +3 -0
- data/lib/sparkpost_rails.rb +4 -0
- data/sparkpost_rails5.gemspec +29 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: '096f85ad1ce7d3ac1416becabde60f53e41a92f8'
|
4
|
+
data.tar.gz: b60b66f21836678e80a7ac65880ff0432e881ebe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ca16f1e8e67e0b00ada0d5f862857011b100a14c2b4bffff27c65d066dbc351b4c16c81cd182dd6a60283c0a4a4512a75b81188fc55ca7f82fa6cdf6c1483fc0
|
7
|
+
data.tar.gz: e127708c6031b9040a65a4098e860bd748e898bb0e404f324575f39a980160d2f57d22ec8bc6742e70abb87c02bd81fadd53491df0d59e18fcc6256c6d757fee
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Sparkpost-Rails
|
2
|
+
|
3
|
+
Simple ActionMailer provider for sending mail through Sparkpost API.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'sparkpost_rails5'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install sparkpost_rails5
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Add this to corresponding environment configuration file (`config/environments/*.rb`):
|
24
|
+
```ruby
|
25
|
+
config.action_mailer.delivery_method = :sparkpost_rails
|
26
|
+
```
|
27
|
+
|
28
|
+
And make sure `ENV['SPARKPOST_API_KEY']` is set.
|
29
|
+
|
30
|
+
You can also provide custom options through `sparkpost_payload` method. The value will be merged with request payload:
|
31
|
+
```ruby
|
32
|
+
class UserMailer < ApplicationMailer
|
33
|
+
|
34
|
+
def confirmation_email(email)
|
35
|
+
sparkpost_payload(
|
36
|
+
options: {
|
37
|
+
open_tracking: true
|
38
|
+
},
|
39
|
+
campaign_id: "Your campaign ID"
|
40
|
+
description: "Confirmation email",
|
41
|
+
content: {
|
42
|
+
headers: {
|
43
|
+
"X-Custom-Header" => "custom header value"
|
44
|
+
}
|
45
|
+
}
|
46
|
+
)
|
47
|
+
mail(to: email, subject: "Confirm Your Account!!!")
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
```
|
52
|
+
See [https://developers.sparkpost.com/api/transmissions.html#transmissions-create](https://developers.sparkpost.com/api/transmissions.html#transmissions-create)
|
53
|
+
|
54
|
+
## Development
|
55
|
+
|
56
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/muhlisbc/sparkpost_rails5. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
61
|
+
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require "base64"
|
2
|
+
require "action_mailer"
|
3
|
+
require "simple_spark"
|
4
|
+
|
5
|
+
module SparkpostRails
|
6
|
+
|
7
|
+
# SparkpostRails::Mailer is an ActionMailer provider for sending mail through Sparkpost.
|
8
|
+
class Mailer
|
9
|
+
|
10
|
+
def initialize(config={})
|
11
|
+
@sp_client = SimpleSpark::Client.new(config)
|
12
|
+
end
|
13
|
+
|
14
|
+
def deliver!(mail)
|
15
|
+
properties = SparkpostRails.transform_mail(mail)
|
16
|
+
response = @sp_client.transmissions.create(properties)
|
17
|
+
mail.message_id = response["id"]
|
18
|
+
response
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module_function
|
23
|
+
|
24
|
+
# Transform Mail::Message object to Hash that will be send as request payload
|
25
|
+
#
|
26
|
+
# @param [Mail::Message] mail
|
27
|
+
#
|
28
|
+
# @return [Hash]
|
29
|
+
def transform_mail(mail)
|
30
|
+
content = {}
|
31
|
+
sp_object = {}
|
32
|
+
|
33
|
+
sp_object[:recipients] = mail[:to].address_list.addresses.map { |recipient| {address: fetch_email_and_name(recipient)} } if mail.to
|
34
|
+
sp_object[:return_path] = mail[:return_path].address_list.addresses[0].raw if mail.return_path
|
35
|
+
content[:from] = fetch_email_and_name(mail[:from].address_list.addresses[0]) if mail.from && mail.from[0]
|
36
|
+
content[:subject] = mail.subject if mail.subject
|
37
|
+
content[:reply_to] = mail[:reply_to].address_list.addresses[0].raw if mail.reply_to && mail.reply_to[0]
|
38
|
+
content[:attachments] = transform_attachments(mail.attachments) if !mail.attachments.empty?
|
39
|
+
|
40
|
+
["html", "text"].each do |part|
|
41
|
+
mail_part = mail.send("#{part}_part")
|
42
|
+
content[part] = mail_part.decoded if mail_part
|
43
|
+
end
|
44
|
+
|
45
|
+
[:cc, :bcc].each do |rcpt_type|
|
46
|
+
content[:headers] ||= {}
|
47
|
+
rcpt_type_val = mail.send(rcpt_type)
|
48
|
+
content[:headers][rcpt_type] = mail.send(rcpt_type).join(",") if rcpt_type_val
|
49
|
+
end
|
50
|
+
|
51
|
+
sp_object[:content] = content
|
52
|
+
sp_object.deep_merge!(mail.sparkpost_payload) if mail.sparkpost_payload
|
53
|
+
sp_object
|
54
|
+
end
|
55
|
+
|
56
|
+
# Transform array of Mail::Attachment object to array of Hash with base64 encoded
|
57
|
+
# attachment content
|
58
|
+
#
|
59
|
+
# @param [Array<Mail::Attachment>] attachments
|
60
|
+
#
|
61
|
+
# @return [Array<Hash>]
|
62
|
+
def transform_attachments(attachments)
|
63
|
+
attachments.map do |attachment|
|
64
|
+
{
|
65
|
+
name: attachment.filename,
|
66
|
+
type: attachment.content_type,
|
67
|
+
data: Base64.encode64(attachment.decoded)
|
68
|
+
}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Fetch email address and name from Mail::Address object.
|
73
|
+
#
|
74
|
+
# @param [Mail::Address] address
|
75
|
+
#
|
76
|
+
# @return [Hash]
|
77
|
+
def fetch_email_and_name(address)
|
78
|
+
hash = {}
|
79
|
+
hash[:email] = address.address if address.address
|
80
|
+
hash[:name] = address.name if address.name
|
81
|
+
hash
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sparkpost_rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sparkpost_rails5"
|
8
|
+
spec.version = SparkpostRails::VERSION
|
9
|
+
spec.authors = ["Muhlis BC"]
|
10
|
+
spec.email = ["muhlisbc@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "ActionMailer provider for sending mail through Sparkpost API."
|
13
|
+
spec.description = "Send mail through Sparkpost API from Rails 5 app."
|
14
|
+
spec.homepage = "https://github.com/muhlisbc/sparkpost_rails5"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features|bin)/})
|
19
|
+
end
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
|
27
|
+
spec.add_dependency "rails", "~> 5.0"
|
28
|
+
spec.add_dependency "simple_spark", "~> 1.0.0"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sparkpost_rails5
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Muhlis BC
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-27 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.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simple_spark
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.0.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.0.0
|
83
|
+
description: Send mail through Sparkpost API from Rails 5 app.
|
84
|
+
email:
|
85
|
+
- muhlisbc@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/sparkpost_rails.rb
|
98
|
+
- lib/sparkpost_rails/custom_payload.rb
|
99
|
+
- lib/sparkpost_rails/mailer.rb
|
100
|
+
- lib/sparkpost_rails/railtie.rb
|
101
|
+
- lib/sparkpost_rails/version.rb
|
102
|
+
- sparkpost_rails5.gemspec
|
103
|
+
homepage: https://github.com/muhlisbc/sparkpost_rails5
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.5.2
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: ActionMailer provider for sending mail through Sparkpost API.
|
127
|
+
test_files: []
|