From d9b7186854a17b5044d3be9add4026c1c214f44b Mon Sep 17 00:00:00 2001 From: Shivam Chandra Date: Mon, 9 Oct 2023 01:08:00 +0530 Subject: [PATCH 1/5] added sparkpost.php --- .../Messaging/Adapters/Email/SparkPost.php | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/Utopia/Messaging/Adapters/Email/SparkPost.php diff --git a/src/Utopia/Messaging/Adapters/Email/SparkPost.php b/src/Utopia/Messaging/Adapters/Email/SparkPost.php new file mode 100644 index 00000000..d795e1f9 --- /dev/null +++ b/src/Utopia/Messaging/Adapters/Email/SparkPost.php @@ -0,0 +1,49 @@ +isEu ? $euDomain : $usDomain; + + $response = $this->request( + method: 'POST', + url: "https://$domain/api/v1/transmissions", + headers :[ + 'Authorization: Basic '.$this->apiKey, + ], + body: \http_build_query([ + 'to' => \implode(',', $message->getTo()), + 'from' => $message->getFrom(), + 'subject' => $message->getSubject(), + 'text' => $message->isHtml() ? null : $message->getContent(), + 'html' => $message->isHtml() ? $message->getContent() : null, + ]), + ); + + return $response; + } +} + +?> \ No newline at end of file From c75b44e86037114a6cc4cfa0b6d133be2d06841d Mon Sep 17 00:00:00 2001 From: Shivam Chandra Date: Mon, 9 Oct 2023 03:54:52 +0530 Subject: [PATCH 2/5] modified request body --- .../Messaging/Adapters/Email/SparkPost.php | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Utopia/Messaging/Adapters/Email/SparkPost.php b/src/Utopia/Messaging/Adapters/Email/SparkPost.php index d795e1f9..10f579f0 100644 --- a/src/Utopia/Messaging/Adapters/Email/SparkPost.php +++ b/src/Utopia/Messaging/Adapters/Email/SparkPost.php @@ -27,19 +27,33 @@ protected function process(Email $message) : string{ $domain = $this->isEu ? $euDomain : $usDomain; + $requestBody = [ + 'recipients' => [ + [ + 'address' => [ + 'email' => $message->getTo()[0], // Assuming you have only one recipient + ], + ], + ], + 'content' => [ + 'from' => [ + 'email' => $message->getFrom(), + ], + 'subject' => $message->getSubject(), + 'html' => $message->isHtml() ? $message->getContent() : null, + 'text' => $message->isHtml() ? null : $message->getContent(), + ], + ]; + + $json_body = json_encode($requestBody); + $response = $this->request( method: 'POST', url: "https://$domain/api/v1/transmissions", headers :[ 'Authorization: Basic '.$this->apiKey, ], - body: \http_build_query([ - 'to' => \implode(',', $message->getTo()), - 'from' => $message->getFrom(), - 'subject' => $message->getSubject(), - 'text' => $message->isHtml() ? null : $message->getContent(), - 'html' => $message->isHtml() ? $message->getContent() : null, - ]), + body: $json_body, ); return $response; From cab7b91f623207bf2495ed27408f1374e0fbd7df Mon Sep 17 00:00:00 2001 From: Shivam Chandra Date: Tue, 10 Oct 2023 01:28:25 +0530 Subject: [PATCH 3/5] added senderDomain as parameter --- src/Utopia/Messaging/Adapters/Email/SparkPost.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Utopia/Messaging/Adapters/Email/SparkPost.php b/src/Utopia/Messaging/Adapters/Email/SparkPost.php index 10f579f0..a8cdadbd 100644 --- a/src/Utopia/Messaging/Adapters/Email/SparkPost.php +++ b/src/Utopia/Messaging/Adapters/Email/SparkPost.php @@ -7,7 +7,7 @@ class SparkPost extends EmailAdapter{ - public function __construct(private string $apiKey, + public function __construct(private string $apiKey, private string $senderDomain, private bool $isEu = false){ } @@ -28,6 +28,10 @@ protected function process(Email $message) : string{ $domain = $this->isEu ? $euDomain : $usDomain; $requestBody = [ + 'options' =>[ + //for testing without domain set sandbox option to true(for more information, visit sparkpost.com) + 'sandbox' => false, + ], 'recipients' => [ [ 'address' => [ @@ -37,7 +41,8 @@ protected function process(Email $message) : string{ ], 'content' => [ 'from' => [ - 'email' => $message->getFrom(), + //sender domain should be registered and verified at sparkpost.com + 'email' => $this->senderDomain, ], 'subject' => $message->getSubject(), 'html' => $message->isHtml() ? $message->getContent() : null, From 7716163ca2bc9ab09f73b963cb33a0856104440b Mon Sep 17 00:00:00 2001 From: Shivam Chandra Date: Wed, 11 Oct 2023 01:25:17 +0530 Subject: [PATCH 4/5] added SparkPostTest.php, added environment variables in docker-compose.yml and test.yml --- .github/workflows/tests.yml | 3 ++ docker-compose.yml | 3 ++ .../Messaging/Adapters/Email/SparkPost.php | 6 ++-- tests/e2e/Email/SparkPostTest.php | 36 +++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 tests/e2e/Email/SparkPostTest.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 552b207d..e49a7098 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -40,6 +40,9 @@ jobs: VONAGE_API_SECRET: ${{ secrets.VONAGE_API_SECRET }} VONAGE_TO: ${{ secrets.VONAGE_TO }} VONAGE_FROM: ${{ secrets.VONAGE_FROM }} + SPARKPOST_API_KEY: ${{ secrets.SPARKPOST_API_KEY }} + SPARKPOST_TO: ${{ secrets.SPARKPOST_TO }} + SPARKPOST_FROM: ${{ secrets.SPARKPOST_FROM}} run: | docker compose up -d --build sleep 5 diff --git a/docker-compose.yml b/docker-compose.yml index 80d626d9..34d107a2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,6 +29,9 @@ services: - VONAGE_API_SECRET - VONAGE_TO - VONAGE_FROM + - SPARKPOST_API_KEY + - SPARKPOST_TO + - SPARKPOST_FROM build: context: . volumes: diff --git a/src/Utopia/Messaging/Adapters/Email/SparkPost.php b/src/Utopia/Messaging/Adapters/Email/SparkPost.php index a8cdadbd..e3c462c4 100644 --- a/src/Utopia/Messaging/Adapters/Email/SparkPost.php +++ b/src/Utopia/Messaging/Adapters/Email/SparkPost.php @@ -7,7 +7,7 @@ class SparkPost extends EmailAdapter{ - public function __construct(private string $apiKey, private string $senderDomain, + public function __construct(private string $apiKey, private bool $isEu = false){ } @@ -41,8 +41,8 @@ protected function process(Email $message) : string{ ], 'content' => [ 'from' => [ - //sender domain should be registered and verified at sparkpost.com - 'email' => $this->senderDomain, + //sender domain should be registered and verified at sparkpost + 'email' => $message->getFrom(), ], 'subject' => $message->getSubject(), 'html' => $message->isHtml() ? $message->getContent() : null, diff --git a/tests/e2e/Email/SparkPostTest.php b/tests/e2e/Email/SparkPostTest.php new file mode 100644 index 00000000..bb19cbb5 --- /dev/null +++ b/tests/e2e/Email/SparkPostTest.php @@ -0,0 +1,36 @@ +send($message); + + $this->assertEquals($response, ''); + } +} +?> \ No newline at end of file From 3b823622ac6f4bd40eeb3566c4ce34ee193e90db Mon Sep 17 00:00:00 2001 From: Shivam Chandra Date: Thu, 12 Oct 2023 03:36:59 +0530 Subject: [PATCH 5/5] implemented SparkPostTest.php --- .github/workflows/tests.yml | 2 +- src/Utopia/Messaging/Adapters/Email/SparkPost.php | 7 +++++-- tests/e2e/Email/SparkPostTest.php | 9 ++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e49a7098..5844f4c5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -42,7 +42,7 @@ jobs: VONAGE_FROM: ${{ secrets.VONAGE_FROM }} SPARKPOST_API_KEY: ${{ secrets.SPARKPOST_API_KEY }} SPARKPOST_TO: ${{ secrets.SPARKPOST_TO }} - SPARKPOST_FROM: ${{ secrets.SPARKPOST_FROM}} + SPARKPOST_FROM: ${{ secrets.SPARKPOST_FROM }} run: | docker compose up -d --build sleep 5 diff --git a/src/Utopia/Messaging/Adapters/Email/SparkPost.php b/src/Utopia/Messaging/Adapters/Email/SparkPost.php index e3c462c4..33dfd6c3 100644 --- a/src/Utopia/Messaging/Adapters/Email/SparkPost.php +++ b/src/Utopia/Messaging/Adapters/Email/SparkPost.php @@ -19,7 +19,6 @@ public function getMaxMessagesPerRequest(): int{ //TODO: real value for this return 1000; } - protected function process(Email $message) : string{ $usDomain = 'api.sparkpost.com'; @@ -40,10 +39,13 @@ protected function process(Email $message) : string{ ], ], 'content' => [ + // for testing with sandbox template id should be 'my-first-email' , so uncomment below line + // 'template_id' => 'my-first-email', 'from' => [ //sender domain should be registered and verified at sparkpost 'email' => $message->getFrom(), ], + //when testing with sandbox comment out below three lines 'subject' => $message->getSubject(), 'html' => $message->isHtml() ? $message->getContent() : null, 'text' => $message->isHtml() ? null : $message->getContent(), @@ -56,7 +58,8 @@ protected function process(Email $message) : string{ method: 'POST', url: "https://$domain/api/v1/transmissions", headers :[ - 'Authorization: Basic '.$this->apiKey, + 'Authorization: '.$this->apiKey, + 'Content-Type: application/json', ], body: $json_body, ); diff --git a/tests/e2e/Email/SparkPostTest.php b/tests/e2e/Email/SparkPostTest.php index bb19cbb5..0c5cc90c 100644 --- a/tests/e2e/Email/SparkPostTest.php +++ b/tests/e2e/Email/SparkPostTest.php @@ -1,5 +1,8 @@ send($message); - - $this->assertEquals($response, ''); + $this->assertNotEmpty($response); } } ?> \ No newline at end of file