4

Is it possible to send a single email to a Subscriber rather than a whole List using the SSJS Send Mail function?

My code is as follows, you can see we have to provide a List ID to send to.

<script type="text/javascript" runat="server">

    // Include SSJS Core Library
    Platform.Load("core", "1.1.1");

    // retrieve all the lists in the account
    var retrievedLists = List.Retrieve();

    // Options
    var options = {
        FromName : "JSON Specified Name",
        FromAddress : "aruiz@example.com",
        Subject : "JSON Test Mail"
    };

    // Send the email to all the lists
    status = Send.Add("Existing_Email_CustKey", retrievedLists, options);
</script>
3

I believe the only two ways to do this are to:

  1. Create a Data Extension or List with just the single individual
  2. Create the email as a Triggered Send Definition and initiate that way.

For 1 - there are ways to create Data Extensions and insert records via the API or SSJS Core library that can allow you to automate this process.

SSJS: Add

var deObj = {
        "CustomerKey" : "demoDE",
        "Name" : "My Demo DE",
        "Fields" : [
          { "Name" : "Field 1", "FieldType" : "Number", "IsPrimaryKey" : true, "IsRequired" : true },
          { "Name" : "Field 2", "FieldType" : "Text", "MaxLength" : 50 },
          { "Name" : "Field 3", "FieldType" : "Date", "Ordinal" : 2 },
        ]
    };

var myDE = DataExtension.Add(deObj);

SSJS: Rows.Add

var birthdayDE = DataExtension.Init("birthdayDE");
birthdayDE.Rows.Add({FirstName:"Angel",LastName:"Ruiz",EmailAddress:"aruiz@example.com",Age:24,Birthday:"11/29/1980"});

API: Create

var prox = new Script.Util.WSProxy();

var guid = Platform.Function.GUID();
var name = "my test de - " + guid;

var de = {
    Name: name,
    CustomerKey: guid,
    Description: "Another DE added via SSJS",
    Fields: [{
        FieldType: "Text",
        Name: "ID",
        MaxLength: 36,
        IsPrimaryKey: true,
        IsNillable: false,
        IsRequired: true
    },
    {
        FieldType: "Text",
        Name: "Name",
        MaxLength: 200
    }],
    CategoryID: 101377
}

var res = prox.createItem("DataExtension", de);

API: Add Data to Data Extension (shown in SOAP, but can be turned into WSProxy)

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
     <soap:Header>
          <wsse:Security soap:mustUnderstand="1">
               <wsse:UsernameToken wsu:Id="SecurityToken-6421ac0b-a454-42a3-99f7-a77ace024446">
                                                      <wsse:Username>XXXXX</wsse:Username>
                    <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXX</wsse:Password>     
               </wsse:UsernameToken>
          </wsse:Security>
     </soap:Header>
     <soap:Body>
          <CreateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">
               <Options/>
                    <Objects xsi:type="DataExtensionObject">
                         <PartnerKey xsi:nil="true"/>
                         <ObjectID xsi:nil="true"/>
                         <CustomerKey>Definition_Key</CustomerKey>
                         <Properties>
                              <Property>
                                   <Name>EmailAddress</Name>
                                   <Value>johndoe@example.com</Value>
                        </Property>
                        <Property>
                        <Name>FirstName</Name>
                           <Value>John</Value>
                        </Property>
                        <Property>
                           <Name>LastName</Name>
                           <Value>Doe</Value>
                        </Property>
                        <Property>
                           <Name>PrimaryKey</Name>
                           <Value>PrimaryKey</Value>
                        </Property>
                    </Properties>
                 </Objects>
        </CreateRequest>
    </soap:Body>
</soap:Envelope>

For 2 - If instead of SSJS you want to go via the API route, you would need to create your TSD (also via SSJS or via API) inside of the UI. Then you just send an API call or SSJS function to send it to the desired recipient.

SSJS: TSD

var triggeredSend = TriggeredSend.Init("triggeredSend");
var status = triggeredSend.Send("aruiz@example.com", {FirstName:"Angel", CouponCode:"AA1AF"});

REST API: TSD (There is also a SOAP Object that you can use if you want)

Host: https://www.exacttargetapis.com
POST /messaging/v1/messageDefinitionSends/key:external_key/send
Content-Type: application/json
Authorization: Bearer YOUR_ACCESS_TOKEN

{
    "From": {
        "Address": "code@exacttarget.com",
        "Name": "Code@"
    },
    "To": {
        "Address": "example@example.com",
        "SubscriberKey": "example@example.com",
        "ContactAttributes": {
            "SubscriberAttributes": {
                "Region": "West",
                "City": "Indianapolis",
                "State": "IN"
            }
        }
    },
    "Options": {
        "RequestType": "ASYNC"
    }
}
  • Hmm I think Data Extensions would be the way to go here, can you confirm we can use them in the SSJS Send functions? I thought this was only for Lists. – Deployment Failure Oct 18 '18 at 13:20
  • Can you clarify the comment above? – Deployment Failure Nov 5 '18 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.