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
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

namespace Utopia\Messaging\Adapters\Email;

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

class Mailjet extends EmailAdapter
{
/**
* @param string $apiKey Your Mailgun API key to authenticate with the API.
* @param string $apiSecret Your Mailgun domain to send messages from.
*/
public function __construct(
private string $apiKey,
private string $apiSecret,
) {
}

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

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

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(Email $message): string
{
$toEmail = [];
foreach ($message->getTo() as $emails) {
foreach ($emails as $email) {
$toEmail[] = [
"Email" => $email,
"Name" => $email
];
}
}
$apiKey = $this->apiKey;
$apiSecret = $this->apiSecret;

$body = [
'Messages' => [
[
'From' => [
'Email' => $message->getFrom(),
'Name' => $message->getFrom()
],
'To' => $toEmail,
'Subject' => $message->getSubject(),
'TextPart' => $message->isHtml() ? null : $message->getContent(),
'HTMLPart' => $message->isHtml() ? $message->getContent() : null
]
]
];

return $this->request(
method: 'POST',
url: 'https://api.mailjet.com/v3.1/send',
headers: [
'Content-Type: application/json',
'Authorization: Basic ' . base64_encode("$apiKey:$apiSecret")
],
body: \json_encode($body)
);
}
}
39 changes: 39 additions & 0 deletions tests/e2e/Email/MailjetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\E2E;

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

class MailjetTest extends Base
{
/**
* @throws \Exception
*/
public function testSendEmail()
{
$apiKey = getenv('MAILJET_API_KEY');
$apiSecret = getenv('MAILJET_API_SECRET');

$sender = new Mailjet(
apiKey: $apiKey,
apiSecret: $apiSecret,
);

$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('success', $result['Messages'][0]->Status);
}
}