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

Add Netcore Email adapter #53

Open
wants to merge 3 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 @@ -40,6 +40,7 @@ jobs:
VONAGE_API_SECRET: ${{ secrets.VONAGE_API_SECRET }}
VONAGE_TO: ${{ secrets.VONAGE_TO }}
VONAGE_FROM: ${{ secrets.VONAGE_FROM }}
NETCORE_API_KEY: ${{ secrets.NETCORE_API_KEY }}
run: |
docker compose up -d --build
sleep 5
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
- VONAGE_API_SECRET
- VONAGE_TO
- VONAGE_FROM
- NETCORE_API_KEY
build:
context: .
volumes:
Expand Down
75 changes: 75 additions & 0 deletions src/Utopia/Messaging/Adapters/Email/Netcore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Utopia\Messaging\Adapters\Email;

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

class Netcore extends EmailAdapter
{
/**
* @param string $apiKey Your Netcore API key to authenticate with the API. (Ref: https://cpaasdocs.netcorecloud.com/docs/pepipost-api/ZG9jOjQyMTU3NjQ0-authentication)
* @param bool $isEU Whether to use the EU domain or not.
*/
public function __construct(
private string $apiKey,
private bool $isEU = false
) {
}

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

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

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(Email $message): string
{
$usDomain = 'emailapi.netcorecloud.net';
$euDomain = 'apieu.netcorecloud.net';

$domain = $this->isEU ? $euDomain : $usDomain;

$body = [
'to' => \implode(',', $message->getTo()),
'from' => $message->getFrom(),
'subject' => $message->getSubject(),
'content' => [
'type' => $message->isHtml() ? 'html' : 'amp',
'value' => $message->getContent(),
],
];

$response = $this->request(
method: 'POST',
url: "https://$domain/v5.1/mail/send",
headers: [
'apiKey: '.base64_encode('api:'.$this->apiKey),
'Accept: application/json',
'Content-Type: application/json',
],
body: \json_encode($body)
);

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

namespace Tests\E2E;

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

class NetcoreTest extends Base
{
/**
* @throws \Exception
*/
public function testSendPlainTextEmail()
{
$this->markTestSkipped('Netcore credentials not set.');

$key = getenv('NETCORE_API_KEY');
$sender = new Netcore(
apiKey: $key,
isEU: false
);

$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->assertArrayHasKey('data', $result);
$this->assertEquals('OK', $result['message']);
$this->assertEquals('success', $result['status']);
}
}