Marketing automation with intercom — sending emails

Viral Tagdiwala
4 min readJul 7, 2021

Intercom lends a WHOLE plethora of features to use, and I’ve mentioned it before, it’s one of the SaaS products that I totally feel is worth the price!

In this article, we’ll be going through how would you go about implementing a system through which your backend can push out emails via intercom. There can be a whole bunch of upsides of doing this -

  1. Implementing your own email system in a way where it doesn't get flagged as spam is genuinely a tough nut to crack
  2. Having all your user-related data in one place allows you to make decisions faster!
  3. You might just have a complicated piece of logic that can’t be implemented right via intercom’s GUI!

In any case, intercom provides us with a npm package (along with type definitions) that we’ll be using to implement this!

We start off by creating an app under intercom developer hub — https://app.intercom.io/a/apps/_/developer-hub

Once the app is created, you’ll have to copy the auth token located where the red box is placed.

You can opt to save this in your .env or a constants file!

Next up, we need to install the npm package — https://www.npmjs.com/package/intercom-client. Assuming you use yarn and typescript, you’ll be running the following commands

yarn add intercom-clientyarn add @types/intercom-client

Now, we create a sendMail function which can be called from our code base, which essentially acts as an abstraction container for calling the intercom API!

Before creating this sendMail function, we do need to have one thing in place though, and that is the intended target’s email address & even more so, we need to generate a user over on intercom with the SAME email address!

The latter is very very important, since if you don’t have a user with the same email address over on intercom, pushing out emails to them will be impossible.

In case you don’t know how to save a new user, here’s how you go about doing it in your front-end logic (after booting intercom up)

window.Intercom("update", {email: users_email});

Coming back to our backend code, we start off by writing a function to retrieve the users based off their email address

Now this script returns the user’s intercom ID back to our main logic (which we will be coming back to shortly)

Note the “sender” here is essentially an intercom client instance that utilises the token of the app you created in step 1.

Notice how we first need to call our function to retrieve the intercom ID of the user and then using this ID alongside our admin ID we can send an email to the intended user.

In order to retrieve your admin ID, you simply need to open up intercom,

  • Open settings -> General
  • Under general click on team-mates
  • Click on the admin account

Your address bar will now have an address that looks something like this

https://app.intercom.com/a/apps/your_app_id/admins/your_admin_id

All you gotta do now is to simply copy the admin ID and just like we did with the token, save it somewhere safe!

There is one “gotcha” that you need to remember!

An important “gotcha” to remember…

Upon saving a user in intercom, the save isn’t instantaneous i.e. if you generate a new user and send them an email right off the bat, the function to retrieve the user will throw you an error saying the user doesn’t exist since it takes at least 30–45 seconds for the user to show up in the panel and subsequently in API calls!

So if you are designing a system where you push an email immediately after the user is created

  1. You might be better off just using the intercom GUI to do it, since you can easily define a trigger based on this event and fire off an email
  2. Write custom logic to handle cases where the user isn’t yet available and maybe implement a strategy like exponential back-off to keep retrying!

That’s it! You now have a functional system to keep pumping out emails via your backend which then allows you to write more nuanced logic than what you can write just by the GUI provided by intercom!

--

--