Skip to content

Send email using SMTP

Steve Lebleu edited this page Feb 25, 2024 · 4 revisions

This section describes how to send emails using SMTP relay through concrete examples.

> Table of contents

> Using your own template

Configure .cliamrc.js:

In cliamrc.js, define your transporter like this:

{
      "id": "hosting-smtp",
      "auth": {
        "username": "USERNAME",
        "password": "¨PASSWORD"
      },
      "options": {
        "host": "mail.host.com",
        "port": 587,
        "secure": true
      }
    }
}

Adapt your input, fire transaction and send email:

import { Cliam } from 'cliam';

// Do some stuffs ...

const payload = {
  transporterId: 'hosting-smtp',
  renderEngine: 'self',
  meta: {...},
  content: [
    { type: 'text/html', value: '<html><p>Email 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": "hosting-smtp",
      "auth": {
        "username": "USERNAME",
        "password": "¨PASSWORD"
      },
      "options": {
        "host": "mail.host.com",
        "port": 587,
        "secure": true
      }
    }
}

Adapt your input, fire transaction and send email:

import { Cliam } from 'cliam';

// Do some stuffs ...

const payload = {
  transporterId: 'hosting-smtp',
  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.