Dart
Dart
Amazon SES v2: Create and preview a receipt template
See more Amazon SES Examples
Create the regional OrderReceipt template once. Set createTemplate to false to preview it with sample order data. To exercise the missing-field case, omit orderNumber from the preview data and inspect the rendering error. A successful preview returns MIME content without sending a message.
CreateEmailTemplate · TestRenderEmailTemplate
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final createTemplate = true;
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
final region = 'us-west-2';
final host = CkStringBuilder();
host.append('email.');
host.append(region);
host.append('.amazonaws.com');
final signer = 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';
final connection = CkSocket();
connection.requireSslCertVerify = true;
try {
connection.connect(host.getAsString(), 443, true, 10000);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final rest = CkRest();
try {
rest.useConnection(connection, false);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
rest.setAuthAws(signer);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// For temporary AWS credentials, supply the session token from the same provider.
final sessionToken = '';
if (sessionToken != '') {
rest.addQueryParam('X-Amz-Security-Token', sessionToken);
}
if (createTemplate) {
final request = CkJsonObject();
request.updateString('TemplateName', 'OrderReceipt');
request.updateString('TemplateContent.Subject', 'Receipt for {{orderNumber}}');
request.updateString('TemplateContent.Text', 'Thank you, {{firstName}}. Order {{orderNumber}}: {{#each items}}{{quantity}} x {{name}}. {{/each}}');
request.updateString('TemplateContent.Html', '<p>Thank you, {{firstName}}.</p><ul>{{#each items}}<li>{{quantity}} x {{name}}</li>{{/each}}</ul>');
rest.addHeader('Content-Type', 'application/json');
final String setupResponse;
try {
setupResponse = rest.fullRequestString('POST', '/v2/email/templates', request.emit());
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
if (rest.responseStatusCode != 200) {
print('SES returned HTTP ${rest.responseStatusCode} $setupResponse');
return;
}
print('Template created. Set createTemplate to false to preview it.');
return;
}
final order = CkJsonObject();
order.updateString('firstName', 'Avery');
order.updateString('orderNumber', '1042');
order.updateString('items[0].name', 'Notebook');
order.updateInt('items[0].quantity', 2);
order.updateString('items[1].name', 'Pen');
order.updateInt('items[1].quantity', 1);
// Remove this key to try the missing-field case.
final tryMissingField = false;
if (tryMissingField) {
order.delete('orderNumber');
}
final preview = CkJsonObject();
preview.updateString('TemplateData', order.emit());
rest.addHeader('Content-Type', 'application/json');
final String responseText;
try {
responseText = rest.fullRequestString('POST', '/v2/email/templates/OrderReceipt/render', preview.emit());
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
if (rest.responseStatusCode != 200) {
print('SES returned HTTP ${rest.responseStatusCode} $responseText');
return;
}
final response = CkJsonObject();
try {
response.load(responseText);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print(response.stringOf('RenderedTemplate'));
}