Skip to content

mailtarget/mailtarget-nodejs-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mailtarget NodeJs SDK

The Mailtarget NodeJs SDK enable NodeJs developer to work with Mailtarget API efficiently.

Getting Started

Requirement

Make sure you have LTS version of NodeJs, minimum NodeJs v14

Installation

To install the SDK to your project, you could get the package from NPM via following command.

# NPM
npm install @mailtarget/nodejs-sdk

# Yarn
yarn add @mailtarget/nodejs-sdk

# PNPM
pnpm add @mailtarget/nodejs-sdk

Authentication

You have to create a mailtarget account to get an API Key. To manage your API Key navigate to the page by clicking Configuration on the navbar in the mailtarget dashboard and then select API Key.

Setup Client

Basic Usage

const { Layang } = require('@mailtarget/nodejs-sdk');

const sender = { email: '[email protected]', name: 'John Doe' };
const subject = 'mailtarget Email Test';
const bodyHtml = `
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Layang</title>
    </head>
    <body style="font-family: "Roboto", "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif">
      <h1>Hello World!!!</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
      <br/><br/>
      <p style="color: #8A8A8A">powered by <a target="_blank" href="https://mailtarget.co" style="color: #8A8A8A">mailtarget</a></p>
    </body>
    </html>
`;
const to = [
  { email: '[email protected]', name: 'Jane Doe' },
];

const sendMail = async () => {
  const mailtarget = new Layang('mailtarget-api-key', sender);
  const transmissionId = await mailtarget.sendMessage(subject, to, bodyHtml);
  if (transmissionId) {
    console.log('Email sent, ID = ', transmissionId);
  } else {
    console.error(mailtarget.getErrorMessage());
  }
};

sendMail();

Using Email Template

If you already using mailtarget Template Creator, you could use that template to send an email by invoking sendMessageTempalate function and pass the templateId as a parameter. Here is an example :

const { Layang } = require('@mailtarget/nodejs-sdk');

const sender = { email: '[email protected]', name: 'John Doe' };
const subject = 'mailtarget Email Test';
const templateId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
const to = [
  { email: '[email protected]', name: 'Jane Doe' },
];

const sendMail = async () => {
  const mailtarget = new Layang('mailtarget-api-key', sender);
  const transmissionId = await mailtarget.sendMessageTemplate(subject, to, templateId);
  if (transmissionId) {
    console.log('Email sent, ID = ', transmissionId);
  } else {
    console.error(mailtarget.getErrorMessage());
  }
};

sendMail();

If you are need to substitute a text using a data you provided (of course you need to set the template accordingly in mailtarget), you could declare it as the fourth parameter of sendMessageTemplate :

...
const substituteData = { lastname: 'Doe', birthdat: '1 January 1975' };
await mailtarget.sendMessageTemplate(subject, to, templateId, substituteData);
...

Optional Configuration

Here is a list of configuration you could make on your email (just make sure you declare it before sendMessag or sendMessageTemplate invocation) :

Carbon Copy (CC)

const cc = [
  { email: '[email protected]', name: 'John Doe' }
];
mailtarget.cc = cc;

Blind Carbon Copy (BCC)

const bcc = [
  { email: '[email protected]', name: 'John Doe' }
];
mailtarget.bcc = bcc;

Body Text

mailtarget.bodyText = 'Your text went here';

Headers

const headers = [
  { name: 'x-header', value: 'your header value' }
];
mailtarget.headers = headers;

Attachment

const attachment = [
  { mimeType: 'application/octet-stream', filename: 'your_file.pdf', value: 'some-value' }
];
mailtarget.attachment = attachment;

Metadata

const metadata = [
  { metaKey: 'meta-value' }
];
mailtarget.metadata = metadata;

Click Tracking

Track a click activity of an email you sent, default set to active. If you prefer it to be disabled, use following line :

mailtarget.setClickTracking(false);

Open Tracking

Track an open activity of an email you sent, default set to active. If you prefer it to be disabled, use following line :

mailtarget.setOpenTracking(false);

Transactional

Treat the email as transactional, default set to active. If you prefer it to be disabled (i.e you want to send a newsletter which has unsubscribe link on it), use following line :

mailtarget.setTransactional(false);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •