Send an email with a TriggeredSend Definition verified

Learn how to send an email using a Triggered Send Definition in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include WSProxy, Core and REST API methods.

Core

var config = {
    TriggeredSendDefinition: 123456,
    Attributes : {
        FirstName: "John",
        Language: "EN"
    },
    Subscriber : {
        EmailAddress: "example@mail.com",
        SubscriberKey: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
    }
}

var tsd = TriggeredSend.Init(config.TriggeredSendDefinition);

var result = tsd.Send(config.Subscriber, config.Attributes);
<script runat="server">

    Platform.Load("core", "1");
	
	try {

        var config = {
            TriggeredSendDefinition: 123456,
            Attributes : {
                FirstName: "John",
                Language: "EN"
            },
            Subscriber : {
                EmailAddress: "example@mail.com",
                SubscriberKey: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
            }
        }

        var tsd = TriggeredSend.Init(config.TriggeredSendDefinition);
        
		var result = tsd.Send(config.Subscriber, config.Attributes);

        Write(Stringify(result));
		
	} catch(error) {

        Write(Stringify(error));
        
    }	
    
</script>
"OK"

WSProxy

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

var config = {
    TriggeredSendDefinition: {
        CustomerKey: 123456
    },
    Subscribers: [
        {
            EmailAddress: "example@mail.com",
            SubscriberKey: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
            Attributes: [
                {
                    Name: "FirstName",
                    Value: "John"
                },
                {
                    Name: "LastName",
                    Value: "Doe"
                }
            ]
        }
    ]
};

var result = api.createItem('TriggeredSend', config);
<script runat="server">

    Platform.Load("core", "1");

    var api = new Script.Util.WSProxy();
	
	try {

        var config = {
            TriggeredSendDefinition: {
                CustomerKey: 123456
            },
            Subscribers: [
                {
                    EmailAddress: "example@mail.com",
                    SubscriberKey: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                    Attributes: [
                        {
                            Name: "FirstName",
                            Value: "John"
                        },
                        {
                            Name: "LastName",
                            Value: "Doe"
                        }
                    ]
                }
            ]
        };

        var result = api.createItem('TriggeredSend', config);

        Write(Stringify(result));
		
	} catch(error) {

        Write(Stringify(error));
        
    }	
    
</script>
{
    "Status": "OK",
    "RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    "Results": [
        {
            "SubscriberFailures": [],
            "NewID": 0,
            "NewObjectID": null,
            "PartnerKey": null,
            "Object": null,
            "CreateResults": null,
            "ParentPropertyName": null,
            "StatusCode": "OK",
            "StatusMessage": "Created TriggeredSend",
            "OrdinalID": 0,
            "ErrorCode": 0,
            "RequestID": null,
            "ConversationID": null,
            "OverallStatusCode": null,
            "RequestType": "Synchronous",
            "ResultType": null,
            "ResultDetailXML": null
        }
    ]
}

REST API

var externalKey = "123456";

var payload = {
    "From": {
        "Address": "example@mail.com",
        "Name": "My Account"
    },
    "To": {
        "Address": "example@mail.com",
        "SubscriberKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
        "ContactAttributes": {
            "SubscriberAttributes": {
                "FirstName": "John",
                "LastName": "Doe"
            }
        }
    }
};

var endpoint = restInstanceUrl + "/messaging/v1/messageDefinitionSends/key:" + externalKey + "/send";

var request = HTTP.Post(endpoint, "application/json", Stringify(payload), ["Authorization"], ["Bearer " + accessToken]);

var result = Platform.Function.ParseJSON(String(request.Response));
<script runat="server">

    Platform.Load("core", "1");

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

    var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
        accessToken     = "YOUR_REST_API_TOKEN";
	
    try {

      var externalKey = "123456";

      var payload = {
          "From": {
              "Address": "example@mail.com",
              "Name": "My Account"
          },
          "To": {
              "Address": "example@mail.com",
              "SubscriberKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
              "ContactAttributes": {
                  "SubscriberAttributes": {
                      "FirstName": "John",
                      "LastName": "Doe"
                  }
              }
          }
      };

      var endpoint = restInstanceUrl + "/messaging/v1/messageDefinitionSends/key:" + externalKey + "/send";

      var request = new Script.Util.HttpRequest(endpoint);
          request.emptyContentHandling = 0;
          request.retries = 2;
          request.continueOnError = true;
          request.setHeader("Authorization", "Bearer " + accessToken);
          request.method = "POST";
          request.contentType = "application/json";
          request.postData = Stringify(payload);

      var results = request.send();

      var result = Platform.Function.ParseJSON(String(results.content));

      Write(Stringify(result));
      
    } catch(error) {

      Write(Stringify(error));
        
    }	

</script>
{
  "requestId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
  "responses": [
    {
      "recipientSendId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
      "hasErrors": false,
      "messages": [
        "Queued"
      ]
    }
  ]
}

WARNING

In payload, From refers to the existing Sender Profile data. Send error will occur when the From data doesn't exist in the implementation.

Reference

Ressources and references related to the current methods.

Official documentation
SOAP object

Last Updated: