-
Notifications
You must be signed in to change notification settings - Fork 5
Deprecate Mailtrap::Mail::FromTemplate #54
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
Merged
+288
−114
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a46ffb
Deprecate Mailtrap::Mail::FromTemplate
i7an 2ed6682
Add builder methods from_content and from_template
i7an 2271774
Remove empty values from request
i7an 65b0250
Update examples and readme
i7an 15f1a0a
Fix rubocop comma offenses
i7an 55817b1
Add examples
i7an 8faa471
Update rubocop
i7an 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -46,20 +46,31 @@ config.action_mailer.delivery_method = :mailtrap | |
```ruby | ||
require 'mailtrap' | ||
|
||
# create mail object | ||
mail = Mailtrap::Mail::Base.new( | ||
# Create mail object | ||
mail = Mailtrap::Mail.from_content( | ||
from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
to: [ | ||
{ email: '[email protected]' } | ||
], | ||
reply_to: { email: '[email protected]', name: 'Mailtrap Reply-To' }, | ||
subject: 'You are awesome!', | ||
text: "Congrats for sending test email with Mailtrap!" | ||
text: 'Congrats for sending test email with Mailtrap!' | ||
) | ||
|
||
# create client and send | ||
# Create client and send | ||
client = Mailtrap::Client.new(api_key: 'your-api-key') | ||
client.send(mail) | ||
|
||
# You can also pass the request parameters directly | ||
client.send( | ||
from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
to: [ | ||
{ email: '[email protected]' } | ||
], | ||
subject: 'You are awesome!', | ||
text: 'Congrats for sending test email with Mailtrap!' | ||
) | ||
|
||
``` | ||
|
||
### Email Templates API | ||
|
This file contains hidden or 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,7 +1,7 @@ | ||
require 'mailtrap' | ||
|
||
# create mail object | ||
mail = Mailtrap::Mail::FromTemplate.new( | ||
# Create mail object | ||
mail = Mailtrap::Mail.from_template( | ||
from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
to: [ | ||
{ email: '[email protected]' } | ||
|
@@ -13,6 +13,18 @@ | |
} | ||
) | ||
|
||
# create client and send | ||
# Create client and send | ||
client = Mailtrap::Client.new(api_key: 'your-api-key') | ||
client.send(mail) | ||
|
||
# You can also pass the request parameters directly | ||
client.send( | ||
from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
to: [ | ||
{ email: '[email protected]' } | ||
], | ||
template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2', | ||
template_variables: { | ||
'user_name' => 'John Doe' | ||
} | ||
) |
This file contains hidden or 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,7 +1,11 @@ | ||
require 'mailtrap' | ||
require 'base64' | ||
|
||
mail = Mailtrap::Mail::Base.new( | ||
# You can create a mail object using one of the following: | ||
# - Mailtrap::Mail.from_content | ||
# - Mailtrap::Mail.from_template | ||
# - Mailtrap::Mail::Base.new | ||
mail = Mailtrap::Mail.from_content( | ||
from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
to: [ | ||
{ email: '[email protected]', name: 'Your name' } | ||
|
@@ -52,3 +56,13 @@ | |
# client = Mailtrap::Client.new(api_key: 'your-api-key', sandbox: true, inbox_id: 12) | ||
|
||
client.send(mail) | ||
|
||
# You can also pass the request parameters directly | ||
client.send( | ||
from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
to: [ | ||
{ email: '[email protected]', name: 'Your name' } | ||
], | ||
subject: 'You are awesome!', | ||
text: 'Congrats for sending test email with Mailtrap!' | ||
) |
This file contains hidden or 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 |
---|---|---|
|
@@ -54,14 +54,32 @@ def initialize( # rubocop:disable Metrics/ParameterLists | |
end | ||
|
||
# Sends an email | ||
# @param mail [Mail::Base] The email to send | ||
# @return [Hash, nil] The JSON response | ||
# @example | ||
# mail = Mailtrap::Mail.from_template( | ||
# from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
# to: [ | ||
# { email: '[email protected]' } | ||
# ], | ||
# template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2', | ||
# template_variables: { | ||
# 'user_name' => 'John Doe' | ||
# } | ||
# ) | ||
# client.send(mail) | ||
# @example | ||
# client.send( | ||
# from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
# to: [ | ||
# { email: '[email protected]' } | ||
# ], | ||
# subject: 'You are awesome!', | ||
# text: 'Congrats for sending test email with Mailtrap!' | ||
# ) | ||
# @param mail [#to_json] The email to send | ||
# @return [Hash] The JSON response | ||
# @!macro api_errors | ||
# @raise [Mailtrap::MailSizeError] If the message is too large | ||
# @raise [ArgumentError] If the mail is not a Mail::Base object | ||
def send(mail) | ||
raise ArgumentError, 'should be Mailtrap::Mail::Base object' unless mail.is_a? Mail::Base | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its ruby 🦆 |
||
|
||
perform_request(:post, api_host, send_path, mail) | ||
end | ||
|
||
|
@@ -118,7 +136,7 @@ def select_api_host(bulk:, sandbox:) | |
end | ||
|
||
def send_path | ||
"/api/send#{sandbox ? "/#{inbox_id}" : ""}" | ||
"/api/send#{"/#{inbox_id}" if sandbox}" | ||
end | ||
|
||
def perform_request(method, host, path, body = nil) | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -7,8 +7,117 @@ | |
require_relative 'errors' | ||
|
||
module Mailtrap | ||
module Mail | ||
module Mail # rubocop:disable Metrics/ModuleLength | ||
SPECIAL_HEADERS = %w[ | ||
from | ||
to | ||
cc | ||
bcc | ||
subject | ||
category | ||
customvariables | ||
contenttype | ||
].freeze | ||
private_constant :SPECIAL_HEADERS | ||
|
||
# ActionMailer adds these headers by calling `Mail::Message#encoded`, | ||
# as if the message is to be delivered via SMTP. | ||
# Since the message will actually be generated on the Mailtrap side from its components, | ||
# the headers are redundant and potentially conflicting, so we remove them. | ||
ACTIONMAILER_ADDED_HEADERS = %w[ | ||
contenttransferencoding | ||
date | ||
messageid | ||
mimeversion | ||
].freeze | ||
private_constant :ACTIONMAILER_ADDED_HEADERS | ||
|
||
HEADERS_TO_REMOVE = (SPECIAL_HEADERS + ACTIONMAILER_ADDED_HEADERS).freeze | ||
private_constant :HEADERS_TO_REMOVE | ||
|
||
class << self | ||
# Builds a mail object that will be sent using a pre-defined email | ||
# template. The template content (subject, text, html, category) is | ||
# defined in the Mailtrap dashboard and referenced by the template_uuid. | ||
# Template variables can be passed to customize the template content. | ||
# @example | ||
# mail = Mailtrap::Mail.from_template( | ||
# from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
# to: [ | ||
# { email: '[email protected]' } | ||
# ], | ||
# template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2', | ||
# template_variables: { | ||
# 'user_name' => 'John Doe' | ||
# } | ||
# ) | ||
def from_template( # rubocop:disable Metrics/ParameterLists | ||
from: nil, | ||
to: [], | ||
reply_to: nil, | ||
cc: [], | ||
bcc: [], | ||
attachments: [], | ||
headers: {}, | ||
custom_variables: {}, | ||
template_uuid: nil, | ||
template_variables: {} | ||
) | ||
Mailtrap::Mail::Base.new( | ||
from:, | ||
to:, | ||
reply_to:, | ||
cc:, | ||
bcc:, | ||
attachments:, | ||
headers:, | ||
custom_variables:, | ||
template_uuid:, | ||
template_variables: | ||
) | ||
end | ||
|
||
# Builds a mail object with content including subject, text, html, and category. | ||
# @example | ||
# mail = Mailtrap::Mail.from_content( | ||
# from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
# to: [ | ||
# { email: '[email protected]' } | ||
# ], | ||
# subject: 'You are awesome!', | ||
# text: 'Congrats for sending test email with Mailtrap!' | ||
# ) | ||
def from_content( # rubocop:disable Metrics/ParameterLists | ||
from: nil, | ||
to: [], | ||
reply_to: nil, | ||
cc: [], | ||
bcc: [], | ||
attachments: [], | ||
headers: {}, | ||
custom_variables: {}, | ||
subject: nil, | ||
text: nil, | ||
html: nil, | ||
category: nil | ||
) | ||
Mailtrap::Mail::Base.new( | ||
from:, | ||
to:, | ||
reply_to:, | ||
cc:, | ||
bcc:, | ||
attachments:, | ||
headers:, | ||
custom_variables:, | ||
subject:, | ||
text:, | ||
html:, | ||
category: | ||
) | ||
end | ||
|
||
# Builds a mail object from Mail::Message | ||
# @param message [Mail::Message] | ||
# @return [Mailtrap::Mail::Base] | ||
def from_message(message) # rubocop:disable Metrics/AbcSize | ||
|
@@ -29,30 +138,6 @@ def from_message(message) # rubocop:disable Metrics/AbcSize | |
|
||
private | ||
|
||
SPECIAL_HEADERS = %w[ | ||
from | ||
to | ||
cc | ||
bcc | ||
subject | ||
category | ||
customvariables | ||
contenttype | ||
].freeze | ||
|
||
# ActionMailer adds these headers by calling `Mail::Message#encoded`, | ||
# as if the message is to be delivered via SMTP. | ||
# Since the message will actually be generated on the Mailtrap side from its components, | ||
# the headers are redundant and potentially conflicting, so we remove them. | ||
ACTIONMAILER_ADDED_HEADERS = %w[ | ||
contenttransferencoding | ||
date | ||
messageid | ||
mimeversion | ||
].freeze | ||
|
||
HEADERS_TO_REMOVE = (SPECIAL_HEADERS + ACTIONMAILER_ADDED_HEADERS).freeze | ||
|
||
# @param header [Mail::Field, nil] | ||
# @return [Mail::AddressList, nil] | ||
def address_list(header) | ||
|
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.
Other name could be
from_hash
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 wanted to highlight the fact that mail object is instantiated with text and html in contrast with from_template where template_uuid is provided.