From 98cbdca01567266dd6ae5d1de34c20593f58a294 Mon Sep 17 00:00:00 2001 From: Avin Divakara Date: Sun, 8 Oct 2023 07:39:40 +0530 Subject: [PATCH 1/6] Added Mailchimp adapter for transactional emails --- .../Messaging/Adapters/Email/Mailchimp.php | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/Utopia/Messaging/Adapters/Email/Mailchimp.php diff --git a/src/Utopia/Messaging/Adapters/Email/Mailchimp.php b/src/Utopia/Messaging/Adapters/Email/Mailchimp.php new file mode 100644 index 00000000..cb8bfc46 --- /dev/null +++ b/src/Utopia/Messaging/Adapters/Email/Mailchimp.php @@ -0,0 +1,83 @@ +request( + method: 'POST', + url: 'https://mandrillapp.com/api/1.0/messages/send', + headers: [ + 'Content-Type: application/json', + ], + body: \json_encode(array( + '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, + ], + ) + ) + ); + } +} + +?> \ No newline at end of file From 7b33bb6d169fe44c2a0b984fa5fc7933fda75948 Mon Sep 17 00:00:00 2001 From: Avin Divakara Date: Sun, 8 Oct 2023 07:40:21 +0530 Subject: [PATCH 2/6] Added testing unit for mailchimp adapter --- tests/e2e/Email/MailchimpTest.php | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/e2e/Email/MailchimpTest.php diff --git a/tests/e2e/Email/MailchimpTest.php b/tests/e2e/Email/MailchimpTest.php new file mode 100644 index 00000000..2bbb791a --- /dev/null +++ b/tests/e2e/Email/MailchimpTest.php @@ -0,0 +1,39 @@ +send($message)); + + $this->assertArrayHasKey('_id', $result); + $this->assertArrayHasKey('status', $result); + $this->assertTrue(str_contains(strtolower($result['status']), 'sent')); + } +} \ No newline at end of file From 17bdbf14ddd6d3f08b9cdfb118178ea9d226552d Mon Sep 17 00:00:00 2001 From: Avin Divakara Date: Sun, 8 Oct 2023 08:16:50 +0530 Subject: [PATCH 3/6] Adjust env variables and docker-compose setup --- docker-compose.yml | 1 + tests/e2e/Email/MailchimpTest.php | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 80d626d9..6f3790db 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,6 +5,7 @@ services: environment: - MAILGUN_API_KEY - MAILGUN_DOMAIN + - MAILCHIMP_API_KEY - SENDGRID_API_KEY - FCM_SERVER_KEY - FCM_SERVER_TO diff --git a/tests/e2e/Email/MailchimpTest.php b/tests/e2e/Email/MailchimpTest.php index 2bbb791a..fd6fa25e 100644 --- a/tests/e2e/Email/MailchimpTest.php +++ b/tests/e2e/Email/MailchimpTest.php @@ -18,10 +18,10 @@ public function testSendEmail() apiKey: $key, ); - $to = getenv('TEST_RECIPIENT_EMAIL'); + $to = getenv('TEST_EMAIL'); $subject = 'Test Subject'; $content = 'Test Content'; - $from = getenv('TEST_SENDER_EMAIL'); + $from = getenv('TEST_FROM_EMAIL'); $message = new Email( to: [$to], From 02f422d5326a8c1638946ef1a0b05368a5244726 Mon Sep 17 00:00:00 2001 From: Avin Divakara Date: Fri, 20 Oct 2023 17:42:23 +0530 Subject: [PATCH 4/6] Resolved linting errors --- .../Messaging/Adapters/Email/Mailchimp.php | 19 +++++++------------ tests/e2e/Email/MailchimpTest.php | 2 +- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/Utopia/Messaging/Adapters/Email/Mailchimp.php b/src/Utopia/Messaging/Adapters/Email/Mailchimp.php index cb8bfc46..9d7d6a36 100644 --- a/src/Utopia/Messaging/Adapters/Email/Mailchimp.php +++ b/src/Utopia/Messaging/Adapters/Email/Mailchimp.php @@ -45,16 +45,13 @@ public function getMaxMessagesPerRequest(): int */ protected function process(Email $message): string { - // $body = - // ); - // var_dump($body); return $this->request( method: 'POST', url: 'https://mandrillapp.com/api/1.0/messages/send', headers: [ 'Content-Type: application/json', ], - body: \json_encode(array( + body: \json_encode([ 'key' => $this->apiKey, 'message' => [ 'html' => $message->isHtml() ? $message->getContent() : null, @@ -67,17 +64,15 @@ protected function process(Email $message): string ), 'attachments' => $message->getAttachments() ? \array_map( fn ($attachement) => [ - "type" => $attachement['type'], - "name" => $attachement['name'], - "content" => $attachement['content'] + 'type' => $attachement['type'], + 'name' => $attachement['name'], + 'content' => $attachement['content'], ], $message->getAttachments() - ) : null, - ], - ) + ) : null, + ], + ] ) ); } } - -?> \ No newline at end of file diff --git a/tests/e2e/Email/MailchimpTest.php b/tests/e2e/Email/MailchimpTest.php index fd6fa25e..f8679ab0 100644 --- a/tests/e2e/Email/MailchimpTest.php +++ b/tests/e2e/Email/MailchimpTest.php @@ -36,4 +36,4 @@ public function testSendEmail() $this->assertArrayHasKey('status', $result); $this->assertTrue(str_contains(strtolower($result['status']), 'sent')); } -} \ No newline at end of file +} From 6b427577fd14b4010876dc0de704e8e8839bf906 Mon Sep 17 00:00:00 2001 From: Avin Divakara Date: Wed, 1 Nov 2023 15:53:43 +0530 Subject: [PATCH 5/6] Test File: Object to Array Typecasting Error fixed --- tests/e2e/Email/MailchimpTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Email/MailchimpTest.php b/tests/e2e/Email/MailchimpTest.php index f8679ab0..3073fe06 100644 --- a/tests/e2e/Email/MailchimpTest.php +++ b/tests/e2e/Email/MailchimpTest.php @@ -30,7 +30,7 @@ public function testSendEmail() content: $content, ); - $result = (array) \json_decode($sender->send($message)); + $result = (array) \json_decode($sender->send($message))[0]; $this->assertArrayHasKey('_id', $result); $this->assertArrayHasKey('status', $result); From 80c22d5de41d4367e01876e167ee9e98945056cc Mon Sep 17 00:00:00 2001 From: Avin Divakara <67309607+AVDiv@users.noreply.github.com> Date: Tue, 21 Nov 2023 18:26:21 +0530 Subject: [PATCH 6/6] Update tests/e2e/Email/MailchimpTest.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Matej Bačo --- tests/e2e/Email/MailchimpTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Email/MailchimpTest.php b/tests/e2e/Email/MailchimpTest.php index 3073fe06..5bed208e 100644 --- a/tests/e2e/Email/MailchimpTest.php +++ b/tests/e2e/Email/MailchimpTest.php @@ -34,6 +34,6 @@ public function testSendEmail() $this->assertArrayHasKey('_id', $result); $this->assertArrayHasKey('status', $result); - $this->assertTrue(str_contains(strtolower($result['status']), 'sent')); + $this->assertStringContainsStringIgnoringCase('sent', $result['status']); } }