In this post, you will learn how to send emails using the Zoho SMTP server using node.js.
We gonna use zeptomail
npm module to send emails using Zoho STMP servers. So let’s install it first. Using the npm install command.
$ npm install zeptomail
Here is the pakcage.json
{
"type": "module",
"dependencies": {
"zeptomail": "^3.0.3"
}
}
Now create a file called sendEmailWithzoho.js with the below code and don’t forget to update the token (see the steps below to generate), to address from address and bounce_address.
// https://www.npmjs.com/package/zeptomail
// For ES6
import { SendMailClient } from "zeptomail";
// For CommonJS
// var { SendMailClient } = require("zeptomail");
const url = "api.zeptomail.com/";
const token = "Zoho-enczapikey xxxxxxxxxxxxxx";
let client = new SendMailClient({url, token});
client.sendMail({
"bounce_address": "[email protected]",
"from":
{
"address": "[email protected]",
"name": "noreply"
},
"to":
[
{
"email_address":
{
"address": "[email protected]",
"name": "Arjun"
}
}
],
"subject": "Test Email",
"htmlbody": "<div><b> Test email sent successfully.</b></div>",
}).then((resp) => console.log("success")).catch((error) => console.log("error"));
Now run your script and If everything goes well you should get an email similar to below in your inbox.
$ node sendEmailWithzoho.js
Steps to generate Send mail token:
- Navigate to the Mail Agents tab on the left panel of your ZeptoMail account and select a Mail Agent that you want to connect to your application.
- On the top right section, click on Setup Info (gear icon). This will open the Setup Details page.
- From here you can copy your Mail Agent-specific send mail token under the API tab.
- You can use the default Send Mail Token or generate a new one.