From dbfd2a91cd9ca3602de63562801f18b2ad03a862 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 315ad222c853ec3d3323bdad105d197673781dda Mon Sep 17 00:00:00 2001 From: Avin Divakara Date: Sun, 8 Oct 2023 08:16:50 +0530 Subject: [PATCH 2/6] Adjust env variables and docker-compose setup --- docker-compose.yml | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 08f116d5..6f3790db 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,48 @@ +version: '3.9' + services: + tests: + environment: + - MAILGUN_API_KEY + - MAILGUN_DOMAIN + - MAILCHIMP_API_KEY + - SENDGRID_API_KEY + - FCM_SERVER_KEY + - FCM_SERVER_TO + - TWILIO_ACCOUNT_SID + - TWILIO_AUTH_TOKEN + - TWILIO_TO + - TWILIO_FROM + - TELNYX_API_KEY + - TELNYX_PUBLIC_KEY + - APNS_AUTHKEY_8KVVCLA3HL + - APNS_AUTH_ID + - APNS_TEAM_ID + - APNS_BUNDLE_ID + - APNS_TO + - MSG_91_SENDER_ID + - MSG_91_AUTH_KEY + - MSG_91_TO + - MSG_91_FROM + - TEST_EMAIL + - TEST_FROM_EMAIL + - VONAGE_API_KEY + - VONAGE_API_SECRET + - VONAGE_TO + - VONAGE_FROM + build: + context: . + volumes: + - ./src:/usr/local/src/src + - ./tests:/usr/local/src/tests + - ./phpunit.xml:/usr/local/src/phpunit.xml + maildev: image: appwrite/mailcatcher:1.0.0 ports: - - '11025:1025' - - '11080:1080' + - '10000:1080' request-catcher: image: appwrite/requestcatcher:1.0.0 ports: - - '15000:5000' + - '10001:5000' \ No newline at end of file From e5763b733dbe55a01eec42fcee195f9c1c3e04da Mon Sep 17 00:00:00 2001 From: Avin Divakara Date: Sun, 8 Oct 2023 07:40:21 +0530 Subject: [PATCH 3/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 53d26ef29c6ab9201129ad7968b2e679adf92d74 Mon Sep 17 00:00:00 2001 From: Avin Divakara Date: Wed, 1 Nov 2023 15:53:43 +0530 Subject: [PATCH 4/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 2bbb791a..44c23c1f 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 7b736a00cc8b2f87ed5bbe21a7205a418b107148 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 5/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 44c23c1f..27325a6e 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']); } } \ No newline at end of file From bcf05814ebd1995c06ec223b96b02c445291d3ad Mon Sep 17 00:00:00 2001 From: deepshekhardas Date: Sat, 25 Jul 2026 13:40:48 +0530 Subject: [PATCH 6/6] fix(Mailchimp): move to correct directory and update to current conventions --- docker-compose.yml | 43 +--------- .../Messaging/Adapter/Email/Mailchimp.php | 78 +++++++++++++++++ .../Messaging/Adapters/Email/Mailchimp.php | 83 ------------------- tests/e2e/Email/MailchimpTest.php | 39 --------- 4 files changed, 81 insertions(+), 162 deletions(-) create mode 100644 src/Utopia/Messaging/Adapter/Email/Mailchimp.php delete mode 100644 src/Utopia/Messaging/Adapters/Email/Mailchimp.php delete mode 100644 tests/e2e/Email/MailchimpTest.php diff --git a/docker-compose.yml b/docker-compose.yml index 6f3790db..08f116d5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,48 +1,11 @@ -version: '3.9' - services: - tests: - environment: - - MAILGUN_API_KEY - - MAILGUN_DOMAIN - - MAILCHIMP_API_KEY - - SENDGRID_API_KEY - - FCM_SERVER_KEY - - FCM_SERVER_TO - - TWILIO_ACCOUNT_SID - - TWILIO_AUTH_TOKEN - - TWILIO_TO - - TWILIO_FROM - - TELNYX_API_KEY - - TELNYX_PUBLIC_KEY - - APNS_AUTHKEY_8KVVCLA3HL - - APNS_AUTH_ID - - APNS_TEAM_ID - - APNS_BUNDLE_ID - - APNS_TO - - MSG_91_SENDER_ID - - MSG_91_AUTH_KEY - - MSG_91_TO - - MSG_91_FROM - - TEST_EMAIL - - TEST_FROM_EMAIL - - VONAGE_API_KEY - - VONAGE_API_SECRET - - VONAGE_TO - - VONAGE_FROM - build: - context: . - volumes: - - ./src:/usr/local/src/src - - ./tests:/usr/local/src/tests - - ./phpunit.xml:/usr/local/src/phpunit.xml - maildev: image: appwrite/mailcatcher:1.0.0 ports: - - '10000:1080' + - '11025:1025' + - '11080:1080' request-catcher: image: appwrite/requestcatcher:1.0.0 ports: - - '10001:5000' \ No newline at end of file + - '15000:5000' diff --git a/src/Utopia/Messaging/Adapter/Email/Mailchimp.php b/src/Utopia/Messaging/Adapter/Email/Mailchimp.php new file mode 100644 index 00000000..a317fd24 --- /dev/null +++ b/src/Utopia/Messaging/Adapter/Email/Mailchimp.php @@ -0,0 +1,78 @@ +getType()); + + $result = $this->request( + method: 'POST', + url: 'https://mandrillapp.com/api/1.0/messages/send', + headers: [ + 'Content-Type: application/json', + ], + body: [ + 'key' => $this->apiKey, + 'message' => [ + 'html' => $message->isHtml() ? $message->getContent() : null, + 'text' => $message->isHtml() ? null : $message->getContent(), + 'subject' => $message->getSubject(), + 'from_email' => $message->getFromEmail(), + 'from_name' => $message->getFromName(), + 'to' => array_map( + fn(array $to) => ['email' => $to['email'], 'name' => $to['name'] ?? ''], + $message->getTo() + ), + ], + ], + ); + + $statusCode = $result['statusCode']; + $responseBody = $result['response']; + + if ($statusCode >= 200 && $statusCode < 300) { + $response->setDeliveredTo(\count($message->getTo())); + foreach ($message->getTo() as $to) { + $response->addResult($to['email']); + } + } elseif ($statusCode >= 400 && $statusCode < 500) { + $error = ''; + if (\is_array($responseBody) && isset($responseBody['message'])) { + $error = $responseBody['message']; + } elseif (\is_string($responseBody)) { + $error = $responseBody; + } + + foreach ($message->getTo() as $to) { + $response->addResult($to['email'], $error ?: 'Unknown error'); + } + } + + return $response->toArray(); + } +} diff --git a/src/Utopia/Messaging/Adapters/Email/Mailchimp.php b/src/Utopia/Messaging/Adapters/Email/Mailchimp.php deleted file mode 100644 index cb8bfc46..00000000 --- a/src/Utopia/Messaging/Adapters/Email/Mailchimp.php +++ /dev/null @@ -1,83 +0,0 @@ -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 diff --git a/tests/e2e/Email/MailchimpTest.php b/tests/e2e/Email/MailchimpTest.php deleted file mode 100644 index 27325a6e..00000000 --- a/tests/e2e/Email/MailchimpTest.php +++ /dev/null @@ -1,39 +0,0 @@ -send($message))[0]; - - $this->assertArrayHasKey('_id', $result); - $this->assertArrayHasKey('status', $result); - $this->assertStringContainsStringIgnoringCase('sent', $result['status']); - } -} \ No newline at end of file