-
Notifications
You must be signed in to change notification settings - Fork 64
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
base: main
Are you sure you want to change the base?
Changes from all commits
d9b7186
c75b44e
cab7b91
7716163
3b82362
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This formatting looks really weird here, could you run the formatter with |
||
} | ||
|
||
public function getName() :string{ | ||
return 'SparkPost'; | ||
} | ||
|
||
public function getMaxMessagesPerRequest(): int{ | ||
//TODO: real value for this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
|
||
?> |
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(){ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This formatting is also weird, should be fixed after you run |
||
$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); | ||
} | ||
} | ||
?> |
There was a problem hiding this comment.
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 theTEST_EMAIL
Env Var in your code as we already use that in the existing tests. Same goes forSPARKPOST_FROM
as we can replace that with the existingTEST_FROM_EMAIL
that we also use in tests already