mailchannels-worker-rails 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/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/lib/mailchannels-worker-rails.rb +3 -0
- data/lib/mailchannels_worker/railtie.rb +11 -0
- data/lib/mailchannels_worker/version.rb +5 -0
- data/lib/mailchannels_worker.rb +51 -0
- metadata +64 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 60be9f2512a2fe1484643715ad912c55e4dfde4f1a4d94ddf2bceb0de09835f9
|
|
4
|
+
data.tar.gz: 4aed5bf195625ec14ddc47659086f736ff5f2d288eb6090f1c09ad65a84581bf
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d3517c13348fc0be01c9c149ed8b13b2c15cb99642bb164310875efb2b8386bb886914afb9fc8a176070ccd95f1053870b95ec51c24ca8c6f451583e673414b4
|
|
7
|
+
data.tar.gz: 460f573d622d1a7ed8a1c60a4d73cf510b5400791c4bb51a31918835d39e4ebb22c22ff47e352b63db5e7add48c4a4a0100778445d0a82f79f731225fd4943ee
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Bruno Prieto
|
|
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,49 @@
|
|
|
1
|
+
# Mailchannels Worker Rails
|
|
2
|
+
|
|
3
|
+
This is an Action Mailer adapter that allows you to send emails using the [Mailchannels Cloudflare Worker](https://github.com/ment-labs/mailchannels-worker).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
8
|
+
|
|
9
|
+
$ bundle add mailchannels-worker-rails
|
|
10
|
+
|
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
12
|
+
|
|
13
|
+
$ gem install mailchannels-worker-rails
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
1. Start by deploying the [Mailchannels Worker](https://github.com/ment-labs/mailchannels-worker) on [Cloudflare Workers](https://workers.cloudflare.com/). Detailed instructions for this setup can be found in the [repository](https://github.com/ment-labs/mailchannels-worker), which includes all the necessary configurations.
|
|
18
|
+
|
|
19
|
+
2. Then, add the following lines to the corresponding environment file, such as "config/environment/production.rb":
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
config.action_mailer.delivery_method = :mailchannels_worker
|
|
23
|
+
config.action_mailer.mailchannels_worker_settings = {
|
|
24
|
+
url: "YOUR_WORKER_URL", # The URL where the worker is deployed
|
|
25
|
+
api_key: "YOUR_API_KEY" # The API Key generated and stored in the worker environment variables
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Ensure to replace "YOUR_WORKER_URL" with the actual path where the worker is deployed, and "YOUR_API_KEY" with the generated API Key stored in the worker environment variables.
|
|
30
|
+
|
|
31
|
+
Remember to store the API Key in a secure location, such as [Rails Credentials](https://guides.rubyonrails.org/security.html#custom-credentials) or secret environment variables.
|
|
32
|
+
|
|
33
|
+
## Development
|
|
34
|
+
|
|
35
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
36
|
+
|
|
37
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
38
|
+
|
|
39
|
+
## Contributing
|
|
40
|
+
|
|
41
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ment-labs/mailchannels-worker-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ment-labs/mailchannels-worker-rails/blob/main/CODE_OF_CONDUCT.md).
|
|
42
|
+
|
|
43
|
+
## License
|
|
44
|
+
|
|
45
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
46
|
+
|
|
47
|
+
## Code of Conduct
|
|
48
|
+
|
|
49
|
+
Everyone interacting in the MailchannelsWorker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ment-labs/mailchannels-worker-rails/blob/main/CODE_OF_CONDUCT.md).
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MailchannelsWorker
|
|
4
|
+
class Railtie < Rails::Railtie
|
|
5
|
+
initializer "mailchannels_worker.add_delivery_method", before: "action_mailer.set_configs" do
|
|
6
|
+
ActiveSupport.on_load :action_mailer do
|
|
7
|
+
ActionMailer::Base.add_delivery_method :mailchannels_worker, MailchannelsWorker::DeliveryMethod
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support"
|
|
4
|
+
require "active_support/core_ext/object/blank"
|
|
5
|
+
require "net/http"
|
|
6
|
+
require "json"
|
|
7
|
+
require_relative "mailchannels_worker/railtie" if defined?(Rails)
|
|
8
|
+
require_relative "mailchannels_worker/version"
|
|
9
|
+
|
|
10
|
+
module MailchannelsWorker
|
|
11
|
+
class DeliveryError < StandardError; end
|
|
12
|
+
|
|
13
|
+
class DeliveryMethod
|
|
14
|
+
RESERVED_HEADERS = %w[received dkim-signature Content-Type Content-Transfer-Encoding Message-ID To From Subject Reply-To CC BCC].freeze
|
|
15
|
+
|
|
16
|
+
attr_reader :url, :api_key
|
|
17
|
+
|
|
18
|
+
def initialize(settings = {})
|
|
19
|
+
@url = settings[:url]
|
|
20
|
+
@api_key = settings[:api_key]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def deliver!(mail)
|
|
24
|
+
body = {
|
|
25
|
+
from: { email: mail.from_address.address, name: mail.from_address.name }.compact,
|
|
26
|
+
subject: mail.subject,
|
|
27
|
+
personalizations: [{
|
|
28
|
+
headers: mail.header.fields.map { |field| [field.name, field.value] unless field.name.in?(RESERVED_HEADERS) }.compact.to_h.presence,
|
|
29
|
+
to: mail.to_addresses.map { |address| { email: address.address, name: address.name }.compact },
|
|
30
|
+
cc: mail.cc_addresses.map { |address| { email: address.address, name: address.name }.compact }.presence,
|
|
31
|
+
bcc: mail.bcc_addresses.map { |address| { email: address.address, name: address.name }.compact }.presence,
|
|
32
|
+
reply_to: (mail.header["Reply-To"]&.element&.addresses || []).map { |address| { email: address.address, name: address.name }.compact }.presence
|
|
33
|
+
}.compact],
|
|
34
|
+
content: mail.parts.map { |part| { type: part.content_type, value: part.decoded } }
|
|
35
|
+
}
|
|
36
|
+
send_email(body)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def send_email(email)
|
|
42
|
+
uri = URI.parse(url)
|
|
43
|
+
headers = {
|
|
44
|
+
"Authorization" => "Bearer #{api_key}",
|
|
45
|
+
"Content-Type" => "application/json"
|
|
46
|
+
}
|
|
47
|
+
response = Net::HTTP.post(uri, email.to_json, headers)
|
|
48
|
+
raise MailchannelsWorker::DeliveryError, response.body unless response.is_a?(Net::HTTPSuccess)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mailchannels-worker-rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Bruno Prieto
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2023-11-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: railties
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '6.1'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '6.1'
|
|
27
|
+
description:
|
|
28
|
+
email:
|
|
29
|
+
- brunoprietog@hey.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- LICENSE.txt
|
|
35
|
+
- README.md
|
|
36
|
+
- lib/mailchannels-worker-rails.rb
|
|
37
|
+
- lib/mailchannels_worker.rb
|
|
38
|
+
- lib/mailchannels_worker/railtie.rb
|
|
39
|
+
- lib/mailchannels_worker/version.rb
|
|
40
|
+
homepage: https://github.com/ment-labs/mailchannels-worker-rails
|
|
41
|
+
licenses:
|
|
42
|
+
- MIT
|
|
43
|
+
metadata:
|
|
44
|
+
rubygems_mfa_required: 'true'
|
|
45
|
+
post_install_message:
|
|
46
|
+
rdoc_options: []
|
|
47
|
+
require_paths:
|
|
48
|
+
- lib
|
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: 3.0.0
|
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '0'
|
|
59
|
+
requirements: []
|
|
60
|
+
rubygems_version: 3.4.22
|
|
61
|
+
signing_key:
|
|
62
|
+
specification_version: 4
|
|
63
|
+
summary: An Action Mailer adapter for Mailchannels Cloudflare Worker.
|
|
64
|
+
test_files: []
|