How to send transactional emails using ZeptoMail in Node.js
- Published : March 27, 2024
- Last Updated : November 28, 2024
- 1.0K Views
- 4 Min Read
Transactional emails like password resets, account notification emails, invoices, reports, and more, are an important part of any business. Sending out transactional emails at the required time also seals the trust your customers have in your brand. Because some of these emails, like security codes or financial statements, contain crucial and sensitive information, there’s a need for a reliable and secure service.
This is where services like Zoho ZeptoMail come in. ZeptoMail ensures hassle-free email delivery from your application or website. This article will focus on transactional email delivery from your Node.js application.
Pre-requisites
Node.js application
Zoho ZeptoMail account
Steps to install ZeptoMail
To install the ZeptoMail npm package, type the following command in your system terminal:
npm install zeptomail
The next step is to obtain your API key. Follow the steps mentioned here to obtain your Send Mail Token (API key) from your ZeptoMail account.
If you’re using the ES6 module, set type as: "module" in package.json. Use the following:
import { SendMailClient } from "zeptomail";
If you’re using CommonJs module, use the following:
var { SendMailClient } = require("zeptomail");
To start sending out emails
// For ES6, set type: "module" in package.json.
import { SendMailClient } from "zeptomail";
// For CommonJS
var { SendMailClient } = require("zeptomail");
const url = "api.zeptomail.com/";
const token = "";
let client = new SendMailClient({url, token});
client.sendMail({
"from":
{
"address": "test@example.com",
"name": "test"
},
"to":
[
{
"email_address":
{
"address": "test1@example.com",
"name": "test1"
}
}
],
"reply_to":
[
{
"address": "test2@example.com",
"name": "test2"
}
],
"subject": "Sending with ZeptoMail to have good experience",
"textbody": "Easy to do from anywhere, with Node.js",
"htmlbody": "Easy to do from anywhere, with Node.js",
"cc":
[
{
"email_address":
{
"address": "test3@example.com",
"name": "test3"
}
}
],
"bcc":
[
{
"email_address":
{
"address": "test4@example.com",
"name": "test4"
}
}
],
"track_clicks": true,
"track_opens": true,
"client_reference": "",
"mime_headers":
{
"X-Zylker-User": "test-xxxx"
},
"attachments":
[
{
"content": "..x+SEXa3zKfnDnBA2qExtXikBpUAFABQAhGSPY/0IoAWgD/2Q==",
"mime_type": "image/jpg",
"name": "DM-welcome-guide"
},
{
"file_cache_key": "",
"name": "DM-reports"
}
],
"inline_images":
[
{
"mime_type": "image/jpg",
"content": "...x+SEXa3zKfnDnBA2qExtXikBpUAFABQAhGSPY/0IoAWgD/2Q==",
"cid": "img-welcome-design"
},
{
"file_cache_key": "",
"cid": "img-CTA"
}
]
}).then((resp) => console.log("success")).catch((error) => console.log("error"));
To send emails using templates
You can use preset templates to send emails from your Node.js application. To send these emails, you should generate the template key from your ZeptoMail account. Follow the steps below:
In your ZeptoMail account, navigate to the desired Mail Agent.
Go to the templates tab and copy the template key.
To copy the template key alias, select the template and go to the edit templates section.
Navigate to the edit templates section and copy the key alias.Use the code to send out emails from your node.js application.
// For ES6, set type: "module" in package.json.
import { SendMailClient } from "zeptomail";
// For CommonJS
var { SendMailClient } = require("zeptomail");
const url = "api.zeptomail.com/";
const token = "";
client.sendMailWithTemplate({
"template_key": "
< template key="" >",
"template_alias": "< template's alias="" >",
"from": {
"address": "test@example.com",
"name": "test"
},
"to": [
{
"email_address": {
"address": "test1@example.com",
"name": "test1"
}
}
],
"cc": [
{
"email_address": {
"address": "test3@example.com",
"name": "test3"
}
}
],
"bcc": [
{
"email_address": {
"address": "test5@example.com",
"name": "test5"
}
}
],
"merge_info": {
"contact_number": "8787xxxxxx789",
"company": "example.com"
},
"reply_to": [
{
"address": "test7@example.com",
"name": "test7"
}
],
"client_reference": "",
"mime_headers": {
"X-Test": "test"
}
}).then((resp) => console.log("success")).catch((error) => console.log("error"));
To send batch emails
Batch emails consist of a single email content sent to multiple users. An example of this is the webinar registration email sent out to the participants. Use to the code below to send out batch emails from your node.js application:
// For ES6, set type: "module" in package.json.
import { SendMailClient } from "zeptomail";
// For CommonJS
var { SendMailClient } = require("zeptomail");
const url = "api.zeptomail.com/";
const token = "";
client.sendBatchMail({
"from": {
"address": "test@example.com",
"name": "test"
},
"to": [
{
"email_address": {
"address": "test1@example.com",
"name": "test1"
},
"merge_info": {
"name": "Micheal scott"
}
}
],
"reply_to":
[
{
"address": "test2@example.com",
"name": "test2"
}
],
"subject": "Sending with ZeptoMail to have good experience",
"textbody": "Hi {{name}}, sending email is easy to do from anywhere, with Node.js",
"htmlbody": "Hi {{name}},
Sending email is easy to do from anywhere, with Node.js",
"cc":
[
{
"email_address":
{
"address": "test3@example.com",
"name": "test3"
}
}
],
"bcc":
[
{
"email_address":
{
"address": "test4@example.com",
"name": "test4"
}
}
],
"track_clicks": true,
"track_opens": true,
"client_reference": "",
"mime_headers":
{
"X-Zylker-User": "test-xxxx"
},
"attachments":
[
{
"content": "..x+SEXa3zKfnDnBA2qExtXikBpUAFABQAhGSPY/0IoAWgD/2Q==",
"mime_type": "image/jpg",
"name": "DM-welcome-guide"
},
{
"file_cache_key": "",
"name": "DM-reports"
}
],
"inline_images":
[
{
"mime_type": "image/jpg",
"content": "...x+SEXa3zKfnDnBA2qExtXikBpUAFABQAhGSPY/0IoAWgD/2Q==",
"cid": "img-welcome-design"
},
{
"file_cache_key": "",
"cid": "img-CTA"
}
]
}).then((resp) => console.log("success")).catch((error) => console.log("error"));
Send batch emails using templates
// For ES6, set type: "module" in package.json.
import { SendMailClient } from "zeptomail";
// For CommonJS
var { SendMailClient } = require("zeptomail");
const url = "api.zeptomail.com/";
const token = "";
client.mailBatchWithTemplate({
"template_key": "",
"template_alias": "",
"from": {
"address": "test@example.com",
"name": "test"
},
"to": [
{
"email_address": {
"address": "test1@example.com",
"name": "test1"
},
"merge_info": {
"contact_number": "8787xxxxxx789",
"company": "example.com"
}
}
],
"reply_to": [
{
"address": "test@example.com",
"name": "test"
}
],
"client_reference": "",
"mime_headers": {
"X-Test": "test"
}
}).then((resp) => console.log("success")).catch((error) => console.log("error"));