mailpace-rails 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d7d4f376af21674d306c2b4edffecef9a2a74c7476270a385756ba5a3804b17d
4
+ data.tar.gz: 58112506f977914969364d5e764cc32d9a0099083698f1db23c913e25e725175
5
+ SHA512:
6
+ metadata.gz: 0d5ae0e7b05188ca5b01539bab467d92bd018a4ebfb3634f384de567ed4e66f252d1a3fec466759717832e0a1fd833fd0f9556f9f82890c5e29ccfb395f86d2c
7
+ data.tar.gz: 83bf637bbde3f65891960dc4f5f554104080a94aaa9f40d604234ad9a60312ead5fa8c93351d44c6218183baf8dcfafa832321a4f051fa23389d16fcf486fe33
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2020 OhMySMTP
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,149 @@
1
+ # MailPace::Rails
2
+
3
+ [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
4
+ [![Gem Version](https://badge.fury.io/rb/mailpace-rails.svg)](https://badge.fury.io/rb/mailpace-rails)
5
+ [![MailPace Rails](https://circleci.com/gh/mailpace/mailpace-rails.svg?style=svg)](https://app.circleci.com/pipelines/github/mailpace/mailpace-rails)
6
+
7
+ [MailPace](https://mailpace.com) lets you send transactional emails from your app over an easy to use API.
8
+
9
+ The MailPace Rails Gem is a plug in for ActionMailer to send emails via [MailPace](https://mailpace.com) to make sending emails from Rails apps super simple.
10
+
11
+ > **New in 0.3.0: The ability to consume [inbound emails](https://docs.mailpace.com/guide/inbound/) from MailPace via ActionMailbox**
12
+
13
+ ## Usage
14
+
15
+ Once installed and configured, continue to send emails using [ActionMailer](https://guides.rubyonrails.org/action_mailer_basics.html) and receive emails with [ActionMailbox](https://edgeguides.rubyonrails.org/action_mailbox_basics.html) like normal.
16
+
17
+ ## Other Requirements
18
+
19
+ You will need an MailPace account with a verified domain and organization with an active plan.
20
+
21
+ ## Installation
22
+
23
+ ### Account Setup
24
+
25
+ Set up an account at [MailPace](https://app.mailpace.com/users/sign_up) and complete the Onboarding steps
26
+
27
+ ### Gem Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ ```ruby
32
+ gem 'mailpace-rails'
33
+ ```
34
+
35
+ And then execute:
36
+ ```bash
37
+ $ bundle
38
+ ```
39
+
40
+ Or install it yourself as:
41
+ ```bash
42
+ $ gem install mailpace-rails
43
+ ```
44
+
45
+ ### Configure the Gem
46
+
47
+ First you will need to retrieve your API token for your sending domain from [MailPace](https://app.mailpace.com). You can find it under Organization -> Domain -> API Tokens.
48
+
49
+ Use the encrypted secret management to save your API Token to `config/credentials.yml.enc` by running the following:
50
+
51
+ ```bash
52
+ rails secret
53
+ rails credentials:edit
54
+ ```
55
+
56
+ Then add your token:
57
+
58
+ ```yaml
59
+ mailpace_api_token: "TOKEN_GOES_HERE"
60
+ ```
61
+
62
+ Set MailPace as your mail delivery method in `config/application.rb`:
63
+
64
+ ```ruby
65
+ config.action_mailer.delivery_method = :mailpace
66
+ config.action_mailer.mailpace_settings = { api_token: Rails.application.credentials.mailpace_api_token }
67
+ ```
68
+
69
+ ## Tagging
70
+
71
+ You can tag messages and filter them later in the MailPace UI. To do this, pass the tags as a header by adding a tag variable to your `mail` method call.
72
+
73
+ ```ruby
74
+ class TestMailer < ApplicationMailer
75
+ default from: 'notifications@example.com',
76
+ to: 'fake@sdfasdfsdaf.com'
77
+
78
+ def single_tag
79
+ mail(
80
+ tags: 'test tag' # One tag
81
+ )
82
+ end
83
+
84
+ def multi_tag
85
+ mail(
86
+ tags: "['test tag', 'another-tag']" # Multiple tags
87
+ )
88
+ end
89
+ end
90
+ ```
91
+
92
+ Note that this should always be a string, even if using an array of multiple tags.
93
+
94
+ ## List-Unsubscribe
95
+
96
+ To add a List-Unsubscribe header, pass a `list_unsubscribe` string to the `mail` function:
97
+
98
+ ```ruby
99
+ class TestMailer < ApplicationMailer
100
+ default from: 'notifications@example.com',
101
+ to: 'fake@sdfasdfsdaf.com'
102
+
103
+ def list_unsub_header
104
+ mail(
105
+ list_unsubscribe: 'https://listunsublink.com'
106
+ )
107
+ end
108
+ end
109
+ ```
110
+
111
+ ## ActionMailbox (for receiving inbound emails)
112
+
113
+ As of v0.3.0, this Gem supports handling Inbound Emails (see https://docs.mailpace.com/guide/inbound/ for more details) via ActionMailbox. To set this up:
114
+
115
+ 1. Tell Action Mailbox to accept emails from MailPace in `config/environments/production.rb`
116
+
117
+ ```ruby
118
+ config.action_mailbox.ingress = :mailpace
119
+ ```
120
+
121
+ 2. Generate a strong password that Action Mailbox can use to authenticate requests to the MailPace ingress.
122
+ Use `bin/rails credentials:edit` to add the password to your application's encrypted credentials under `action_mailbox.ingress_password`, where Action Mailbox will automatically find it:
123
+
124
+ ```yaml
125
+ action_mailbox:
126
+ ingress_password: ...
127
+ ```
128
+
129
+ Alternatively, provide the password in the `RAILS_INBOUND_EMAIL_PASSWORD` environment variable.
130
+
131
+ 3. Configure MailPace to forward inbound emails to `/rails/action_mailbox/mailpace/inbound_emails` with the username `actionmailbox` and the password you previously generated. If your application lived at `https://example.com` you would configure your MailPace inbound endpoint URL with the following fully-qualified URL:
132
+
133
+ `https://actionmailbox:PASSWORD@example.com/rails/action_mailbox/mailpace/inbound_emails`
134
+
135
+ That's it! Emails should start flowing into your app just like magic.
136
+ ## Support
137
+
138
+ For support please check the [MailPace Documentation](https://docs.mailpace.com) or contact us at support@mailpace.com
139
+
140
+ ## Contributing
141
+
142
+ Please ensure to add a test for any change you make. To run the tests:
143
+
144
+ `bin/test`
145
+
146
+ Pull requests always welcome
147
+
148
+ ## License
149
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Mailpace::Rails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionMailbox
4
+ module Ingresses
5
+ module Mailpace
6
+ # Ingests inbound emails from MailPace. Uses the a +raw+ parameter containing the full RFC 822 message.
7
+ #
8
+ # Authenticates requests using HTTP basic access authentication. The username is always +actionmailbox+, and the
9
+ # password is read from the application's encrypted credentials or an environment variable. See the Usage section.
10
+ #
11
+ # Returns:
12
+ #
13
+ # - <tt>204 No Content</tt> if an inbound email is successfully recorded and enqueued for routing
14
+ # - <tt>401 Unauthorized</tt> if the request's signature could not be validated
15
+ # - <tt>404 Not Found</tt> if Action Mailbox is not configured to accept inbound emails from MailPace
16
+ # - <tt>422 Unprocessable Entity</tt> if the request is missing the required +RawEmail+ parameter
17
+ # - <tt>500 Server Error</tt> if the ingress password is not configured, or if one of the Active Record database,
18
+ # the Active Storage service, or the Active Job backend is misconfigured or unavailable
19
+ #
20
+ # == Usage
21
+ #
22
+ # 1. Tell Action Mailbox to accept emails from MailPace:
23
+ #
24
+ # # config/environments/production.rb
25
+ # config.action_mailbox.ingress = :mailpace
26
+ #
27
+ # 2. Generate a strong password that Action Mailbox can use to authenticate requests to the MailPace ingress.
28
+ #
29
+ # Use <tt>bin/rails credentials:edit</tt> to add the password to your application's encrypted credentials under
30
+ # +action_mailbox.ingress_password+, where Action Mailbox will automatically find it:
31
+ #
32
+ # action_mailbox:
33
+ # ingress_password: ...
34
+ #
35
+ # Alternatively, provide the password in the +RAILS_INBOUND_EMAIL_PASSWORD+ environment variable.
36
+ #
37
+ # 3. {Configure MailPace}[https://docs.mailpace.com/guide/inbound] to forward inbound emails
38
+ # to +/rails/action_mailbox/mailpace/inbound_emails+ with the username +actionmailbox+ and the password you
39
+ # previously generated. If your application lived at <tt>https://example.com</tt>, you would configure your
40
+ # MailPace inbound endpoint URL with the following fully-qualified URL:
41
+ #
42
+ # https://actionmailbox:PASSWORD@example.com/rails/action_mailbox/mailpace/inbound_emails
43
+ #
44
+ class InboundEmailsController < ActionMailbox::BaseController
45
+ before_action :authenticate_by_password
46
+
47
+ def create
48
+ ActionMailbox::InboundEmail.create_and_extract_message_id! params.require('raw')
49
+ rescue ActionController::ParameterMissing => e
50
+ logger.error e.message
51
+
52
+ head :unprocessable_entity
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ Rails.application.routes.draw do
2
+ post 'rails/action_mailbox/mailpace/inbound_emails', to: 'action_mailbox/ingresses/mailpace/inbound_emails#create',
3
+ as: 'rails_mailpace_inbound_emails'
4
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailpace
4
+ # Provides the delivery method & sets up action mailbox
5
+ class Engine < ::Rails::Engine
6
+ initializer 'mailpace.add_delivery_method', before: 'action_mailer.set_configs' do
7
+ ActionMailer::Base.add_delivery_method(:mailpace, Mailpace::DeliveryMethod)
8
+ end
9
+
10
+ config.action_mailbox.mailpace = ActiveSupport::OrderedOptions.new
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Mailpace
2
+ module Rails
3
+ VERSION = '0.3.0'
4
+ end
5
+ end
@@ -0,0 +1,88 @@
1
+ require 'action_mailer'
2
+ require 'action_mailbox/engine'
3
+ require 'httparty'
4
+ require 'uri'
5
+ require 'json'
6
+ require 'mailpace-rails/version'
7
+ require 'mailpace-rails/engine' if defined? Rails
8
+
9
+ module Mailpace
10
+ # MailPace ActionMailer delivery method
11
+ class DeliveryMethod
12
+ attr_accessor :settings
13
+
14
+ def initialize(values)
15
+ check_api_token(values)
16
+ self.settings = { return_response: true }.merge!(values)
17
+ end
18
+
19
+ def deliver!(mail)
20
+ check_delivery_params(mail)
21
+ result = HTTParty.post(
22
+ 'https://app.mailpace.com/api/v1/send',
23
+ body: {
24
+ from: mail.header[:from]&.address_list&.addresses&.first.to_s,
25
+ to: mail.to.join(','),
26
+ subject: mail.subject,
27
+ htmlbody: mail.html_part ? mail.html_part.body.decoded : mail.body.to_s,
28
+ textbody: if mail.multipart?
29
+ mail.text_part ? mail.text_part.body.decoded : nil
30
+ end,
31
+ cc: mail.cc&.join(','),
32
+ bcc: mail.bcc&.join(','),
33
+ replyto: mail.reply_to,
34
+ list_unsubscribe: mail.header['list_unsubscribe'].to_s,
35
+ attachments: format_attachments(mail.attachments),
36
+ tags: mail.header['tags'].to_s
37
+ }.delete_if { |_key, value| value.blank? }.to_json,
38
+ headers: {
39
+ 'User-Agent' => "MailPace Rails Gem v#{Mailpace::Rails::VERSION}",
40
+ 'Accept' => 'application/json',
41
+ 'Content-Type' => 'application/json',
42
+ 'Mailpace-Server-Token' => settings[:api_token]
43
+ }
44
+ )
45
+
46
+ handle_response(result)
47
+ end
48
+
49
+ private
50
+
51
+ def check_api_token(values)
52
+ return if values[:api_token].present?
53
+
54
+ raise ArgumentError, 'MailPace API token is not set'
55
+ end
56
+
57
+ def check_delivery_params(mail)
58
+ return unless mail.from.nil? || mail.to.nil?
59
+
60
+ raise ArgumentError, 'Missing to or from address in email'
61
+ end
62
+
63
+ def handle_response(result)
64
+ return result unless result.code != 200
65
+
66
+ # TODO: Improved error handling
67
+ res = result.parsed_response
68
+ raise res['error']&.to_s || res['errors']&.to_s
69
+ end
70
+
71
+ def format_attachments(attachments)
72
+ attachments.map do |attachment|
73
+ {
74
+ name: attachment.filename,
75
+ content_type: attachment.mime_type,
76
+ content: Base64.encode64(attachment.body.encoded),
77
+ cid: attachment.content_id
78
+ }.compact
79
+ end
80
+ end
81
+ end
82
+
83
+ class Error < StandardError; end
84
+
85
+ def self.root
86
+ Pathname.new(File.expand_path(File.join(__dir__, '..')))
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mailpace-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - MailPace
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionmailer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.18.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.18.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 6.1.4.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 6.1.4.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.4.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.4.2
69
+ description: The MailPace Rails Gem is a plug in for ActionMailer to send emails via
70
+ MailPace to make sending emails from Rails apps super simple.
71
+ email:
72
+ - support@mailpace.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - MIT-LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - app/controllers/action_mailbox/ingresses/mailpace/inbound_emails_controller.rb
81
+ - config/routes.rb
82
+ - lib/mailpace-rails.rb
83
+ - lib/mailpace-rails/engine.rb
84
+ - lib/mailpace-rails/version.rb
85
+ homepage: https://mailpace.com
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.1.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Lets you send transactional emails from your app over an easy to use API
108
+ test_files: []