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.

%%[
VAR @ts, @tsDef, @ts_extkey, @ts_email, @ts_sub, @ts_statusCode, @ts_statusMsg, @errorCode
SET @ts = CreateObject("TriggeredSend")
SET @tsDef = CreateObject("TriggeredSendDefinition")
SET @ts_extkey = "ExternalKey"
SET @ts_email = "email@example.com"
SetObjectProperty(@tsDef, "CustomerKey", @ts_extkey)
SetObjectProperty(@ts, "TriggeredSendDefinition", @tsDef)
SET @ts_sub = CreateObject("Subscriber")
SetObjectProperty(@ts_sub, "EmailAddress", @ts_email)
SetObjectProperty(@ts_sub, "SubscriberKey", @ts_email)
AddObjectArrayItem(@ts, "Subscribers", @ts_sub)
SET @ts_statusCode = InvokeCreate(@ts, @ts_statusMsg, @errorCode)
IF @ts_statusCode != "OK" THEN
RaiseError(@ts_statusMsg, 0, @ts_statusCode, @errorCode)
ENDIF
]%%

In the above script, replace ExternalKey with the External Key of the Triggered Send you created earlier and replace email@example.com 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:

%%[
VAR @ts, @tsDef, @ts_extkey, @ts_email, @ts_sub, @ts_statusCode, @ts_statusMsg, @errorCode
SET @ts = CreateObject("TriggeredSend")
SET @tsDef = CreateObject("TriggeredSendDefinition")
SET @ts_extkey = "ExternalKey"
SET @ts_email = "email@example.com"
SET @ts_subkey = _subscriberkey
SetObjectProperty(@tsDef, "CustomerKey", @ts_extkey)
SetObjectProperty(@ts, "TriggeredSendDefinition", @tsDef)
SET @ts_sub = CreateObject("Subscriber")
SetObjectProperty(@ts_sub, "EmailAddress", @ts_email)
SetObjectProperty(@ts_sub, "SubscriberKey", @ts_subkey)
AddObjectArrayItem(@ts, "Subscribers", @ts_sub)
SET @ts_statusCode = InvokeCreate(@ts, @ts_statusMsg, @errorCode)
IF @ts_statusCode != "OK" THEN
RaiseError(@ts_statusMsg, 0, @ts_statusCode, @errorCode)
ENDIF
]%%

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:

20 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.

    Regards,

    Girish

    Like

  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: https://sfmarketing.cloud/2019/09/22/create-a-sales-service-cloud-integrated-lead-capture-form-using-ampscript/
    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: https://www.w3schools.com/html/html_form_elements.asp

    Hope this helps!
    Zuzanna

    Like

  3. Peter

    Hi,

    I’m using the above code and it works which is amazing!(never knew i could do this in AMPScript.

    what if I want to add additional attributes to the triggered send DE so I can include it to the email how can I do this with the above code?

    Like

  4. Hi there,

    This is really helpful thanks. I have a question about this bit:
    SetObjectProperty(@tsDef, “CustomerKey”, @ts_extkey)

    What is the CustomerKey?

    I get this error: Error Message: “Invalid Customer Key””ExactTarget.OMM.AMPScriptRaiseErrorException: Invalid Customer Key – from Jint\r\n\r\n”

    Not sure why or what I can replace it with?

    Like

  5. Darshan

    The below form is submitted but data is not added in Trigger DE and Trigger Email is also not sent.
    Not sure what is going wrong?

    %%[
    VAR @ts, @tsDef, @ts_extkey, @ts_email, @ts_sub, @ts_statusCode, @ts_statusMsg, @errorCode
    if RequestParameter(“submitted”) == “submitted” then
    SET @ts = CreateObject(“TriggeredSend”)
    SET @tsDef = CreateObject(“TriggeredSendDefinition”)
    SET @ts_extkey = “12188314”
    SET @ts_email = RequestParameter(“EmailAddress”)
    SET @ts_subkey = _subscriberkey

    SetObjectProperty(@tsDef, “CustomerKey”, @ts_extkey)
    SetObjectProperty(@ts, “TriggeredSendDefinition”, @tsDef)

    SET @ts_sub = CreateObject(“Subscriber”)
    SetObjectProperty(@ts_sub, “EmailAddress”, @ts_email)
    SetObjectProperty(@ts_sub, “SubscriberKey”, @ts_subkey)
    AddObjectArrayItem(@ts, “Subscribers”, @ts_sub)

    SET @ts_statusCode = InvokeCreate(@ts, @ts_statusMsg, @errorCode)
    IF @ts_statusCode != “OK” THEN
    RaiseError(@ts_statusMsg, 0, @ts_statusCode, @errorCode)
    ENDIF

    ENDIF
    ]%%

    Email Address

    Like

  6. Hi Zuzanna, I used this code and it’s working, however in the triggered send DE it’s adding a record with SubscriberKey and EmailAddress as the email mentioned here:

    SET @ts_email = “abc@MyDomain.com”

    Also, it’s sending the auto email to this ID itself. It should capture the SubscriberKey and EmailAddress from the form field values and the auto email should go to the email mentioned by the user.

    Like

  7. Pingback: OPT (One-Time Password) flow using Triggered Sends and CloudPages – sfmarketing.cloud

  8. Mario

    Hi Zuzanna,

    Thanks for the post! im trying to use your code and update with the correct key for triggered send definition and email address.

    But, as soon as i publish it gives me this error in cloud page:

    Error Message: “CreateObject Function is not valid in content. This function is only allowed in non sendable content. Function: CreateObject(\”TriggeredSend\”)”

    Description: “ExactTarget.OMM.FunctionExecutionException: CreateObject Function is not valid in content. This function is only allowed in non sendable content. Function: CreateObject(\”TriggeredSend\”) Error Code: OMM_FUNC_CONTEXT_ERR – from Jint ”

    Do you know what this causing?

    Thanks,
    Mario

    Like

  9. Mario

    Hi Zuzanna,

    My previous attempt did trigger successfully, which is a positive outcome. However, I’m now working with a Smart Capture form and planning to use AMPscript to trigger a confirmation email from another CloudPage – specifically, the “Thank You” page.

    Can you provide any guidance on how I can pass the submitted email address from the Smart Capture form to the “Thank You” page? “Thank You” page is where I have the AMPscript setup for the triggered sends.

    Thank you.
    Mario

    Like

  10. Pingback: Build an Application Portal With Marketing Cloud & Cloudpages | Salesforce Ben

Leave a comment