JavaScript
JavaScript
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
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
var request = new CkJsonObject();
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\"}");
var firstReminder = new CkJsonObject();
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());
var secondReminder = new CkJsonObject();
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.
var region = "us-west-2";
var host = new CkStringBuilder();
host.Append("email.");
host.Append(region);
host.Append(".amazonaws.com");
var signer = new CkAuthAws();
// 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";
var connection = new CkSocket();
connection.RequireSslCertVerify = true;
success = connection.Connect(host.GetAsString(),443,true,10000);
if (success !== true) {
console.log(connection.LastErrorText);
return;
}
var rest = new CkRest();
success = rest.UseConnection(connection,false);
if (success !== true) {
console.log(rest.LastErrorText);
return;
}
success = rest.SetAuthAws(signer);
if (success !== true) {
console.log(rest.LastErrorText);
return;
}
// For temporary AWS credentials, supply the session token from the same provider.
var sessionToken = "";
if (sessionToken !== "") {
rest.AddQueryParam("X-Amz-Security-Token",sessionToken);
}
rest.AddHeader("Content-Type","application/json");
var responseText = rest.FullRequestString("POST","/v2/email/outbound-bulk-emails",request.Emit());
if (rest.LastMethodSuccess !== true) {
console.log(rest.LastErrorText);
return;
}
if (rest.ResponseStatusCode !== 200) {
console.log("SES returned HTTP " + rest.ResponseStatusCode + " " + responseText);
return;
}
var response = new CkJsonObject();
success = response.Load(responseText);
if (success !== true) {
console.log(response.LastErrorText);
return;
}
var count = response.SizeOfArray("BulkEmailEntryResults");
if (count !== request.SizeOfArray("BulkEmailEntries")) {
console.log("Incomplete results. Reconcile the original batch before retrying.");
return;
}
var i = 0;
while ((i < count)) {
response.I = i;
request.I = i;
var recipient = request.StringOf("BulkEmailEntries[i].Destination.ToAddresses[0]");
var status = response.StringOf("BulkEmailEntryResults[i].Status");
if (status == "SUCCESS") {
console.log(recipient + " accepted: " + response.StringOf("BulkEmailEntryResults[i].MessageId"));
}
else {
if ((status == "ACCOUNT_THROTTLED") || (status == "TRANSIENT_FAILURE")) {
console.log(recipient + " retry later: " + status);
}
else {
console.log(recipient + " review before retry: " + status + " " + response.StringOf("BulkEmailEntryResults[i].Error"));
}
}
i = i+1;
}