mail_plugger 1.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +22 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +16 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +50 -0
- data/.travis.yml +22 -0
- data/Appraisals +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +14 -0
- data/CONTRIBUTING.md +26 -0
- data/Gemfile +19 -0
- data/Gemfile.lock +92 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +11 -0
- data/bin/console +17 -0
- data/bin/setup +8 -0
- data/docs/usage_in_ruby_on_rails.md +182 -0
- data/docs/usage_in_script_or_console.md +158 -0
- data/gemfiles/.bundle/config +2 -0
- data/gemfiles/mail_2.6.gemfile +15 -0
- data/gemfiles/mail_2.6.gemfile.lock +95 -0
- data/gemfiles/mail_2.7.0.gemfile +15 -0
- data/gemfiles/mail_2.7.0.gemfile.lock +93 -0
- data/gemfiles/mail_2.7.gemfile +15 -0
- data/gemfiles/mail_2.7.gemfile.lock +93 -0
- data/images/mail_plugger.png +0 -0
- data/lib/mail_plugger.rb +97 -0
- data/lib/mail_plugger/delivery_method.rb +45 -0
- data/lib/mail_plugger/error.rb +25 -0
- data/lib/mail_plugger/mail_helper.rb +185 -0
- data/lib/mail_plugger/railtie.rb +14 -0
- data/lib/mail_plugger/version.rb +5 -0
- data/mail_plugger.gemspec +39 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b8f511638e18d76bc9e3c85d7fc7b0869765e64116b1118558560ed6b87eae69
|
4
|
+
data.tar.gz: 673381bb010b20fdba29dab25ed09b2dccb6c5b2c95e44a90e0f3524d75b40bc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b566e670154a0ffdd282b0d9ebaf297e797fb687e3192ddcea92e3e3810edc5f82b180cab8a57e469929f1fd531b3e217dc2b7990840277af7fbc9a731c92ee0
|
7
|
+
data.tar.gz: 21c9c007a6923fa03f5b5e1f536ad2a7ec2ff64d323c37e3ba3404ebeb3eef46350d29d1ec16de5935992f77d18ba2ff53b46413ade6f25fde5991059b032b7a
|
@@ -0,0 +1,22 @@
|
|
1
|
+
---
|
2
|
+
name: Bug report
|
3
|
+
about: Create a report to help us improve
|
4
|
+
---
|
5
|
+
|
6
|
+
**Describe the bug**
|
7
|
+
A clear and concise description of what the bug is.
|
8
|
+
|
9
|
+
**To Reproduce**
|
10
|
+
This is important! Providing us with a reliable way to reproduce a problem will expedite its solution.
|
11
|
+
|
12
|
+
**Expected behavior**
|
13
|
+
A clear and concise description of what you expected to happen.
|
14
|
+
|
15
|
+
**Actual behavior
|
16
|
+
Describe here what actually happened.
|
17
|
+
|
18
|
+
**Screenshots**
|
19
|
+
If applicable, add screenshots to help explain your problem.
|
20
|
+
|
21
|
+
**Additional context**
|
22
|
+
Add any other context about the problem here.
|
@@ -0,0 +1,16 @@
|
|
1
|
+
---
|
2
|
+
name: Feature request
|
3
|
+
about: Suggest an idea for this project
|
4
|
+
---
|
5
|
+
|
6
|
+
**Is your feature request related to a problem? Please describe.**
|
7
|
+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
8
|
+
|
9
|
+
**Describe the solution you'd like**
|
10
|
+
A clear and concise description of what you want to happen.
|
11
|
+
|
12
|
+
**Describe alternatives you've considered**
|
13
|
+
A clear and concise description of any alternative solutions or features you've considered.
|
14
|
+
|
15
|
+
**Additional context**
|
16
|
+
Add any other context or screenshots about the feature request here.
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
NewCops: enable
|
7
|
+
SuggestExtensions: false
|
8
|
+
Exclude:
|
9
|
+
- 'gemfiles/**/*'
|
10
|
+
|
11
|
+
Layout/LineLength:
|
12
|
+
Max: 80
|
13
|
+
|
14
|
+
Metrics/BlockLength:
|
15
|
+
Exclude:
|
16
|
+
- spec/**/*
|
17
|
+
|
18
|
+
Metrics/MethodLength:
|
19
|
+
Max: 20
|
20
|
+
|
21
|
+
Metrics/ModuleLength:
|
22
|
+
Max: 150
|
23
|
+
|
24
|
+
Lint/AmbiguousBlockAssociation:
|
25
|
+
Exclude:
|
26
|
+
- spec/**/*
|
27
|
+
|
28
|
+
Style/Documentation:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Style/DocumentationMethod:
|
32
|
+
Enabled: true
|
33
|
+
|
34
|
+
RSpec/ContextWording:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
RSpec/MessageChain:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
RSpec/MessageSpies:
|
41
|
+
EnforcedStyle: receive
|
42
|
+
|
43
|
+
RSpec/MultipleExpectations:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
RSpec/MultipleMemoizedHelpers:
|
47
|
+
Max: 10
|
48
|
+
|
49
|
+
RSpec/NestedGroups:
|
50
|
+
Enabled: false
|
data/.travis.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
---
|
2
|
+
env:
|
3
|
+
global:
|
4
|
+
- CC_TEST_REPORTER_ID=b510dcaf1de6f05f2fcdd623d5870628f3ab45124249a0819a63bea1257e8e79
|
5
|
+
language: ruby
|
6
|
+
cache: bundler
|
7
|
+
rvm:
|
8
|
+
- 2.5
|
9
|
+
- 2.6
|
10
|
+
- 2.7
|
11
|
+
- 3.0
|
12
|
+
before_install: gem install bundler -v 2.2.3
|
13
|
+
before_script:
|
14
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
15
|
+
- chmod +x ./cc-test-reporter
|
16
|
+
- ./cc-test-reporter before-build
|
17
|
+
gemfile:
|
18
|
+
- gemfiles/mail_2.7.gemfile
|
19
|
+
- gemfiles/mail_2.7.0.gemfile
|
20
|
+
- gemfiles/mail_2.6.gemfile
|
21
|
+
after_script:
|
22
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/Appraisals
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# The MailPlugger Community Code of Conduct
|
2
|
+
|
3
|
+
**Note:** We have picked the following code of conduct based on [Ruby's own code of conduct](https://www.ruby-lang.org/en/conduct/).
|
4
|
+
|
5
|
+
This document provides a few simple community guidelines for a safe, respectful,
|
6
|
+
productive, and collaborative place for any person who is willing to contribute
|
7
|
+
to the MailPlugger community. It applies to all "collaborative spaces", which are
|
8
|
+
defined as community communications channels (such as mailing lists, submitted
|
9
|
+
patches, commit comments, etc.).
|
10
|
+
|
11
|
+
* Participants will be tolerant of opposing views.
|
12
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
13
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
14
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
If you discover issues, have ideas for improvements or new features,
|
4
|
+
please report them to the [issue tracker](https://github.com/norbertszivos/mail_plugger/issues) of the repository or
|
5
|
+
submit a pull request. Please, try to follow these guidelines when you
|
6
|
+
do so.
|
7
|
+
|
8
|
+
## Issue reporting
|
9
|
+
|
10
|
+
* Check that the issue has not already been reported.
|
11
|
+
* Check that the issue has not already been fixed in the latest code.
|
12
|
+
* Be clear, concise and precise in your description of the problem.
|
13
|
+
* Open an issue with a descriptive title and summary.
|
14
|
+
* Include any relevant code to the issue summary.
|
15
|
+
|
16
|
+
## Pull requests
|
17
|
+
|
18
|
+
* Fork the project.
|
19
|
+
* Use a feature branch to easily amend a pull request later, if necessary.
|
20
|
+
* Write good commit messages.
|
21
|
+
* Use the same coding conventions as the rest of the project.
|
22
|
+
* If your change has a corresponding open GitHub issue, prefix the commit message with `[#github-issue-number]`.
|
23
|
+
* Make sure to add tests for it.
|
24
|
+
* Make sure the test suite is passing (usually this is as simple as running `bundle exec rake`).
|
25
|
+
* Squash related commits together.
|
26
|
+
* Open a pull request that relates to *only* one subject with a clear title and description.
|
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in mail_plugger.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'appraisal'
|
9
|
+
|
10
|
+
gem 'rake', '~> 13.0'
|
11
|
+
|
12
|
+
# Ruby static code analyzer and code formatter, code linter in short
|
13
|
+
gem 'rubocop', '~> 1.7', require: false
|
14
|
+
gem 'rubocop-performance', require: false
|
15
|
+
gem 'rubocop-rspec', require: false
|
16
|
+
|
17
|
+
gem 'rspec', '~> 3.0'
|
18
|
+
gem 'simplecov', require: false
|
19
|
+
gem 'webmock', '~> 3.0'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mail_plugger (1.0.0.beta1)
|
5
|
+
mail (~> 2.5)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.7.0)
|
11
|
+
public_suffix (>= 2.0.2, < 5.0)
|
12
|
+
appraisal (2.3.0)
|
13
|
+
bundler
|
14
|
+
rake
|
15
|
+
thor (>= 0.14.0)
|
16
|
+
ast (2.4.1)
|
17
|
+
crack (0.4.5)
|
18
|
+
rexml
|
19
|
+
diff-lcs (1.4.4)
|
20
|
+
docile (1.3.4)
|
21
|
+
hashdiff (1.0.1)
|
22
|
+
mail (2.7.1)
|
23
|
+
mini_mime (>= 0.1.1)
|
24
|
+
mini_mime (1.0.2)
|
25
|
+
parallel (1.20.1)
|
26
|
+
parser (3.0.0.0)
|
27
|
+
ast (~> 2.4.1)
|
28
|
+
public_suffix (4.0.6)
|
29
|
+
rainbow (3.0.0)
|
30
|
+
rake (13.0.3)
|
31
|
+
regexp_parser (2.0.3)
|
32
|
+
rexml (3.2.4)
|
33
|
+
rspec (3.10.0)
|
34
|
+
rspec-core (~> 3.10.0)
|
35
|
+
rspec-expectations (~> 3.10.0)
|
36
|
+
rspec-mocks (~> 3.10.0)
|
37
|
+
rspec-core (3.10.1)
|
38
|
+
rspec-support (~> 3.10.0)
|
39
|
+
rspec-expectations (3.10.1)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.10.0)
|
42
|
+
rspec-mocks (3.10.1)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.10.0)
|
45
|
+
rspec-support (3.10.1)
|
46
|
+
rubocop (1.7.0)
|
47
|
+
parallel (~> 1.10)
|
48
|
+
parser (>= 2.7.1.5)
|
49
|
+
rainbow (>= 2.2.2, < 4.0)
|
50
|
+
regexp_parser (>= 1.8, < 3.0)
|
51
|
+
rexml
|
52
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
53
|
+
ruby-progressbar (~> 1.7)
|
54
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
55
|
+
rubocop-ast (1.3.0)
|
56
|
+
parser (>= 2.7.1.5)
|
57
|
+
rubocop-performance (1.9.2)
|
58
|
+
rubocop (>= 0.90.0, < 2.0)
|
59
|
+
rubocop-ast (>= 0.4.0)
|
60
|
+
rubocop-rspec (2.1.0)
|
61
|
+
rubocop (~> 1.0)
|
62
|
+
rubocop-ast (>= 1.1.0)
|
63
|
+
ruby-progressbar (1.11.0)
|
64
|
+
simplecov (0.20.0)
|
65
|
+
docile (~> 1.1)
|
66
|
+
simplecov-html (~> 0.11)
|
67
|
+
simplecov_json_formatter (~> 0.1)
|
68
|
+
simplecov-html (0.12.3)
|
69
|
+
simplecov_json_formatter (0.1.2)
|
70
|
+
thor (1.0.1)
|
71
|
+
unicode-display_width (1.7.0)
|
72
|
+
webmock (3.11.0)
|
73
|
+
addressable (>= 2.3.6)
|
74
|
+
crack (>= 0.3.2)
|
75
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
76
|
+
|
77
|
+
PLATFORMS
|
78
|
+
ruby
|
79
|
+
|
80
|
+
DEPENDENCIES
|
81
|
+
appraisal
|
82
|
+
mail_plugger!
|
83
|
+
rake (~> 13.0)
|
84
|
+
rspec (~> 3.0)
|
85
|
+
rubocop (~> 1.7)
|
86
|
+
rubocop-performance
|
87
|
+
rubocop-rspec
|
88
|
+
simplecov
|
89
|
+
webmock (~> 3.0)
|
90
|
+
|
91
|
+
BUNDLED WITH
|
92
|
+
2.2.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Norbert Szivós
|
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,51 @@
|
|
1
|
+
# M<img src="https://github.com/norbertszivos/mail_plugger/blob/main/images/mail_plugger.png" height="25" />ilPlugger
|
2
|
+
|
3
|
+
[![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop-hq/rubocop)
|
4
|
+
[![Build Status](https://travis-ci.com/norbertszivos/mail_plugger.svg?branch=main)](https://travis-ci.com/norbertszivos/mail_plugger)
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/bd2cda43214c111d8d16/maintainability)](https://codeclimate.com/github/norbertszivos/mail_plugger/maintainability)
|
6
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/bd2cda43214c111d8d16/test_coverage)](https://codeclimate.com/github/norbertszivos/mail_plugger/test_coverage)
|
7
|
+
|
8
|
+
**MailPlugger** helps you to use different mail providers' **API**. You can use any APIs which one would like to use. It allows you to send different emails with different APIs. Also it can help to move between providers, load balacing or cost management.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'mail_plugger'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle install
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install mail_plugger
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
[Use MailPlugger in a Ruby script or IRB console](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_in_script_or_console.md)
|
29
|
+
|
30
|
+
[Use MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_in_ruby_on_rails.md)
|
31
|
+
|
32
|
+
## Development
|
33
|
+
|
34
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
35
|
+
|
36
|
+
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).
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
Bug reports and pull requests are welcome. Please read [CONTRIBUTING.md](https://github.com/norbertszivos/mail_plugger/blob/main/CONTRIBUTING.md) if you would like to contribute to this project.
|
41
|
+
|
42
|
+
## Inspiration
|
43
|
+
|
44
|
+
- [T-mailer](https://github.com/100Starlings/t-mailer)
|
45
|
+
- [Mandrill DM](https://github.com/kshnurov/mandrill_dm)
|
46
|
+
- [SparkPost Rails](https://github.com/the-refinery/sparkpost_rails)
|
47
|
+
- and other solutions regarding in this topic
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
The gem is available as open source under the terms of the [MIT License](https://github.com/norbertszivos/mail_plugger/blob/main/LICENSE.txt).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'mail_plugger'
|
7
|
+
require 'mail'
|
8
|
+
|
9
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
10
|
+
# with your gem easier. You can also use a different console, if you like.
|
11
|
+
|
12
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
13
|
+
# require "pry"
|
14
|
+
# Pry.start
|
15
|
+
|
16
|
+
require 'irb'
|
17
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
## Use MailPlugger in Ruby on Rails
|
2
|
+
|
3
|
+
After to add `mail_plugger` gem and the gem of API of the mail provider, create `config/initializers/mail_plugger.rb` file and add something similar.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# NOTE: This is just an example for testing...
|
7
|
+
class TestApiClientClass
|
8
|
+
def initialize(options = {})
|
9
|
+
@settings = { api_key: '12345' }
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
def deliver
|
14
|
+
# e.g. API.new(@settings).client.post(generate_mail_hash)
|
15
|
+
puts " >>> settings: #{@settings.inspect}"
|
16
|
+
puts " >>> options: #{@options.inspect}"
|
17
|
+
puts " >>> generate_mail_hash: #{generate_mail_hash.inspect}"
|
18
|
+
{ response: 'OK' }
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def generate_mail_hash
|
24
|
+
{
|
25
|
+
to: generate_recipients,
|
26
|
+
from: {
|
27
|
+
email: @options[:from].first
|
28
|
+
},
|
29
|
+
subject: @options[:subject],
|
30
|
+
content: [
|
31
|
+
{
|
32
|
+
type: 'text/plain',
|
33
|
+
value: @options[:text_part]
|
34
|
+
},
|
35
|
+
{
|
36
|
+
type: 'text/html; charset=UTF-8',
|
37
|
+
value: @options[:html_part]
|
38
|
+
}
|
39
|
+
]
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
def generate_recipients
|
44
|
+
@options[:to].map do |to|
|
45
|
+
{
|
46
|
+
email: to
|
47
|
+
}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
MailPlugger.plug_in('test_api_client') do |api|
|
53
|
+
api.delivery_options = %i[from to subject text_part html_part]
|
54
|
+
api.client = TestApiClientClass
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
Then change `config/application.rb` file.
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
config.action_mailer.delivery_method = :mail_plugger
|
62
|
+
```
|
63
|
+
|
64
|
+
So now we should add a mailer method. Let's create `app/mailers/test_mailer.rb` file.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class TestMailer < ApplicationMailer
|
68
|
+
default from: 'from@example.com'
|
69
|
+
|
70
|
+
def send_test
|
71
|
+
mail subject: 'Test email', to: 'to@example.com'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
Then we should add views (the body) of this email, so create `app/views/test_mailer/send_test.html.erb`
|
77
|
+
|
78
|
+
```erb
|
79
|
+
<p>Test email body</p>
|
80
|
+
```
|
81
|
+
|
82
|
+
and `app/views/test_mailer/send_test.text.erb`.
|
83
|
+
|
84
|
+
```erb
|
85
|
+
Test email body
|
86
|
+
```
|
87
|
+
|
88
|
+
In the `rails console` we can try it out.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
TestMailer.send_test.deliver_now
|
92
|
+
# Rendering test_mailer/send_test.html.erb within layouts/mailer
|
93
|
+
# Rendered test_mailer/send_test.html.erb within layouts/mailer (1.3ms)
|
94
|
+
# Rendering test_mailer/send_test.text.erb within layouts/mailer
|
95
|
+
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.5ms)
|
96
|
+
#TestMailer#send_test: processed outbound mail in 62.2ms
|
97
|
+
# >>> settings: {:api_key=>"12345"}
|
98
|
+
# >>> options: {:from=>["from@example.com"], :to=>["to@example.com"], :subject=>"Test email", :text_part=>"Test email body\n\n", :html_part=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
|
99
|
+
# >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
|
100
|
+
#Sent mail to to@example.com (12.2ms)
|
101
|
+
#Date: Sat, 02 Jan 2021 15:08:53 +0100
|
102
|
+
#From: from@example.com
|
103
|
+
#To: to@example.com
|
104
|
+
#Message-ID: <5ff07e7597b40_104cfebb4988d3@100S-Mac.local.mail>
|
105
|
+
#Subject: Test email
|
106
|
+
#Mime-Version: 1.0
|
107
|
+
#Content-Type: multipart/alternative;
|
108
|
+
# boundary="--==_mimepart_5ff07e75956a7_104cfebb498739";
|
109
|
+
# charset=UTF-8
|
110
|
+
#Content-Transfer-Encoding: 7bit
|
111
|
+
#
|
112
|
+
#
|
113
|
+
#----==_mimepart_5ff07e75956a7_104cfebb498739
|
114
|
+
#Content-Type: text/plain;
|
115
|
+
# charset=UTF-8
|
116
|
+
#Content-Transfer-Encoding: 7bit
|
117
|
+
#
|
118
|
+
#Test email body
|
119
|
+
#
|
120
|
+
#
|
121
|
+
#----==_mimepart_5ff07e75956a7_104cfebb498739
|
122
|
+
#Content-Type: text/html;
|
123
|
+
# charset=UTF-8
|
124
|
+
#Content-Transfer-Encoding: 7bit
|
125
|
+
#
|
126
|
+
#<!DOCTYPE html>
|
127
|
+
#<html>
|
128
|
+
# <head>
|
129
|
+
# <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
130
|
+
# <style>
|
131
|
+
# /* Email styles need to be inline */
|
132
|
+
# </style>
|
133
|
+
# </head>
|
134
|
+
#
|
135
|
+
# <body>
|
136
|
+
# <p>Test email body</p>
|
137
|
+
#
|
138
|
+
# </body>
|
139
|
+
#</html>
|
140
|
+
#
|
141
|
+
#----==_mimepart_5ff07e75956a7_104cfebb498739--
|
142
|
+
|
143
|
+
#=> #<Mail::Message:61100, Multipart: true, Headers: <Date: Sat, 02 Jan 2021 15:08:53 +0100>, <From: from@example.com>, <To: to@example.com>, <Message-ID: <5ff07e7597b40_104cfebb4988d3@100S-Mac.local.mail>>, <Subject: Test email>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_5ff07e75956a7_104cfebb498739"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>
|
144
|
+
|
145
|
+
# or use ! to not render mail
|
146
|
+
|
147
|
+
TestMailer.send_test.deliver_now!
|
148
|
+
# Rendering test_mailer/send_test.html.erb within layouts/mailer
|
149
|
+
# Rendered test_mailer/send_test.html.erb within layouts/mailer (0.1ms)
|
150
|
+
# Rendering test_mailer/send_test.text.erb within layouts/mailer
|
151
|
+
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.0ms)
|
152
|
+
#TestMailer#send_test: processed outbound mail in 20.9ms
|
153
|
+
# >>> settings: {:api_key=>"12345"}
|
154
|
+
# >>> options: {:from=>["from@example.com"], :to=>["to@example.com"], :subject=>"Test email", :text_part=>"Test email body\n\n", :html_part=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
|
155
|
+
# >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
|
156
|
+
#=> #<Mail::Message:61140, Multipart: true, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Test email>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_5ff082bd7aab5_10afcebb4503a4"; charset=UTF-8>>
|
157
|
+
```
|
158
|
+
|
159
|
+
Let's add delivery settings as well in `config/initializers/mail_plugger.rb`.
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
MailPlugger.plug_in('test_api_client') do |api|
|
163
|
+
api.delivery_options = %i[from to subject text_part html_part]
|
164
|
+
api.delivery_settings = { return_response: true }
|
165
|
+
api.client = TestApiClientClass
|
166
|
+
end
|
167
|
+
```
|
168
|
+
|
169
|
+
Then in the `rails console`.
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
TestMailer.send_test.deliver_now!
|
173
|
+
# Rendering test_mailer/send_test.html.erb within layouts/mailer
|
174
|
+
# Rendered test_mailer/send_test.html.erb within layouts/mailer (0.8ms)
|
175
|
+
# Rendering test_mailer/send_test.text.erb within layouts/mailer
|
176
|
+
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.4ms)
|
177
|
+
#TestMailer#send_test: processed outbound mail in 37.0ms
|
178
|
+
# >>> settings: {:api_key=>"12345"}
|
179
|
+
# >>> options: {:from=>["from@example.com"], :to=>["to@example.com"], :subject=>"Test email", :text_part=>"Test email body\n\n", :html_part=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
|
180
|
+
# >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
|
181
|
+
#=> {:response=>"OK"}
|
182
|
+
```
|