Skip to content

Added Elastic Email Adapter #46

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

Open
wants to merge 8 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
3 changes: 2 additions & 1 deletion 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
- ELASTICEMAIL_API_KEY
- FCM_SERVER_KEY
- FCM_SERVER_TO
- TWILIO_ACCOUNT_SID
Expand Down Expand Up @@ -44,4 +45,4 @@ services:
request-catcher:
image: appwrite/requestcatcher:1.0.0
ports:
- '10001:5000'
- '10001:5000'
71 changes: 71 additions & 0 deletions src/Utopia/Messaging/Adapters/Email/ElasticEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Utopia\Messaging\Adapters\Email;

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

class ElasticEmail extends EmailAdapter
{
/**
* @param string $apiKey Your ElasticEmail API key to authenticate with the API.
* @param string $domain Your ElasticEmail domain to send messages from.
*/
public function __construct(
private string $apiKey,
private string $domain,
private bool $isEU = false
) {
}

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

/**
* Get adapter description.
*
* @return int
*/
public function getMaxMessagesPerRequest(): int
{
return 1000;
}

/**
* {@inheritdoc}
*
* @param Email $message
* @return string
*
* @throws \Exception
*/
protected function process(Email $message): string
{
$domain = 'api.ElasticEmail.com';

$response = $this->request(
method: 'POST',
url: "https://$domain/v4/{$this->domain}/send",
headers: [
'Authorization: Basic '.base64_encode('api:'.$this->apiKey),
],
body: http_build_query([
'apiKey' => $this->apiKey,
'from' => $message->getFrom(),
'fromName' => $message->getFromName(),
'subject' => $message->getSubject(),
'bodyHtml' => $message->isHtml() ? $message->getContent() : null,
'bodyText' => $message->isHtml() ? null : $message->getContent(),
'to' => implode(',', $message->getTo()), ]),
);

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

namespace Tests\E2E;

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

class ElasticEmailTest extends Base
{
/**
* @throws \Exception
*/
public function testSendEmail()
{
$key = getenv('ElasticEmail_API_KEY');
Copy link
Contributor

Choose a reason for hiding this comment

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

capitalize keys


$sender = new ElasticEmail(
apiKey: $key,

);

$to = getenv('TEST_EMAIL');
$subject = 'Test Subject';
$content = 'Test Content';
$from = 'sender@'.$domain;

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

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

$this->assertArrayHasKey('transactionId', $result);
$this->assertArrayHasKey('message', $result);
$this->assertTrue(str_contains(strtolower($result['message']), 'queued'));
}
}