Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ $messaging->send($message);
- [ ] [Postmark](https://postmarkapp.com/)
- [ ] [SparkPost](https://www.sparkpost.com/)
- [ ] [SendinBlue](https://www.sendinblue.com/)
- [ ] [MailSlurp](https://www.mailslurp.com/)
- [x] [MailSlurp](https://www.mailslurp.com/)
- [ ] [ElasticEmail](https://elasticemail.com/)
- [ ] [SES](https://aws.amazon.com/ses/)

Expand Down
68 changes: 68 additions & 0 deletions src/Utopia/Messaging/Adapters/Email/MailSlurp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Utopia\Messaging\Adapters\Email;

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

class MailSlurp extends EmailAdapter
{
/**
* @param string $apiKey Your MailSlurp API key to authenticate with the API.
* @param ?string $inboxId Your MailSlurp inbox ID to send emails from. If left empty, a random inbox will be used.
*/
public function __construct(
private string $apiKey,
private ?string $inboxId = null,
) {
}

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

/**
* Get adapter description.
*
* @return int
*/
public function getMaxMessagesPerRequest(): int
{
// TODO: Find out the actual limit. The docs say "limit varies from plan to plan" without exact numbers.
return 1000;
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(Email $message): string
{
$url = "https://api.mailslurp.com/emails";
if ($this->inboxId !== null) {
$url .= '?inboxId='.$this->inboxId;
}
return $this->request(
method: 'POST',
url: $url,
headers: [
'x-api-key: '.$this->apiKey,
'Content-Type: application/json',
],
body: \json_encode([
'to' => $message->getTo(),
'from' => $message->getFrom(),
'subject' => $message->getSubject(),
'text' => $message->getContent(),
'html' => $message->isHtml(),
]),
);
}
}
34 changes: 34 additions & 0 deletions tests/e2e/Email/MailSlurpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tests\E2E;

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

class MailSlurpTest extends Base
{
/**
* @throws \Exception
*/
public function testSendEmail()
{
$key = getenv('MAILSLURP_API_KEY');
$inboxId = getenv('MAILSLURP_INBOX_ID');
$sender = new MailSlurp($key, empty($inboxId) ? null : $inboxId);

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

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

$response = $sender->send($message);
$this->assertEquals($response, '');
}
}