Skip to content
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

feat: added Mandrill email adapter #60

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
MAILGUN_API_KEY: ${{ secrets.MAILGUN_API_KEY }}
MAILGUN_DOMAIN: ${{ secrets.MAILGUN_DOMAIN }}
SENDGRID_API_KEY: ${{ secrets.SENDGRID_API_KEY }}
MANDRILL_API_KEY: ${{ secrets.MANDRILL_API_KEY }}
FCM_SERVER_KEY: ${{ secrets.FCM_SERVER_KEY }}
FCM_SERVER_TO: ${{ secrets.FCM_SERVER_TO }}
TWILIO_ACCOUNT_SID: ${{ secrets.TWILIO_ACCOUNT_SID }}
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
- MAILGUN_API_KEY
- MAILGUN_DOMAIN
- SENDGRID_API_KEY
- MANDRILL_API_KEY
- FCM_SERVER_KEY
- FCM_SERVER_TO
- TWILIO_ACCOUNT_SID
Expand Down
69 changes: 69 additions & 0 deletions src/Utopia/Messaging/Adapters/Email/Mandrill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Utopia\Messaging\Adapters\Email;

use Utopia\Messaging\Adapters\Email as EmailAdapter;
use Utopia\Messaging\Messages\Email;

class Mandrill extends EmailAdapter
{
/**
* @param string $apiKey Your Mandrill API key to authenticate with the API.
* @return void
*/
public function __construct(private string $apiKey)
{
}

/**
* Get adapter name.
*
* @return string
*/
public function getName(): string
{
return 'Mandrill';
}

/**
* Get max messages per request.
*
* @return int
*/
public function getMaxMessagesPerRequest(): int
{
return 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a link to the documentation to support this?

}

/**
* {@inheritdoc}
*
* @param Email $message
* @return string
*
* @throws Exception
*/
protected function process(Email $message): string
{
return $this->request(
method: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send',
headers: [
'Content-Type: application/json',
],
body: \json_encode([
"key" => $this->apiKey,
"message" => [
"subject" => $message->getSubject(),
"html" => $message->isHTML() ? $message->getContent() : null,
"text" => $message->isHTML() ? null : $message->getContent(),
"from_email" => $message->getFrom(),
"to" => \array_map(
fn ($to) => ['email' => $to],
$message->getTo()
),
Copy link
Contributor

Choose a reason for hiding this comment

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

This is probably your linter problem

]
]),
);
}
}
38 changes: 38 additions & 0 deletions tests/e2e/Email/MandrillTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Tests\E2E;

use Utopia\Messaging\Adapters\Email\Mandrill;
use Utopia\Messaging\Messages\Email;

class MandrillTest extends Base
{
/**
* @throws \Exception
*/
public function testSendEmail()
{
$key = getenv('MANDRILL_API_KEY');
$sender = new Mandrill($key);

$to = getenv('TEST_EMAIL');
$subject = 'Test Subject';
$content = 'Test Content';
$from = getenv('TEST_FROM_EMAIL');

$message = new Email(
to: [$to],
from: $from,
subject: $subject,
content: $content,
);

$response =json_decode($sender->send($message))[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$response =json_decode($sender->send($message))[0];
$response = json_decode($sender->send($message))[0];


$this->assertArrayHasKey('_id',$response);
$this->assertArrayHasKey('email',$response);
$this->assertArrayHasKey('status',$response);
$this->assertArrayHasKey('reject_reason',$response);
$this->assertArrayHasKey('queued_reason',$response);
}
}