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 sparkpost.php #47

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove SPARKPOST_TO and instead use the TEST_EMAIL Env Var in your code as we already use that in the existing tests. Same goes for SPARKPOST_FROM as we can replace that with the existing TEST_FROM_EMAIL that we also use in tests already

SPARKPOST_FROM: ${{ secrets.SPARKPOST_FROM }}
run: |
docker compose up -d --build
sleep 5
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ services:
- VONAGE_API_SECRET
- VONAGE_TO
- VONAGE_FROM
- SPARKPOST_API_KEY
- SPARKPOST_TO
- SPARKPOST_FROM
build:
context: .
volumes:
Expand Down
71 changes: 71 additions & 0 deletions src/Utopia/Messaging/Adapters/Email/SparkPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Utopia\Messaging\Adapters\Email;

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

class SparkPost extends EmailAdapter{

public function __construct(private string $apiKey,
private bool $isEu = false){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formatting looks really weird here, could you run the formatter with composer format?

}

public function getName() :string{
return 'SparkPost';
}

public function getMaxMessagesPerRequest(): int{
//TODO: real value for this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you find the real value for this or do you want to keep it as 1000?

return 1000;
}
protected function process(Email $message) : string{

$usDomain = 'api.sparkpost.com';
$euDomain = 'api.eu.sparkpost.com';

$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' => [
'email' => $message->getTo()[0], // Assuming you have only one recipient
],
],
],
'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(),
],
];

$json_body = json_encode($requestBody);

$response = $this->request(
method: 'POST',
url: "https://$domain/api/v1/transmissions",
headers :[
'Authorization: '.$this->apiKey,
'Content-Type: application/json',
],
body: $json_body,
);

return $response;
}
}

?>
35 changes: 35 additions & 0 deletions tests/e2e/Email/SparkPostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Tests\E2E;


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

class SparkPostTest extends Base{
/**
* @throws \Exception
*/

public function testSendEmail(){

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formatting is also weird, should be fixed after you run composer format

$apiKey = getenv('SPARKPOST_API_KEY');

$to = getenv('SPARKPOST_TO');
$subject = 'Test Subject';
$content = 'Test Content';
$from = getenv('SPARKPOST_FROM');

$sparkPost = new SparkPost($apiKey);
$message = new Email(
to: [$to],
from: $from,
subject: $subject,
content: $content ,
);

$response = $sparkPost->send($message);
$this->assertNotEmpty($response);
}
}
?>
Loading