Repository navigation
Send email using web API provider
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.
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",
}
}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)
});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/htmland typetext/plain). - Type
text/plainis optionnal and will be generated fromtext/htmlif not present.
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.