Skip to content

kannon-email/kannon.js

Repository files navigation

kannon.js is the official node client library for Kannon Email Sender

Usage

Instantiate kannon cli

const kannon = new KannonCli(
  '<YOUR DOMAIN>',
  '<API KEY>',
  {
    email: '[email protected]',
    alias: 'Kannon',
  },
  {
    host: '<YOU KANNON API HOST>',
  },
);

Basic Usage

async function sendHtml() {
  const html = `...`;

  return await kannon.sendMail(['[email protected]'], 'This is an email from kannon.js', html);
}

Sending Templates

async function sendHtml() {
  const templateId = `...`;

  return await kannon.sendTemplate(['[email protected]'], '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: '[email protected]', fields: { name: 'test 1' } },
    { email: '[email protected]', 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: '[email protected]', fields: { name: 'test 1' } },
    { email: '[email protected]', fields: { name: 'test 2' } }
  ],
  'This is an email from kannon.js',
  html,
  {
    globalFields: { global: 'global value' }
  }
);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published