0.5.0 • Published 1 year ago
kannon.js v0.5.0
kannon.js is the official node client library for Kannon Email Sender
Usage
Instantiate kannon cli
const kannon = new KannonCli(
'<YOUR DONMAIN>',
'<API KEY>',
{
email: 'sender@kannon.dev',
alias: 'Kannon',
},
{
host: '<YOU KANNON API HOST>',
},
);
### Basic Usage
async function sendHtml() {
const html = `...`;
return await kannon.sendMail(['test@email.com'], 'This is an email from kannon.js', html);
}
Sending Templates
async function sendHtml() {
const templateId = `...`;
return await kannon.sendTemplate(['test@email.com'], 'This is an email from kannon.js', templateId);
}
Sending Attachments
const res = await cli.sendHtml(
[
// ...
],
'Send Attachment',
html,
{
attachments: [
{
filename: 'test.txt',
content: Buffer.from('Hello from Kannon!'),
},
],
},
);
Fields and Global Fields
You can customize the html (or the template) per recipient by using fields parameters.
const html = `Hello {{name}}!`;
return await kannon.sendMail(
[
{ email: 'test1@email.com', fields: { name: 'test 1' } }
{ email: 'test2@email.com', fields: { name: 'test 2' } }
],
'This is an email from kannon.js',
html,
);
The text between {{
and }}
will be replaced by the value of the field.
If you want to use the same field for all recipients, you can use the globalFields parameter.
const html = `Hello {{name}}! This is a global field: {{ global }}`;
return await kannon.sendMail(
[
{ email: 'test1@email.com', fields: { name: 'test 1' } }
{ email: 'test2@email.com', fields: { name: 'test 2' } }
],
'This is an email from kannon.js',
html,
{
globalFields: { global: 'global value' }
}
);