How to send an email with a coupon to your users?

This tutorial will show you how to send a coupon to your customers when a specific event is triggered using the MoonMail SDK. MoonMail Event-based campaigns are a powerful way to send a message to those of your customers who trigger a specific event. 


  1. Design your event
  2. Create your audience
  3. Create your event-based campaign
    1. Add campaign details
    2. Choose your audience
    3. Create your message
    4. Choose when to send the campaign
    5. Review and launch
  4. Report events in your application using the MoonMail SDK
  5. Conclusion

1. Design your event

In the first place, you need to give a name to the event you're going to send with the MoonMail SDK. As you're going to send to your customers a discount coupon, you might call it just COUPON.
When the COUPON event is sent you'll also need to send a couple of user attributes for the contact who is going to receive it. Those user attributes are  Firstname and  Coupon so you can customize the email body your event-based campaign is going to send.

2. Create your audience

In this step, you'll create a dynamic segment for the contacts you'll add to your account with the MoonMail SDK.

There are two steps involved in creating a dynamic segment with all your present and future contacts. First, you set up the segment. Next, you set up the segment groups for the segment.

To create a segment

  1. Sign in to your MoonMail account.
  2. Choose the Segments section. The Segments page opens and displays segments that you previously defined.
  3. Choose Create segment.
  4. Under Create a segment, choose Build a segment.
  5. For Segment name, type a name for the segment to make it easy to recognize later like All Contacts.

Configure the segment groups

  1. Under Segment group 1, next to Include contacts that are in, choose all.
  2. Next to of the following segments, choose no base segment at all to identify all the contacts in your account.
  3. When you finish setting up the segment, choose Create segment.

By completing these steps, you've created a dynamic segment will all the existing contacts in your MoonMail account. But also, all the contacts you create in the future in your account will be available in this segment too.

3. Create your event-based campaign

In this step, you'll create an event-based campaign that will react to the events you send to the newly created contacts, assigned to the previous segment, with the MoonMail SDK.

To create a campaign

  1. Choose Create campaign in the Campaigns section.

Step 1: Add campaign details

  1. Choose the Event-based campaign type.
  2.  On the Campaign details page, for Campaign name enter a descriptive name for the campaign.
  3. Optionally add a description in the Campaign description. Using a description makes it easier to find or search for the campaign later.
  4. Choose Next.
Step 2: Choose your audience
  1. In this step, you have to choose a campaign sub-type. Choose the Standard campaign to send a campaign with a single message.
  2. On the Campaign audience page, choose the All Contacts Segment you created previously to send your campaign to the contacts that are going to be created for this segment. 
  3. Choose  Next.
Step 3: Create your message
In this step, you'll write an email message customized with the contact's attributes sent with the MoonMail SDK. This email will be received by the contact created with the SDK. 
On the Create your message page, do the following:
  1. For From address, enter the from address for your email message. You must verify a domain in your MoonMail account to prove that you own it before you can use email addresses on that domain as a "From address".
  2. For Subject, enter the subject line for your email message, for example, Your discount coupon is waiting.
  3. For  HTML Body, enter the email body. We'll use dynamic variables to access the attributes of the contact sent with the MoonMail SDK. For example, 
  4. Hello {{UserAttributes.Firstname}}, 
    Use this coupon to get a 10% off in your next purchase: {{UserAttributes.Coupon}}
  5. Choose Next.

Step 4: Choose when to send the campaign

In this step, you have to schedule the campaign and choose the event that will trigger it.

  1. For Choose an event that will trigger the campaign, choose the name of the event that triggers the execution of the campaign. For this tutorial type COUPON as the event type that will trigger this campaign.
  2. Under Time range when campaign is active, choose a start date. MoonMail sends the campaign only if the event that you specified earlier occurs after the start date.
  3. Note the Start date and time that you choose has to be at least one minute in the future.
  4. For the End date, choose an end date. MoonMail sends the campaign only if the event that you specified earlier occurs before the end date.
  5. Choose Next.

Step 5: Review and launch

This is the final step to create a campaign.
  1. On the Review and launch page, review the settings for the campaign. If you need to make changes, use the navigation section on the left side of the window to go directly to the page that contains the content that you want to edit.
  2. A spam checking process is run automatically in the background. If the message is considered to be spam you will need to make some changes in Step 3: Create your message.
  3. If all the settings are correct and the message is not considered to be spam, choose Activate campaign.

4. Report events in your application using the MoonMail SDK

To report the events in your application you need to use the MoonMail SDK. To start doing so create a Node.js project with the following commands.

npm init -y 
npm install moonmail-js-sdk --save-dev

After that, you need to add a script.js file to your project with the following content. 

const MoonMail = require("moonmail-js-sdk");

(async () => {
  // Step 1: Setup the SDK with your MoonMail account ID
  const api = MoonMail.init("<YOUR-ACCOUNT-ID>");

  // Step 2: Setup the contact
  const Contact = {
    Address: "",  // <-- YOUR-CUSTOMER-EMAIL-ADDRESS
    UserAttributes: {
      Firstname: "John",
      Coupon: "ABC123"

  // Step 3: Setup the event
  const Events = [
      EventType: "COUPON" 

  // Step 4: Send the data to your MoonMail account
  await api.putEvents({ Contact, Events });

You can see four steps in the script:

  1. Setup the SDK with your MoonMail account ID. The first step initializes the SDK with your account ID. You can find it in your account settings.
  2. Setup the contact. In this step, you're going to create the Contact object you're going to create or update in your MoonMail account and its attributes. In the Address key, input the email address of your customer. Also, in UserAttributes input the keys that you need to customize your email campaign, i.e., Firstname and Coupon.
  3. Setup the event. In this step, you're going to create the Events array. In this array create an object with the EventType key with the name of the event you want to be triggered, i.e., COUPON.
  4. Send the data to your MoonMail account. With the putEvents method, you can send to your MoonMail account the information about the contacts and events you've set up.

Finally, just run the script:

node script.js

5. Conclusion

If you've followed correctly the instructions your customer will receive the following email when you trigger the COUPON event with the MoonMail SDK.

Hello John,   

Use this coupon to get a 10% off in your next purchase: ABC123
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us