Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>
#include <C_CkAuthAws.h>
#include <C_CkSocket.h>
#include <C_CkRest.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObject order;
    HCkJsonObject request;
    const char *region;
    HCkStringBuilder host;
    HCkAuthAws signer;
    HCkSocket connection;
    HCkRest rest;
    const char *sessionToken;
    const char *responseText;
    HCkJsonObject response;

    success = FALSE;

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

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

    signer = CkAuthAws_Create();
    //  Supply the credentials from your application's credential provider.
    CkAuthAws_putAccessKey(signer,"AWS_ACCESS_KEY_ID");
    CkAuthAws_putSecretKey(signer,"AWS_SECRET_ACCESS_KEY");
    CkAuthAws_putRegion(signer,region);
    CkAuthAws_putServiceName(signer,"ses");

    connection = CkSocket_Create();
    CkSocket_putRequireSslCertVerify(connection,TRUE);
    success = CkSocket_Connect(connection,CkStringBuilder_getAsString(host),443,TRUE,10000);
    if (success != TRUE) {
        printf("%s\n",CkSocket_lastErrorText(connection));
        CkJsonObject_Dispose(order);
        CkJsonObject_Dispose(request);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        return;
    }

    rest = CkRest_Create();
    success = CkRest_UseConnection(rest,connection,FALSE);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkJsonObject_Dispose(order);
        CkJsonObject_Dispose(request);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        return;
    }

    success = CkRest_SetAuthAws(rest,signer);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkJsonObject_Dispose(order);
        CkJsonObject_Dispose(request);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        return;
    }

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

    CkRest_AddHeader(rest,"Content-Type","application/json");
    responseText = CkRest_fullRequestString(rest,"POST","/v2/email/outbound-emails",CkJsonObject_emit(request));
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkJsonObject_Dispose(order);
        CkJsonObject_Dispose(request);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        return;
    }

    if (CkRest_getResponseStatusCode(rest) != 200) {
        printf("SES returned HTTP %d %s\n",CkRest_getResponseStatusCode(rest),responseText);
        CkJsonObject_Dispose(order);
        CkJsonObject_Dispose(request);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        return;
    }

    response = CkJsonObject_Create();
    success = CkJsonObject_Load(response,responseText);
    if (success != TRUE) {
        printf("%s\n",CkJsonObject_lastErrorText(response));
        CkJsonObject_Dispose(order);
        CkJsonObject_Dispose(request);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(response);
        return;
    }

    printf("SES accepted the message: %s\n",CkJsonObject_stringOf(response,"MessageId"));


    CkJsonObject_Dispose(order);
    CkJsonObject_Dispose(request);
    CkStringBuilder_Dispose(host);
    CkAuthAws_Dispose(signer);
    CkSocket_Dispose(connection);
    CkRest_Dispose(rest);
    CkJsonObject_Dispose(response);

    }