PHP
PHP
Amazon SES v2: Send private appointment reminders and inspect each result
See more Amazon SES Examples
Replace the two recipients with test mailboxes you control. The example supplies an inline template and one private destination per entry. Store each successful message ID against its appointment. Schedule a retry with backoff only for entries with a retryable outcome. Correct permanent errors before resubmission.
SendBulkEmail · Per-entry outcomes
Chilkat PHP Downloads
<?php
use Chilkat2\AuthAws;
use Chilkat2\JsonObject;
use Chilkat2\Rest;
use Chilkat2\Socket;
use Chilkat2\StringBuilder;
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = false;
$request = new JsonObject();
$request->UpdateString('FromEmailAddress', 'sender@example.com');
$request->UpdateString('DefaultContent.Template.TemplateContent.Subject', 'Appointment reminder');
$request->UpdateString('DefaultContent.Template.TemplateContent.Text', 'Hello {{name}}, your appointment is {{slot}}.');
$request->UpdateString('DefaultContent.Template.TemplateData', '{"name":"there","slot":"in your booking confirmation"}');
$firstReminder = new JsonObject();
$firstReminder->UpdateString('name', 'Avery');
$firstReminder->UpdateString('slot', 'Tuesday at 9 AM Central');
$request->UpdateString('BulkEmailEntries[0].Destination.ToAddresses[0]', 'avery@example.com');
$request->UpdateString('BulkEmailEntries[0].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData', $firstReminder->Emit());
$secondReminder = new JsonObject();
$secondReminder->UpdateString('name', 'Robin');
$secondReminder->UpdateString('slot', 'Wednesday at 2 PM Central');
$request->UpdateString('BulkEmailEntries[1].Destination.ToAddresses[0]', 'robin@example.com');
$request->UpdateString('BulkEmailEntries[1].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData', $secondReminder->Emit());
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
$region = 'us-west-2';
$host = new StringBuilder();
$host->Append('email.');
$host->Append($region);
$host->Append('.amazonaws.com');
$signer = new AuthAws();
// Supply the credentials from your application's credential provider.
$signer->AccessKey = 'AWS_ACCESS_KEY_ID';
$signer->SecretKey = 'AWS_SECRET_ACCESS_KEY';
$signer->Region = $region;
$signer->ServiceName = 'ses';
$connection = new Socket();
$connection->RequireSslCertVerify = true;
try {
$connection->Connect($host->GetAsString(), 443, true, 10000);
$rest = new Rest();
$rest->UseConnection($connection, false);
$rest->SetAuthAws($signer);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// For temporary AWS credentials, supply the session token from the same provider.
$sessionToken = '';
if ($sessionToken !== '') {
$rest->AddQueryParam('X-Amz-Security-Token', $sessionToken);
}
$rest->AddHeader('Content-Type', 'application/json');
try {
$responseText = $rest->FullRequestString('POST', '/v2/email/outbound-bulk-emails', $request->Emit());
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
if ($rest->ResponseStatusCode != 200) {
echo 'SES returned HTTP ', $rest->ResponseStatusCode, ' ', $responseText, "\n";
exit;
}
$response = new JsonObject();
try {
$response->Load($responseText);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
$count = $response->SizeOfArray('BulkEmailEntryResults');
if ($count != $request->SizeOfArray('BulkEmailEntries')) {
echo "Incomplete results. Reconcile the original batch before retrying.\n";
exit;
}
$i = 0;
while ($i < $count) {
$response->I = $i;
$request->I = $i;
$recipient = $request->StringOf('BulkEmailEntries[i].Destination.ToAddresses[0]');
$status = $response->StringOf('BulkEmailEntryResults[i].Status');
if ($status === 'SUCCESS') {
echo $recipient, ' accepted: ', $response->StringOf('BulkEmailEntryResults[i].MessageId'), "\n";
} else {
if (($status === 'ACCOUNT_THROTTLED') || ($status === 'TRANSIENT_FAILURE')) {
echo $recipient, ' retry later: ', $status, "\n";
} else {
echo $recipient, ' review before retry: ', $status, ' ', $response->StringOf('BulkEmailEntryResults[i].Error'), "\n";
}
}
$i = $i + 1;
}