-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from chijioke-ibekwe/add-ses-email-provider
feat: implement amazon ses email provider
- Loading branch information
Showing
16 changed files
with
374 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ build | |
composer.lock | ||
coverage | ||
docs | ||
phpunit.xml | ||
phpstan.neon | ||
testbench.yaml | ||
vendor | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Unit"> | ||
<directory suffix="Test.php">./tests/Unit</directory> | ||
</testsuite> | ||
|
||
<testsuite name="Feature"> | ||
<directory suffix="Test.php">./tests/Feature</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<php> | ||
<env name="APP_ENV" value="testing"/> | ||
<env name="DB_CONNECTION" value="sqlite"/> | ||
<env name="DB_DATABASE" value=":memory:"/> | ||
</php> | ||
|
||
<source> | ||
<include> | ||
<directory suffix=".php">src/</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace ChijiokeIbekwe\Raven\Channels; | ||
|
||
use Aws\Ses\Exception\SesException; | ||
use Aws\Ses\SesClient; | ||
use ChijiokeIbekwe\Raven\Notifications\EmailNotificationSender; | ||
use Exception; | ||
use Illuminate\Support\Facades\Log; | ||
use SendGrid; | ||
|
||
class AmazonSesChannel | ||
{ | ||
private SesClient $sesClient; | ||
private SendGrid $sendGrid; | ||
|
||
public function __construct() | ||
{ | ||
$this->sesClient = app(SesClient::class); | ||
$this->sendGrid = app(SendGrid::class); | ||
} | ||
|
||
/** | ||
* Send the given notification. | ||
* | ||
* @param mixed $notifiable | ||
* @param EmailNotificationSender $emailNotification | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function send(mixed $notifiable, EmailNotificationSender $emailNotification): void | ||
{ | ||
$email = $emailNotification->toAmazonSes($notifiable); | ||
|
||
$sender = config('raven.customizations.mail.from'); | ||
$email->setFrom($sender['address'], $sender['name']); | ||
|
||
$template_source = config('raven.providers.ses.template_source'); | ||
if($template_source !== 'sendgrid') { | ||
Log::error("Template source $template_source not currently supported"); | ||
throw new Exception("Template source $template_source not currently supported"); | ||
} | ||
|
||
$template_response = $this->getSendGridTemplateContent($emailNotification); | ||
|
||
$params = $emailNotification->notificationData->getParams(); | ||
$clean_html = $this->cleanTemplate($template_response['html_content'], $params); | ||
$clean_plain = $this->cleanTemplate($template_response['plain_content'], $params); | ||
|
||
$email->Subject = $template_response['subject']; | ||
$email->Body = $clean_html; | ||
$email->AltBody = $clean_plain; | ||
|
||
if (!$email->preSend()) { | ||
Log::error("Failed sending mail: " . $email->ErrorInfo); | ||
throw new Exception($email->ErrorInfo); | ||
} else { | ||
$message = $email->getSentMIMEMessage(); | ||
} | ||
|
||
try { | ||
$result = $this->sesClient->sendRawEmail([ | ||
'RawMessage' => [ | ||
'Data' => $message | ||
] | ||
]); | ||
Log::info($result); | ||
} catch (SesException $error) { | ||
Log::error("Failed sending mail: " . $error->getAwsErrorMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
private function getSendGridTemplateContent(EmailNotificationSender $emailNotification): array | ||
{ | ||
try { | ||
$template_id = $emailNotification->notificationContext->email_template_id; | ||
$response = $this->sendGrid->client->templates()->_($template_id)->get(); | ||
|
||
if(!($response->statusCode() >= '200' && $response->statusCode() < 300)) { | ||
throw new Exception("SendGrid server returned error response"); | ||
} | ||
|
||
$body_json = $response->body(); | ||
$body_arr = json_decode($body_json, true); | ||
$subject = $body_arr['versions'][0]['subject']; | ||
$html_content = $body_arr['versions'][0]['html_content']; | ||
$plain_content = $body_arr['versions'][0]['plain_content']; | ||
|
||
return [ | ||
'subject' => $subject, | ||
'html_content' => $html_content, | ||
'plain_content' => $plain_content | ||
]; | ||
|
||
} catch (Exception $e) { | ||
Log::error("Failed sending mail: " . $e->getMessage()); | ||
throw new Exception($e); | ||
} | ||
} | ||
|
||
private function cleanTemplate($template, $data) | ||
{ | ||
foreach ($data as $key => $value) { | ||
$template = str_replace('{{' . $key . '}}', $value, $template); | ||
} | ||
return $template; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.