Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkStringBuilderW.h>
#include <CkAuthAwsW.h>
#include <CkSocketW.h>
#include <CkRestW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;

    bool createTemplate = true;
    //  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    const wchar_t *region = L"us-west-2";
    CkStringBuilderW host;
    host.Append(L"email.");
    host.Append(region);
    host.Append(L".amazonaws.com");

    CkAuthAwsW signer;
    //  Supply the credentials from your application's credential provider.
    signer.put_AccessKey(L"AWS_ACCESS_KEY_ID");
    signer.put_SecretKey(L"AWS_SECRET_ACCESS_KEY");
    signer.put_Region(region);
    signer.put_ServiceName(L"ses");

    CkSocketW connection;
    connection.put_RequireSslCertVerify(true);
    success = connection.Connect(host.getAsString(),443,true,10000);
    if (success != true) {
        wprintf(L"%s\n",connection.lastErrorText());
        return;
    }

    CkRestW rest;
    success = rest.UseConnection(connection,false);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    success = rest.SetAuthAws(signer);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    //  For temporary AWS credentials, supply the session token from the same provider.
    const wchar_t *sessionToken = L"";
    if (sessionToken != L"") {
        rest.AddQueryParam(L"X-Amz-Security-Token",sessionToken);
    }

    if (createTemplate == true) {
        CkJsonObjectW request;
        request.UpdateString(L"TemplateName",L"OrderReceipt");
        request.UpdateString(L"TemplateContent.Subject",L"Receipt for {{orderNumber}}");
        request.UpdateString(L"TemplateContent.Text",L"Thank you, {{firstName}}. Order {{orderNumber}}: {{#each items}}{{quantity}} x {{name}}. {{/each}}");
        request.UpdateString(L"TemplateContent.Html",L"<p>Thank you, {{firstName}}.</p><ul>{{#each items}}<li>{{quantity}} x {{name}}</li>{{/each}}</ul>");

        rest.AddHeader(L"Content-Type",L"application/json");
        const wchar_t *setupResponse = rest.fullRequestString(L"POST",L"/v2/email/templates",request.emit());
        if (rest.get_LastMethodSuccess() != true) {
            wprintf(L"%s\n",rest.lastErrorText());
            return;
        }

        if (rest.get_ResponseStatusCode() != 200) {
            wprintf(L"SES returned HTTP %d %s\n",rest.get_ResponseStatusCode(),setupResponse);
            return;
        }

        wprintf(L"Template created. Set createTemplate to false to preview it.\n");
        return;
    }

    CkJsonObjectW order;
    order.UpdateString(L"firstName",L"Avery");
    order.UpdateString(L"orderNumber",L"1042");
    order.UpdateString(L"items[0].name",L"Notebook");
    order.UpdateInt(L"items[0].quantity",2);
    order.UpdateString(L"items[1].name",L"Pen");
    order.UpdateInt(L"items[1].quantity",1);
    //  Remove this key to try the missing-field case.
    bool tryMissingField = false;
    if (tryMissingField == true) {
        order.Delete(L"orderNumber");
    }

    CkJsonObjectW preview;
    preview.UpdateString(L"TemplateData",order.emit());

    rest.AddHeader(L"Content-Type",L"application/json");
    const wchar_t *responseText = rest.fullRequestString(L"POST",L"/v2/email/templates/OrderReceipt/render",preview.emit());
    if (rest.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    if (rest.get_ResponseStatusCode() != 200) {
        wprintf(L"SES returned HTTP %d %s\n",rest.get_ResponseStatusCode(),responseText);
        return;
    }

    CkJsonObjectW response;
    success = response.Load(responseText);
    if (success != true) {
        wprintf(L"%s\n",response.lastErrorText());
        return;
    }

    wprintf(L"%s\n",response.stringOf(L"RenderedTemplate"));
    }