The Mailtarget NodeJs SDK enable NodeJs developer to work with Mailtarget API efficiently.
Make sure you have LTS version of NodeJs, minimum NodeJs v14
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
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.
const { Layang } = require('@mailtarget/nodejs-sdk');
const sender = { email: 'usename@yourdomain.com', 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: 'janedoe@recipient-domain.com', 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();
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: 'usename@yourdomain.com', name: 'John Doe' };
const subject = 'mailtarget Email Test';
const templateId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
const to = [
{ email: 'janedoe@recipient-domain.com', 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);
...
Here is a list of configuration you could make on your email
(just make sure you declare it before sendMessag
or sendMessageTemplate
invocation) :
const cc = [
{ email: 'usename@yourdomain.com', name: 'John Doe' }
];
mailtarget.cc = cc;
const bcc = [
{ email: 'usename@yourdomain.com', name: 'John Doe' }
];
mailtarget.bcc = bcc;
mailtarget.bodyText = 'Your text went here';
const headers = [
{ name: 'x-header', value: 'your header value' }
];
mailtarget.headers = headers;
const attachment = [
{ mimeType: 'application/octet-stream', filename: 'your_file.pdf', value: 'some-value' }
];
mailtarget.attachment = attachment;
const metadata = [
{ metaKey: 'meta-value' }
];
mailtarget.metadata = metadata;
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);
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);
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);