Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ 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]' }
Expand All @@ -57,9 +57,20 @@ mail = Mailtrap::Mail::Base.new(
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
Expand Down
18 changes: 15 additions & 3 deletions examples/email_template.rb
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]' }
Expand All @@ -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'
}
)
16 changes: 15 additions & 1 deletion examples/full.rb
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' }
Expand Down Expand Up @@ -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!'
)
7 changes: 2 additions & 5 deletions lib/mailtrap/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,11 @@ def initialize( # rubocop:disable Metrics/ParameterLists
end

# Sends an email
# @param mail [Mail::Base] The email to send
# @return [Hash, nil] The JSON response
# @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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its ruby 🦆


perform_request(:post, api_host, send_path, mail)
end

Expand Down
64 changes: 63 additions & 1 deletion lib/mailtrap/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,70 @@
require_relative 'errors'

module Mailtrap
module Mail
module Mail # rubocop:disable Metrics/ModuleLength
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.
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.
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
Expand Down
19 changes: 15 additions & 4 deletions lib/mailtrap/mail/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
module Mailtrap
module Mail
class Base
attr_accessor :from, :to, :reply_to, :cc, :bcc, :headers, :custom_variables, :subject, :text, :html, :category
attr_accessor :from, :to, :reply_to, :cc, :bcc, :headers, :custom_variables, :subject, :text, :html, :category,
:template_uuid, :template_variables
attr_reader :attachments

def initialize( # rubocop:disable Metrics/ParameterLists
Expand All @@ -20,7 +21,9 @@ def initialize( # rubocop:disable Metrics/ParameterLists
attachments: [],
headers: {},
custom_variables: {},
category: nil
category: nil,
template_uuid: nil,
template_variables: {}
)
@from = from
@to = to
Expand All @@ -34,6 +37,8 @@ def initialize( # rubocop:disable Metrics/ParameterLists
@headers = headers
@custom_variables = custom_variables
@category = category
@template_uuid = template_uuid
@template_variables = template_variables
end

def as_json
Expand All @@ -50,8 +55,10 @@ def as_json
# TODO: update headers and custom_variables with as_json method
'headers' => headers,
'custom_variables' => custom_variables,
'category' => category
}.compact
'category' => category,
'template_uuid' => template_uuid,
'template_variables' => template_variables
}.transform_values { |value| presence value }.compact
end

def to_json(*args)
Expand All @@ -77,6 +84,10 @@ def add_attachment(content:, filename:, type: nil, disposition: nil, content_id:

attachment
end

def presence(value)
value.respond_to?(:empty?) && value.empty? ? nil : value
end
end
end
end
25 changes: 2 additions & 23 deletions lib/mailtrap/mail/from_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

module Mailtrap
module Mail
# @deprecated Use Mailtrap::Mail::Base
class FromTemplate < Base
attr_accessor :template_uuid, :template_variables

def initialize( # rubocop:disable Metrics/ParameterLists
from: nil,
to: [],
Expand All @@ -17,27 +16,7 @@ def initialize( # rubocop:disable Metrics/ParameterLists
template_uuid: nil,
template_variables: {}
)
super(
from:,
to:,
reply_to:,
cc:,
bcc:,
attachments:,
headers:,
custom_variables:
)
@template_uuid = template_uuid
@template_variables = template_variables
end

def as_json
super.merge(
{
'template_uuid' => template_uuid,
'template_variables' => template_variables
}
).compact
super
end
end
end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading