Send a triggered email using AMPscript

Triggered sends allow you to automatically send personalized and timely messages to your subscribers. They are sent to an individual subscriber in response to a subscriber action. Common use cases include sending welcome emails to new subscribers, service case notifications or post-purchase thank you notes to clients. They can be personalized just as any other email sent from Salesforce Marketing Cloud and triggered in various ways, among others, using AMPscript. You can read more about Triggered Sends in Salesforce Marketing Cloud here: Triggered Emails.

Create a Triggered Email Message Interaction

In order to create a Triggered Email Message Interaction, we need all the elements that we would use for a user-initiated email: an email, a sender profile and a list for managing subscriber statuses. Once you have all of the above ready, you can create a Triggered Send:

  • Go to Email Studio > Interactions > Triggered Sends
  • Click on Create
  • Provide a name, select your sender profile, email and list used for subscriber management
  • Save your Triggered Send
  • Check the checkbox on the list of your Triggered Sends and click on “Start/Restart”

Now copy the “External Key” of the Triggered Send you just created, you will need to insert it into the script in the next step.

Trigger the email using AMPscript

In order to trigger the email, we will use the InvokeCreate function, which invokes the Create method on an API object. The following script uses the CreateObject function to create the TriggeredSend and TriggeredSendDefinition objects as @ts and @tsDef, and to create a new Subscriber record as @ts_sub. We will specify the attributes of the Triggered Send object and the Subscriber object using the SetObjectProperty function. The AddObjectArrayItem function is used to add the object attributes, and everything is then passed into the InvokeCreate function. The InvokeCreate function returns a status code and a status message, by which exception handling can be built using the RaiseError function.

In the above script, replace ExternalKey with the External Key of the Triggered Send you created earlier and replace with your email address. You are now ready to run this script on a CloudPage by publishing it. Upon success, you should be able to see the following values:

Triggered Send status code: OK
Triggered Send status message: Created TriggeredSend
Triggered Send error code: 0

The above script is simplified, as it uses the email address of a subscriber as the subscriber key. If you would like to pass the subscriber key separately, you will need to add one more variable @ts_subkey and change the SetObjectProperty for the Subscriber record:

To see how this script works in action, visit the CloudPage that I created and submit your email address here.

The delivery speed for Triggered Send emails is usually quicker than for user-initiated emails, so if the email doesn’t make it into your inbox in a couple of minutes, check your spam folder.

Here are additional resources to learn more about Triggered Sends:

2 thoughts on “Send a triggered email using AMPscript

  1. Hi Zuzanna,

    I’m a subscriber to your email publication and we are also connected on LinkedIn. I’m trying to build a triggered send in MC for the first time. I saw your article on “Send a triggered email using AMPscript”. I have created a cloud page and a TS Definition in MC. The form also has five fields which we need to capture from a visitor / lead on the site. I believe we will need to add 5 variables for these in the form to capture their values. My questions are :
    1. where in the html should I put the ampscript code for the triggered send ?
    2. Also if I already have a triggered send definition do I need the part in the code where it creates the objects:
    SET @ts = CreateObject(“TriggeredSend”)
    SET @tsDef = CreateObject(“TriggeredSendDefinition”)
    3. Will we need to add 5 more variables for the 5 capture fields ? the five field are :
    First Name, Last Name, E-mail, Job Title, Company Name & Industry.

    4. Job title & Industry are picklists on the original web form on the client’s website, so how do we create or what do we use too create picklists in a cloud page?

    Very new to ampscript too. trying to learn. Please help.




  2. Hi Girish and thank you for your questions! I will answer them in the same order:
    1. This doesn’t make too much difference, but if you’re building a form which posts to itself, then it makes sense to put the AMPscript for the Triggered Send in the beginning, before the form and add a condition to resolve the script only after the form has been submitted. AMPscript is procedural, which means that it will resolve top-down, in the order that you structure it. This article might help you understand how to structure your script on a CloudPage with a form:
    2. The terminology used by AMPscript creators is very misleading, and I guess most of us it, even though it doesn’t make too much sense: the CreateObject function does not create a new object, but rather, it instantiates the specified Marketing Cloud API Object. So the answer is yes, you do need this part of the script for your Triggered Send to work.
    3. This depends on whether you are planning to use those in the triggered email or whether those fields are required in your database – I guess this is more of a business requirement question. If you need them for the email (well, you will need the email address at least), then do include them in the form. In the email, you can reference them using the AttributeValue() function, for example %%=v(AttributeValue(“firstname”))=%%. Make sure you plan for exception handling when creating a personalized email and always include a fallback version.
    4. You would use the regular HTML form features in a CloudPage. Take a look at the tag:

    Hope this helps!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s