Skip to content

Send email using web API provider

Steve Lebleu edited this page Apr 25, 2026 · 5 revisions

This section deescribes the email sending using web API provder through concrete examples.

Most common use case is email sending with your API provider. For all examples below, we start with the idea to send a welcome email.

> Table of contents

> Configure your provider

In cliamrc.js, define your transporter as usual. In addition, if you want to use the templates of your API provider, you must define a templates mapping between a transactional event (key) and the provider template id (value):

{
  "id": "provider-api",
  "provider": "mailgun",
  "auth": {
    "apiKey": "API_KEY"
  },
  "templates": {
    "default": "default", 
    "user.welcome": "welcome",
  }
}

> Send email

> Using provider template

Target your configuration using transporterId property, adapt your input using a data attribute:

import { Cliam } from 'cliam';

const payload = {
  transporterId: 'provider-api',
  meta: {...},
  data: {...}
};

Trigger email using the event key related to the template you want to use:

Cliam.mail('user.welcome', payload)
  .then(res => {
    console.log('Email has been delivered: ', res);
  })
  .catch(err => {
    console.log('Error while mail sending: ', err)
  });

> Using your own template

Adapt simply your input to use content property, and send email:

import { Cliam } from 'cliam';

const payload = {
  transporterId: 'provider-api',
  renderEngine: 'self',
  meta: {...},
  content: [
    { type: 'text/html', value: '<html><p>Your content</p></html>' }
  ]
};
  
Cliam.mail('user.welcome', payload)
  .then(res => {
    console.log('Email has been delivered: ', res);
  })
  .catch(err => {
    console.log('Error while mail sending: ', err)
  });

The content property accept an object array as value:

  • Min 1 object with type text/html.
  • Max 2 objects (type text/html and type text/plain).
  • Type text/plain is optionnal and will be generated from text/html if not present.

> Using cliam template

Adapt your input to use data property without a valid transactional event or just without to provide any template nor content, and send email:

import { Cliam } from 'cliam';

const payload = {
  transporterId: 'provider-api',
  renderEngine: 'cliam',
  meta: {...},
  data: {
    user: { 
      username: 'John Doe',
      email: 'john.doe@example.com'
    },
    message: 'Hi John ! \n\n Welcome on board !',
    cta: {
      label: 'Go to your profile',
      link: 'https://www.example.com/profile'
    }
  }
};
  
Cliam.mail('user.welcome', payload)
  .then(res => {
    console.log('Email has been delivered: ', res);
  })
  .catch(err => {
    console.log('Error while mail sending: ', err)
  });

See transactions definitions to know more about available transactions and associated inputs.

Discuss on Slack

Clone this wiki locally