Sample code for 30+ languages & platforms
Unicode C

Amazon SES v2: Send a personalized receipt from a stored template

See more Amazon SES Examples

First create and preview the OrderReceipt template in the sending Region. Supply a verified sender. Build the order as structured data, then serialize it once into the string required by TemplateData. A message ID records acceptance. Rendering failures can be reported through SES event publishing.

SendEmail

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkSocketW.h>
#include <C_CkRestW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW order;
    HCkJsonObjectW request;
    const wchar_t *region;
    HCkStringBuilderW host;
    HCkAuthAwsW signer;
    HCkSocketW connection;
    HCkRestW rest;
    const wchar_t *sessionToken;
    const wchar_t *responseText;
    HCkJsonObjectW response;

    success = FALSE;

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

    order = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(order,L"firstName",L"Avery");
    CkJsonObjectW_UpdateString(order,L"orderNumber",L"1042");
    CkJsonObjectW_UpdateString(order,L"items[0].name",L"Notebook");
    CkJsonObjectW_UpdateInt(order,L"items[0].quantity",2);
    CkJsonObjectW_UpdateString(order,L"items[1].name",L"Pen");
    CkJsonObjectW_UpdateInt(order,L"items[1].quantity",1);
    request = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(request,L"FromEmailAddress",L"sender@example.com");
    CkJsonObjectW_UpdateString(request,L"Destination.ToAddresses[0]",L"success@simulator.amazonses.com");
    CkJsonObjectW_UpdateString(request,L"Content.Template.TemplateName",L"OrderReceipt");
    CkJsonObjectW_UpdateString(request,L"Content.Template.TemplateData",CkJsonObjectW_emit(order));
    //  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    region = L"us-west-2";
    host = CkStringBuilderW_Create();
    CkStringBuilderW_Append(host,L"email.");
    CkStringBuilderW_Append(host,region);
    CkStringBuilderW_Append(host,L".amazonaws.com");

    signer = CkAuthAwsW_Create();
    //  Supply the credentials from your application's credential provider.
    CkAuthAwsW_putAccessKey(signer,L"AWS_ACCESS_KEY_ID");
    CkAuthAwsW_putSecretKey(signer,L"AWS_SECRET_ACCESS_KEY");
    CkAuthAwsW_putRegion(signer,region);
    CkAuthAwsW_putServiceName(signer,L"ses");

    connection = CkSocketW_Create();
    CkSocketW_putRequireSslCertVerify(connection,TRUE);
    success = CkSocketW_Connect(connection,CkStringBuilderW_getAsString(host),443,TRUE,10000);
    if (success != TRUE) {
        wprintf(L"%s\n",CkSocketW_lastErrorText(connection));
        CkJsonObjectW_Dispose(order);
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        return;
    }

    rest = CkRestW_Create();
    success = CkRestW_UseConnection(rest,connection,FALSE);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkJsonObjectW_Dispose(order);
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        return;
    }

    success = CkRestW_SetAuthAws(rest,signer);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkJsonObjectW_Dispose(order);
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        return;
    }

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

    CkRestW_AddHeader(rest,L"Content-Type",L"application/json");
    responseText = CkRestW_fullRequestString(rest,L"POST",L"/v2/email/outbound-emails",CkJsonObjectW_emit(request));
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkJsonObjectW_Dispose(order);
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        return;
    }

    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"SES returned HTTP %d %s\n",CkRestW_getResponseStatusCode(rest),responseText);
        CkJsonObjectW_Dispose(order);
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        return;
    }

    response = CkJsonObjectW_Create();
    success = CkJsonObjectW_Load(response,responseText);
    if (success != TRUE) {
        wprintf(L"%s\n",CkJsonObjectW_lastErrorText(response));
        CkJsonObjectW_Dispose(order);
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        CkJsonObjectW_Dispose(response);
        return;
    }

    wprintf(L"SES accepted the message: %s\n",CkJsonObjectW_stringOf(response,L"MessageId"));


    CkJsonObjectW_Dispose(order);
    CkJsonObjectW_Dispose(request);
    CkStringBuilderW_Dispose(host);
    CkAuthAwsW_Dispose(signer);
    CkSocketW_Dispose(connection);
    CkRestW_Dispose(rest);
    CkJsonObjectW_Dispose(response);

    }