Last active
April 21, 2020 13:44
-
-
Save kntmr/c9b0217215c7da34e0e5e4183744ac92 to your computer and use it in GitHub Desktop.
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
<?php | |
require '../vendor/autoload.php'; | |
use Aws\Sqs\SqsClient; | |
use Aws\Exception\AwsException; | |
use Aws\Ses\SesClient; | |
$queueUrl = '<QUEUE_URL>'; | |
$client = new SqsClient([ | |
'region' => 'ap-northeast-1', | |
'version' => '2012-11-05' | |
]); | |
$ses = SesClient::factory(array( | |
'version'=> 'latest', | |
'region' => 'us-east-1', | |
)); | |
try { | |
$result = $client->receiveMessage(array( | |
'AttributeNames' => ['SentTimestamp'], | |
'MaxNumberOfMessages' => 1, | |
'MessageAttributeNames' => ['All'], | |
'QueueUrl' => $queueUrl, // REQUIRED | |
'WaitTimeSeconds' => 0, | |
)); | |
if (count($result->get('Messages')) > 0) { | |
$messages = $result->get('Messages'); | |
if ($messages) { | |
foreach ($messages as $item) { | |
$attr = $item['MessageAttributes']; | |
$sesResult = $ses->sendEmail([ | |
'Source' => $attr['from']['StringValue'], | |
'Destination' => [ | |
'ToAddresses' => [ | |
$attr['to']['StringValue'], | |
], | |
], | |
'Message' => [ | |
'Subject' => [ | |
'Charset' => 'UTF-8', | |
'Data' => $attr['subject']['StringValue'], | |
], | |
'Body' => [ | |
'Text' => [ | |
'Charset' => 'UTF-8', | |
'Data' => $item['Body']."\n", | |
], | |
], | |
], | |
]); | |
$messageId = $sesResult->get('MessageId'); | |
echo("Email sent! Message ID: $messageId"."\n"); | |
} | |
} | |
$result = $client->deleteMessage([ | |
'QueueUrl' => $queueUrl, // REQUIRED | |
'ReceiptHandle' => $result->get('Messages')[0]['ReceiptHandle'] // REQUIRED | |
]); | |
} else { | |
echo "No messages in queue. \n"; | |
} | |
} catch (AwsException $e) { | |
error_log($e->getMessage()); | |
} catch (SesException $error) { | |
echo("The email was not sent. Error message: ".$error->getAwsErrorMessage()."\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment