-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ActionMailer support #3
Merged
+392
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,8 @@ | |
## [1.0.1] - 2022-06-20 | ||
|
||
- Update packed files list | ||
|
||
|
||
## [1.1.0] - 2022-07-22 | ||
|
||
- Add ActionMailer support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,27 @@ client = Mailtrap::Sending::Client.new(api_key: 'your-api-key') | |
client.send(mail) | ||
``` | ||
|
||
### ActionMailer | ||
|
||
This gem also adds ActionMailer delivery method. To configure it, add following to your ActionMailer configuration (in Rails projects located in `config/$ENVIRONMENT.rb`): | ||
```ruby | ||
config.action_mailer.delivery_method = :mailtrap | ||
config.action_mailer.mailtrap_settings = { | ||
api_key: ENV.fetch('MAILTRAP_API_KEY') | ||
} | ||
``` | ||
And continue to use ActionMailer as usual. | ||
|
||
To add `category` and `custom_variables`, add them to the mail generation: | ||
```ruby | ||
mail( | ||
to: '[email protected]', | ||
subject: 'You are awesome!', | ||
category: 'Test category', | ||
custom_variables: { test_variable: 'abc' } | ||
) | ||
``` | ||
|
||
## Development | ||
|
||
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'action_mailer/delivery_method' | ||
require_relative 'action_mailer/railtie' if defined? Rails | ||
|
||
module Mailtrap | ||
module ActionMailer; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mailtrap | ||
module ActionMailer | ||
class DeliveryMethod | ||
attr_accessor :settings | ||
|
||
def initialize(settings) | ||
self.settings = settings | ||
end | ||
|
||
def deliver!(message) | ||
mail = Mailtrap::Sending::Convert.from_message(message) | ||
|
||
client.send(mail) | ||
end | ||
|
||
private | ||
|
||
def client | ||
@client ||= Mailtrap::Sending::Client.new(**settings.slice(:api_key, :api_host, :api_port)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mailtrap | ||
module ActionMailer | ||
class Railtie < Rails::Railtie | ||
initializer 'mailtrap_action_mailer.add_delivery_method', before: 'action_mailer.set_configs' do | ||
ActiveSupport.on_load(:action_mailer) do | ||
::ActionMailer::Base.add_delivery_method(:mailtrap, Mailtrap::ActionMailer::DeliveryMethod) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'base64' | ||
|
||
module Mailtrap | ||
module Sending | ||
module Convert | ||
class << self | ||
def from_message(message) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity | ||
Mailtrap::Sending::Mail.new( | ||
from: prepare_address(message['from']&.address_list&.addresses&.first), | ||
to: prepare_addresses(message['to']&.address_list&.addresses), | ||
cc: prepare_addresses(message['cc']&.address_list&.addresses), | ||
bcc: prepare_addresses(message['bcc']&.address_list&.addresses), | ||
subject: message.subject, | ||
text: prepare_text_part(message), | ||
html: prepare_html_part(message), | ||
headers: prepare_headers(message), | ||
attachments: prepare_attachments(message.attachments), | ||
category: message['category']&.unparsed_value, | ||
custom_variables: message['custom_variables']&.unparsed_value | ||
) | ||
end | ||
|
||
private | ||
|
||
PROCESSED_HEADERS = %w[ | ||
from | ||
to | ||
cc | ||
bcc | ||
subject | ||
category | ||
customvariables | ||
contenttype | ||
].freeze | ||
|
||
def prepare_addresses(addresses) | ||
Array(addresses).map { |address| prepare_address(address) } | ||
end | ||
|
||
def prepare_headers(message) | ||
message | ||
.header_fields | ||
.reject { |header| PROCESSED_HEADERS.include?(header.name.downcase.delete('-')) } | ||
.to_h { |header| [header.name, header.value] } | ||
.compact | ||
end | ||
|
||
def prepare_address(address) | ||
{ | ||
email: address.address, | ||
name: address.display_name | ||
}.compact | ||
end | ||
|
||
def prepare_attachments(attachments_list = []) | ||
attachments_list.map do |attachment| | ||
{ | ||
content: Base64.strict_encode64(attachment.body.decoded), | ||
type: attachment.mime_type, | ||
filename: attachment.filename, | ||
disposition: attachment.header[:content_disposition]&.disposition_type, | ||
content_id: attachment.header[:content_id]&.field&.content_id | ||
}.compact | ||
end | ||
end | ||
|
||
def prepare_html_part(message) | ||
return message.body.decoded if message.mime_type == 'text/html' | ||
|
||
message.html_part&.decoded | ||
end | ||
|
||
def prepare_text_part(message) | ||
return message.body.decoded if message.mime_type == 'text/plain' || message.mime_type.nil? | ||
|
||
message.text_part&.decoded | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mailtrap | ||
VERSION = '1.0.1' | ||
VERSION = '1.1.0' | ||
end |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a text file |
41 changes: 41 additions & 0 deletions
41
...Mailtrap_ActionMailer_DeliveryMethod/_deliver_/converts_the_message_and_sends_via_API.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'mailtrap/action_mailer' | ||
|
||
RSpec.describe Mailtrap::ActionMailer::DeliveryMethod, :vcr do | ||
describe '#deliver!' do | ||
subject(:deliver!) { described_class.new(settings).deliver!(message) } | ||
|
||
let(:settings) { { api_key: 'correct-api-key' } } | ||
let(:message) { Mail::Message.new(params) } | ||
let(:params) do | ||
{ | ||
from: 'Mailtrap Test <[email protected]>', | ||
to: 'To 1 <[email protected]>, [email protected]', | ||
cc: '[email protected], Cc 2 <[email protected]>', | ||
bcc: '[email protected], [email protected]', | ||
reply_to: '[email protected]', | ||
subject: 'You are awesome!', | ||
category: 'Module Test' | ||
} | ||
end | ||
let(:expected_message_ids) do | ||
%w[ | ||
858cbc46-09d5-11ed-91e0-0a58a9feac02 | ||
858cbc5c-09d5-11ed-91e0-0a58a9feac02 | ||
858cbc6d-09d5-11ed-91e0-0a58a9feac02 | ||
858cbc7e-09d5-11ed-91e0-0a58a9feac02 | ||
858cbc90-09d5-11ed-91e0-0a58a9feac02 | ||
858cbca1-09d5-11ed-91e0-0a58a9feac02 | ||
] | ||
end | ||
|
||
before do | ||
allow(::Mail::ContentTypeField).to receive(:generate_boundary).and_return('--==_mimepart_random_boundary') | ||
message.text_part = 'Some text' | ||
message.html_part = '<div>HTML part</div>' | ||
message.headers('X-Special-Domain-Specific-Header': 'SecretValue') | ||
message.headers('One-more-custom-header': 'CustomValue') | ||
message.attachments['file.txt'] = File.read('spec/fixtures/files/attachments/file.txt') | ||
message.attachments['file.txt'].content_id = '<[email protected]>' | ||
message.attachments.inline['file.png'] = File.read('spec/fixtures/files/attachments/file.png') | ||
message.attachments['file.png'].content_id = '<[email protected]>' | ||
end | ||
|
||
it 'converts the message and sends via API' do | ||
expect(deliver!).to eq({ success: true, message_ids: expected_message_ids }) | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we commit code with
rubocop:disasble
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's a good idea to liberate rubocop settings for the entire repo having in mind we are talking about a single method with disables.
Any ideas to simplify the code?