Skip to content

Send email using web API provider

Steve Lebleu edited this page Feb 25, 2024 · 4 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

> Using provider template

Configure .cliamrc.js

In cliamrc.js, define your transporter like this:

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

Adapt your input, fire transaction and send email:

import { Cliam } from 'cliam';

// Do some stuffs ...

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

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

Configure .cliamrc.js:

In cliamrc.js, define your transporter like this:

    {
      "id": "provider-api",
      "provider": "mailgun",
      "auth": {
        "apiKey": "API_KEY"
      },
    },

Adapt your input, fire transaction and send email:

import { Cliam } from 'cliam';

// Do some stuffs ...

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

Configure .cliamrc.js:

In cliamrc.js, define your transporter like this:

    {
      "id": "provider-api",
      "provider": "mailgun",
      "auth": {
        "apiKey": "API_KEY"
      },
    },

Adapt your input, fire transaction and send email:

import { Cliam } from 'cliam';

// Do some stuffs ...

const payload = {
  transporterId: 'provider-api',
  renderEngine: 'cliam',
  meta: {...},
  data: {
    user: { 
      username: 'John Doe',
      email: '[email protected]'
    },
    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