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
28 changes: 28 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@ version: '3.9'

services:
tests:
environment:
- MAILGUN_API_KEY
- MAILGUN_DOMAIN
- POSTMARK_API_KEY
- SENDGRID_API_KEY
- FCM_SERVER_KEY
- FCM_SERVER_TO
- TWILIO_ACCOUNT_SID
- TWILIO_AUTH_TOKEN
- TWILIO_TO
- TWILIO_FROM
- TELNYX_API_KEY
- TELNYX_PUBLIC_KEY
- APNS_AUTHKEY_8KVVCLA3HL
- APNS_AUTH_ID
- APNS_TEAM_ID
- APNS_BUNDLE_ID
- APNS_TO
- MSG_91_SENDER_ID
- MSG_91_AUTH_KEY
- MSG_91_TO
- MSG_91_FROM
- TEST_EMAIL
- TEST_FROM_EMAIL
- VONAGE_API_KEY
- VONAGE_API_SECRET
- VONAGE_TO
- VONAGE_FROM
build:
context: .
volumes:
Expand Down
67 changes: 67 additions & 0 deletions src/Utopia/Messaging/Adapters/Email/Postmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Utopia\Messaging\Adapters\Email;

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

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

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

/**
* Get adapter description.
*
* @see https://postmarkapp.com/developer/api/email-api#send-batch-emails For information about batch email limit.
*
* @return int
*/
public function getMaxMessagesPerRequest(): int
{
return 50;
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(Email $message): string
{
$bodyKey = $message->isHtml() ? 'HtmlBody' : 'TextBody';

$response = $this->request(
method: 'POST',
url: 'https://api.postmarkapp.com/email',
headers: [
'Accept: application/json',
'Content-Type: application/json',
'X-Postmark-Server-Token: '.$this->apiKey,
],
body: \json_encode([
'To' => \implode(',', $message->getTo()),
'From' => $message->getFrom(),
'Subject' => $message->getSubject(),
$bodyKey => $message->getContent(),
]),
);

return $response;
}
}
35 changes: 35 additions & 0 deletions tests/e2e/Email/PostmarkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Tests\E2E;

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

class PostmarkTest extends Base
{
/**
* @throws \Exception
*/
public function testSendPlainTextEmail()
{
$key = getenv('POSTMARK_API_KEY');
$sender = new Postmark($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,
);

$result = (array) \json_decode($sender->send($message));

$this->assertEquals($to, $result['To']);
$this->assertEquals('OK', $result['Message']);
}
}