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

Added Mailchimp Email Adapater #45

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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 docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
environment:
- MAILGUN_API_KEY
- MAILGUN_DOMAIN
- MAILCHIMP_API_KEY
- SENDGRID_API_KEY
- FCM_SERVER_KEY
- FCM_SERVER_TO
Expand Down
78 changes: 78 additions & 0 deletions src/Utopia/Messaging/Adapters/Email/Mailchimp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Utopia\Messaging\Adapters\Email;

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

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

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

/**
* Get adapter description.
*
* @return int
*/
public function getMaxMessagesPerRequest(): int
{
return 1000; // Depends based on the pricing plan
}

/**
* {@inheritdoc}
*
* @param Email $message
* @return string
*
* @throws Exception
*/
protected function process(Email $message): string
{
return $this->request(
method: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send',
headers: [
'Content-Type: application/json',
],
body: \json_encode([
'key' => $this->apiKey,
'message' => [
'html' => $message->isHtml() ? $message->getContent() : null,
'text' => $message->isHtml() ? null : $message->getContent(),
'subject' => $message->getSubject(),
'from_email' => $message->getFrom(),
'to' => \array_map(
fn ($to) => ['email' => $to],
$message->getTo()
),
'attachments' => $message->getAttachments() ? \array_map(
fn ($attachement) => [
'type' => $attachement['type'],
'name' => $attachement['name'],
'content' => $attachement['content'],
],
$message->getAttachments()
) : null,
],
]
)
);
}
}
39 changes: 39 additions & 0 deletions tests/e2e/Email/MailchimpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\E2E;

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

class MailchimpTest extends Base
{
/**
* @throws \Exception
*/
public function testSendEmail()
{
$key = getenv('MAILCHIMP_API_KEY');

$sender = new Mailchimp(
apiKey: $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))[0];

$this->assertArrayHasKey('_id', $result);
$this->assertArrayHasKey('status', $result);
$this->assertTrue(str_contains(strtolower($result['status']), 'sent'));
AVDiv marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading