Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ Courrier supports these transactional email providers:
- [Cloudflare Email Service](https://developers.cloudflare.com/email-service/)
- [Lettermint](https://lettermint.co)
- [Loops](https://loops.so)
- [MailerSend](https://www.mailersend.com/)
- [Mailgun](https://mailgun.com)
- [MailPace](https://mailpace.com)
- [Postmark](https://postmarkapp.com)
Expand Down
2 changes: 2 additions & 0 deletions lib/courrier/email/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "courrier/email/providers/lettermint"
require "courrier/email/providers/logger"
require "courrier/email/providers/loops"
require "courrier/email/providers/mailersend"
require "courrier/email/providers/mailgun"
require "courrier/email/providers/mailjet"
require "courrier/email/providers/mailpace"
Expand All @@ -26,6 +27,7 @@ class Provider
logger: Courrier::Email::Providers::Logger,
lettermint: Courrier::Email::Providers::Lettermint,
loops: Courrier::Email::Providers::Loops,
mailersend: Courrier::Email::Providers::Mailersend,
mailgun: Courrier::Email::Providers::Mailgun,
mailjet: Courrier::Email::Providers::Mailjet,
mailpace: Courrier::Email::Providers::Mailpace,
Expand Down
36 changes: 36 additions & 0 deletions lib/courrier/email/providers/mailersend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module Courrier
class Email
module Providers
class Mailersend < Base
ENDPOINT_URL = "https://api.mailersend.com/v1/email"

def body
{
"from" => email_address(@options.from),
"to" => email_addresses(@options.to),
"cc" => email_addresses(@options.cc),
"bcc" => email_addresses(@options.bcc),
"reply_to" => email_address(@options.reply_to),
"subject" => @options.subject,
"text" => @options.text,
"html" => @options.html
}.compact
end

private

def default_headers = {"Authorization" => "Bearer #{@api_key}"}

def email_addresses(addresses)
addresses&.split(",")&.map { |address| email_address(address) }
end

def email_address(address)
{"email" => address.strip} if address
end
end
end
end
end
52 changes: 52 additions & 0 deletions test/courrier/email/providers/mailersend_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require "test_helper"

module Courrier::Email::Providers
class MailersendTest < Minitest::Test
def setup
email = TestEmail.new(
from: "devs@railsdesigner.com",
to: "first@example.com, second@example.com",
reply_to: "support@railsdesigner.com",
cc: "copy@example.com",
bcc: "archive@example.com"
)

@provider = Mailersend.new(api_key: "test_key", options: email.options)
end

def test_formats_transactional_email
assert_equal(
{
"from" => {"email" => "devs@railsdesigner.com"},
"to" => [{"email" => "first@example.com"}, {"email" => "second@example.com"}],
"cc" => [{"email" => "copy@example.com"}],
"bcc" => [{"email" => "archive@example.com"}],
"reply_to" => {"email" => "support@railsdesigner.com"},
"subject" => "Test Subject",
"text" => "Test Body",
"html" => "<p>Test HTML Body</p>"
},
@provider.body
)
end

def test_authenticates_with_api_key
assert_equal({"Authorization" => "Bearer test_key"}, @provider.send(:default_headers))
end

def test_is_available_through_provider_registry
mock_provider = Minitest::Mock.new
mock_provider.expect(:deliver, nil)

Mailersend.stub :new, mock_provider do
Courrier::Email::Provider.new(
provider: "mailersend",
api_key: "test_key",
options: @provider.instance_variable_get(:@options)
).deliver
end

mock_provider.verify
end
end
end