diff --git a/Gemfile.lock b/Gemfile.lock index 2cde30a..322f7e2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -13,7 +13,7 @@ GEM bundler rake thor (>= 0.14.0) - ast (2.4.2) + ast (2.4.3) base64 (0.3.0) bigdecimal (3.1.8) crack (1.0.0) @@ -27,8 +27,9 @@ GEM pp (>= 0.6.0) rdoc (>= 4.0.0) reline (>= 0.4.2) - json (2.7.2) - language_server-protocol (3.17.0.3) + json (2.12.2) + language_server-protocol (3.17.0.5) + lint_roller (1.1.0) mail (2.8.1) mini_mime (>= 0.1.1) net-imap @@ -44,26 +45,27 @@ GEM timeout net-smtp (0.5.0) net-protocol - parallel (1.25.1) - parser (3.3.3.0) + parallel (1.27.0) + parser (3.3.8.0) ast (~> 2.4.1) racc pp (0.6.2) prettyprint prettyprint (0.2.0) + prism (1.4.0) psych (5.2.6) date stringio public_suffix (5.1.1) - racc (1.8.0) + racc (1.8.1) rainbow (3.1.1) rake (13.2.1) rdoc (6.13.1) psych (>= 4.0.0) - regexp_parser (2.9.2) + regexp_parser (2.10.0) reline (0.6.1) io-console (~> 0.5) - rexml (3.3.9) + rexml (3.4.1) rspec (3.13.0) rspec-core (~> 3.13.0) rspec-expectations (~> 3.13.0) @@ -80,19 +82,20 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) rspec-support (3.13.1) - rubocop (1.64.1) + rubocop (1.78.0) json (~> 2.3) - language_server-protocol (>= 3.17.0) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) parallel (~> 1.10) parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 1.8, < 3.0) - rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.31.1, < 2.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.45.1, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.31.3) - parser (>= 3.3.1.0) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.45.1) + parser (>= 3.3.7.2) + prism (~> 1.4) rubocop-rake (0.6.0) rubocop (~> 1.0) rubocop-rspec (3.0.2) @@ -101,7 +104,9 @@ GEM stringio (3.1.7) thor (1.3.1) timeout (0.4.3) - unicode-display_width (2.5.0) + unicode-display_width (3.1.4) + unicode-emoji (~> 4.0, >= 4.0.4) + unicode-emoji (4.0.4) vcr (6.2.0) webmock (3.23.1) addressable (>= 2.8.0) diff --git a/README.md b/README.md index a800919..b769589 100644 --- a/README.md +++ b/README.md @@ -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: 'mailtrap@example.com', name: 'Mailtrap Test' }, to: [ { email: 'your@email.com' } ], reply_to: { email: 'support@example.com', 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: 'mailtrap@example.com', name: 'Mailtrap Test' }, + to: [ + { email: 'your@email.com' } + ], + subject: 'You are awesome!', + text: 'Congrats for sending test email with Mailtrap!' +) + ``` ### Email Templates API diff --git a/examples/email_template.rb b/examples/email_template.rb index 0721b76..ad3333a 100644 --- a/examples/email_template.rb +++ b/examples/email_template.rb @@ -1,7 +1,7 @@ require 'mailtrap' -# create mail object -mail = Mailtrap::Mail::FromTemplate.new( +# Create mail object +mail = Mailtrap::Mail.from_template( from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' }, to: [ { email: 'your@email.com' } @@ -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: 'mailtrap@example.com', name: 'Mailtrap Test' }, + to: [ + { email: 'your@email.com' } + ], + template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2', + template_variables: { + 'user_name' => 'John Doe' + } +) diff --git a/examples/full.rb b/examples/full.rb index 0b1d5c9..57053d6 100644 --- a/examples/full.rb +++ b/examples/full.rb @@ -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: 'mailtrap@example.com', name: 'Mailtrap Test' }, to: [ { email: 'your@email.com', 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: 'mailtrap@example.com', name: 'Mailtrap Test' }, + to: [ + { email: 'your@email.com', name: 'Your name' } + ], + subject: 'You are awesome!', + text: 'Congrats for sending test email with Mailtrap!' +) diff --git a/lib/mailtrap/client.rb b/lib/mailtrap/client.rb index 8b9d9e3..9e4de51 100644 --- a/lib/mailtrap/client.rb +++ b/lib/mailtrap/client.rb @@ -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: 'mailtrap@example.com', name: 'Mailtrap Test' }, + # to: [ + # { email: 'your@email.com' } + # ], + # template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2', + # template_variables: { + # 'user_name' => 'John Doe' + # } + # ) + # client.send(mail) + # @example + # client.send( + # from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' }, + # to: [ + # { email: 'your@email.com' } + # ], + # 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 - 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) diff --git a/lib/mailtrap/mail.rb b/lib/mailtrap/mail.rb index 666ff58..7ecfe55 100644 --- a/lib/mailtrap/mail.rb +++ b/lib/mailtrap/mail.rb @@ -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: 'mailtrap@example.com', name: 'Mailtrap Test' }, + # to: [ + # { email: 'your@email.com' } + # ], + # 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: 'mailtrap@example.com', name: 'Mailtrap Test' }, + # to: [ + # { email: 'your@email.com' } + # ], + # 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) diff --git a/lib/mailtrap/mail/base.rb b/lib/mailtrap/mail/base.rb index 5eff2a9..b0117bf 100644 --- a/lib/mailtrap/mail/base.rb +++ b/lib/mailtrap/mail/base.rb @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/lib/mailtrap/mail/from_template.rb b/lib/mailtrap/mail/from_template.rb index b19bfb6..3bc3a61 100644 --- a/lib/mailtrap/mail/from_template.rb +++ b/lib/mailtrap/mail/from_template.rb @@ -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: [], @@ -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 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_all_params_are_set/sending_is_successful.yml b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_all_params_are_set/sending_is_successful.yml index 03da4d0..4d6ac4e 100644 --- a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_all_params_are_set/sending_is_successful.yml +++ b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_all_params_are_set/sending_is_successful.yml @@ -5,7 +5,7 @@ http_interactions: uri: https://send.api.mailtrap.io/api/send body: encoding: UTF-8 - string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"cc":[],"bcc":[],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"headers":{},"custom_variables":{},"category":"Integration Test"}' + string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"category":"Integration Test"}' headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_api_key_is_incorrect/raises_authorization_error_with_array_of_errors.yml b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_api_key_is_incorrect/raises_authorization_error_with_array_of_errors.yml index c6d4f45..4f60959 100644 --- a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_api_key_is_incorrect/raises_authorization_error_with_array_of_errors.yml +++ b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_api_key_is_incorrect/raises_authorization_error_with_array_of_errors.yml @@ -5,7 +5,7 @@ http_interactions: uri: https://send.api.mailtrap.io/api/send body: encoding: UTF-8 - string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"cc":[],"bcc":[],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"headers":{},"custom_variables":{},"category":"Integration Test"}' + string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"category":"Integration Test"}' headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_no_subject_and_no_text_set/raises_sending_error_with_array_of_errors.yml b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_no_subject_and_no_text_set/raises_sending_error_with_array_of_errors.yml index 1d2d09b..3d99c34 100644 --- a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_no_subject_and_no_text_set/raises_sending_error_with_array_of_errors.yml +++ b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/when_no_subject_and_no_text_set/raises_sending_error_with_array_of_errors.yml @@ -5,7 +5,7 @@ http_interactions: uri: https://send.api.mailtrap.io/api/send body: encoding: UTF-8 - string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"cc":[],"bcc":[],"attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"headers":{},"custom_variables":{},"category":"Integration Test"}' + string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"category":"Integration Test"}' headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_an_alternative_host/sending_is_successful.yml b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_an_alternative_host/sending_is_successful.yml index e23e603..0d27c1b 100644 --- a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_an_alternative_host/sending_is_successful.yml +++ b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_an_alternative_host/sending_is_successful.yml @@ -5,7 +5,7 @@ http_interactions: uri: https://alternative.host.mailtrap.io:8080/api/send body: encoding: UTF-8 - string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"cc":[],"bcc":[],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"headers":{},"custom_variables":{},"category":"Integration Test"}' + string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"category":"Integration Test"}' headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_bulk_flag/chooses_host_for_bulk_sending.yml b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_bulk_flag/chooses_host_for_bulk_sending.yml index 39d7510..abcd255 100644 --- a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_bulk_flag/chooses_host_for_bulk_sending.yml +++ b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_bulk_flag/chooses_host_for_bulk_sending.yml @@ -5,7 +5,7 @@ http_interactions: uri: https://bulk.api.mailtrap.io/api/send body: encoding: UTF-8 - string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"cc":[],"bcc":[],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"headers":{},"custom_variables":{},"category":"Integration Test"}' + string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"category":"Integration Test"}' headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_bulk_flag_and_alternative_host/chooses_alternative_host.yml b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_bulk_flag_and_alternative_host/chooses_alternative_host.yml index c2d4360..5370b68 100644 --- a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_bulk_flag_and_alternative_host/chooses_alternative_host.yml +++ b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_bulk_flag_and_alternative_host/chooses_alternative_host.yml @@ -5,7 +5,7 @@ http_interactions: uri: https://alternative.host.mailtrap.io:8080/api/send body: encoding: UTF-8 - string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"cc":[],"bcc":[],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"headers":{},"custom_variables":{},"category":"Integration Test"}' + string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"category":"Integration Test"}' headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_sandbox_flag/chooses_host_for_sandbox_sending.yml b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_sandbox_flag/chooses_host_for_sandbox_sending.yml index 86ea04f..331703d 100644 --- a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_sandbox_flag/chooses_host_for_sandbox_sending.yml +++ b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail/with_sandbox_flag/chooses_host_for_sandbox_sending.yml @@ -5,7 +5,7 @@ http_interactions: uri: https://sandbox.api.mailtrap.io:443/api/send/12 body: encoding: UTF-8 - string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"cc":[],"bcc":[],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"headers":{},"custom_variables":{},"category":"Integration Test"}' + string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"category":"Integration Test"}' headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail_is_hash/sends_an_email.yml b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail_is_hash/sends_an_email.yml new file mode 100644 index 0000000..10b082c --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail_is_hash/sends_an_email.yml @@ -0,0 +1,37 @@ +--- +http_interactions: + - request: + method: post + uri: https://send.api.mailtrap.io/api/send + body: + encoding: UTF-8 + string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"subject":"You are awesome!","text":"Congrats for sending test email with Mailtrap!","category":"Integration Test","attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}]}' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - mailtrap-ruby (https://github.com/railsware/mailtrap-ruby) + Authorization: + - Bearer + Content-Type: + - application/json + response: + status: + code: 200 + message: OK + headers: + Date: + - Tue, 31 May 2022 15:49:47 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '71' + Connection: + - keep-alive + body: + encoding: UTF-8 + string: '{"success":true,"message_ids":["4c2446b6-e0f9-11ec-9487-0a58a9feac02"]}' + recorded_at: Tue, 31 May 2022 15:49:47 GMT +recorded_with: VCR 6.1.0 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_all_params_are_set/sending_is_successful.yml b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_all_params_are_set/sending_is_successful.yml index 5de3411..7e64b65 100644 --- a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_all_params_are_set/sending_is_successful.yml +++ b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_all_params_are_set/sending_is_successful.yml @@ -5,7 +5,7 @@ http_interactions: uri: https://send.api.mailtrap.io/api/send body: encoding: UTF-8 - string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"cc":[],"bcc":[],"attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"headers":{},"custom_variables":{},"template_uuid":"aeb1ec59-2737-4a1d-9c95-0baf3be49d74","template_variables":{"user_name":"John Doe"}}' + string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"template_uuid":"aeb1ec59-2737-4a1d-9c95-0baf3be49d74","template_variables":{"user_name":"John Doe"}}' headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_api_key_is_incorrect/raises_authorization_error_with_array_of_errors.yml b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_api_key_is_incorrect/raises_authorization_error_with_array_of_errors.yml index 76b1734..b172a82 100644 --- a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_api_key_is_incorrect/raises_authorization_error_with_array_of_errors.yml +++ b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_api_key_is_incorrect/raises_authorization_error_with_array_of_errors.yml @@ -5,7 +5,7 @@ http_interactions: uri: https://send.api.mailtrap.io/api/send body: encoding: UTF-8 - string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"cc":[],"bcc":[],"attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"headers":{},"custom_variables":{},"template_uuid":"aeb1ec59-2737-4a1d-9c95-0baf3be49d74","template_variables":{"user_name":"John Doe"}}' + string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"template_uuid":"aeb1ec59-2737-4a1d-9c95-0baf3be49d74","template_variables":{"user_name":"John Doe"}}' headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 diff --git a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_using_sandbox/sending_is_successful.yml b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_using_sandbox/sending_is_successful.yml index 51967e0..3382476 100644 --- a/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_using_sandbox/sending_is_successful.yml +++ b/spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_template/when_using_sandbox/sending_is_successful.yml @@ -5,7 +5,7 @@ http_interactions: uri: https://sandbox.api.mailtrap.io/api/send/13 body: encoding: UTF-8 - string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"cc":[],"bcc":[],"attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"headers":{},"custom_variables":{},"template_uuid":"aeb1ec59-2737-4a1d-9c95-0baf3be49d74","template_variables":{"user_name":"John Doe"}}' + string: '{"from":{"email":"mailtrap@mailtrap.io","name":"Mailtrap Test"},"to":[{"email":"mailtrap@railsware.com"}],"attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],"template_uuid":"aeb1ec59-2737-4a1d-9c95-0baf3be49d74","template_variables":{"user_name":"John Doe"}}' headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 diff --git a/spec/mailtrap/client_spec.rb b/spec/mailtrap/client_spec.rb index 6ae1855..cd8375a 100644 --- a/spec/mailtrap/client_spec.rb +++ b/spec/mailtrap/client_spec.rb @@ -57,12 +57,6 @@ end end - context 'when mail object is not a Mailtrap::Mail::Base' do - let(:mail) { 'it-a-string' } - - it { expect { send }.to raise_error(ArgumentError, 'should be Mailtrap::Mail::Base object') } - end - context 'with an alternative host' do let(:client) do described_class.new(api_key:, api_host: 'alternative.host.mailtrap.io', api_port: 8080) @@ -120,6 +114,27 @@ end end + context 'when mail is hash' do + let(:mail) do + { + from: { email: 'mailtrap@mailtrap.io', name: 'Mailtrap Test' }, + to: [ + { email: 'mailtrap@railsware.com' } + ], + subject: 'You are awesome!', + text: 'Congrats for sending test email with Mailtrap!', + category: 'Integration Test', + attachments: [ + { content: Base64.strict_encode64('hello world'), filename: 'attachment.txt' } + ] + } + end + + it 'sends an email' do + expect(send).to eq({ message_ids: ['4c2446b6-e0f9-11ec-9487-0a58a9feac02'], success: true }) + end + end + context 'when template' do let(:mail) do Mailtrap::Mail::FromTemplate.new( diff --git a/spec/mailtrap/mail/base_spec.rb b/spec/mailtrap/mail/base_spec.rb index bc2ed24..00d0c51 100644 --- a/spec/mailtrap/mail/base_spec.rb +++ b/spec/mailtrap/mail/base_spec.rb @@ -67,13 +67,8 @@ { 'from' => { email: 'test@example.com', name: 'Mailtrap User' }, 'to' => [{ email: 'to@example.com' }, { email: 'to2@example.com', name: 'To Two' }], - 'cc' => [], - 'bcc' => [], 'subject' => 'This is subject', - 'text' => 'This is text', - 'headers' => {}, - 'attachments' => [], - 'custom_variables' => {} + 'text' => 'This is text' } end @@ -139,7 +134,6 @@ '"html":"
Test HTML
",' \ '"attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],' \ '"headers":{"Category-Header":"some_category"},' \ - '"custom_variables":{},' \ '"category":"another_category"' \ '}' end diff --git a/spec/mailtrap/mail/from_template_spec.rb b/spec/mailtrap/mail/from_template_spec.rb index 46a5b38..ef7dc72 100644 --- a/spec/mailtrap/mail/from_template_spec.rb +++ b/spec/mailtrap/mail/from_template_spec.rb @@ -96,13 +96,7 @@ let(:expected_hash) do { 'from' => { email: 'test@example.com', name: 'Mailtrap User' }, - 'to' => [{ email: 'to@example.com' }, { email: 'to2@example.com', name: 'To Two' }], - 'cc' => [], - 'bcc' => [], - 'headers' => {}, - 'attachments' => [], - 'custom_variables' => {}, - 'template_variables' => {} + 'to' => [{ email: 'to@example.com' }, { email: 'to2@example.com', name: 'To Two' }] } end @@ -133,7 +127,6 @@ '"bcc":[{"email":"bcc@example.com"}],' \ '"attachments":[{"content":"aGVsbG8gd29ybGQ=","filename":"attachment.txt"}],' \ '"headers":{"Category-Header":"some_category"},' \ - '"custom_variables":{},' \ '"template_uuid":"7a58f94d-d282-4538-ae50-4714eafef80e",' \ '"template_variables":{"user.name":"Jack Daniels"}' \ '}'