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 brevo adapter and test suite for the same. #34

Open
wants to merge 1 commit 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
48 changes: 48 additions & 0 deletions src/Utopia/Messaging/Adapters/Email/Brevo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Utopia\Messaging\Adapters\Email;

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

class Brevo extends EmailAdapter
{
public function __construct(private string $apiKey)
{
}

public function getName(): string
{
return 'Brevo';
}

public function getMaxMessagesPerRequest(): int
{
return 1000;
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 link the documentation to support this in the PR description or as a response to this comment?

}

protected function process(Email $message): string
{
$bodyKey = $message->isHtml() ? 'htmlContent' : 'textContent';
return $this->request(
method: 'POST',
url: 'https://api.brevo.com/v3/smtp/email',
headers: [
'accept: application/json',
'Content-Type: application/json',
'api-key: '.$this->apiKey,
],
body: \json_encode([
"sender" => [
"email" => $message->getFrom()
],
"to" => \array_map(
fn ($to) => ['email' => $to],
$message->getTo()
),
"subject" => $message->getSubject(),
$bodyKey => $message->getContent(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we do the same as in Mailgun.php

    'text' => $message->isHtml() ? null : $message->getContent(),
    'html' => $message->isHtml() ? $message->getContent() : null,

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

namespace Tests\E2E;

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

class BrevoTest extends Base
{
/**
* @throws \Exception
*/
public function testSendPlainTextEmail()
{
// $this->markTestSkipped('Brevo credentials not set.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this?


$key = getenv('BREVO_API_KEY');
$sender = new Brevo($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 = $sender->send($message);
$this->assertArrayHasKey('messageId', json_decode($response, true));



Comment on lines +34 to +36
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove these empty lines?

}
}
Loading