zoho smtp mail nodejs
Zoho SMTP Mail with Node.js
If you are developing a web application with Node.js and need to send emails, you can use Zoho SMTP mail service. Zoho provides secure SMTP server settings to send emails from your application. Here is how you can configure Zoho SMTP mail with Node.js:
Step 1: Create a Zoho Mail Account
If you don't have a Zoho mail account, create one by visiting https://www.zoho.com/mail/. Once you have created your account, log in to your Zoho account.
Step 2: Generate an App Password
To send emails using the SMTP server, you need to generate an app password. To generate an app password, follow these steps:
- Go to Zoho Mail Control Panel -> Mail Accounts.
- Select the email account for which you want to generate the app password.
- Click on the Generate New Password button.
- Enter a name for the app password and click on the Generate button.
- Note down the app password, as it will be required later in your Node.js application.
Step 3: Install Nodemailer Module
npm install nodemailer
Step 4: Configure Nodemailer with Zoho SMTP Settings
Now, you need to configure the Nodemailer module with your Zoho SMTP settings. Here's the code:
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: 'smtp.zoho.com',
port: 465,
secure: true,
auth: {
user: 'your-email@zoho.com',
pass: 'your-app-password'
}
});
Replace your-email@zoho.com
with your Zoho email address and your-app-password
with the app password you generated in step 2.
Step 5: Send Email
let mailOptions = {
from: 'your-email@zoho.com',
to: 'recipient-email@example.com',
subject: 'Test Email',
text: 'This is a test email from Node.js using Zoho SMTP.'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
Replace your-email@zoho.com
with your Zoho email address and recipient-email@example.com
with the email address of the recipient.
Alternative Method: Use Zoho SMTP Relay
If you face any issues with the above method, you can use Zoho SMTP relay to send emails. Here's how:
- Go to Zoho Mail Control Panel -> SMTP/IMAP.
- Select Add SMTP Relay button.
- Enter a name for the SMTP relay and click on the Add button.
- Note down the SMTP relay details, as it will be required later in your Node.js application.
Follow steps 3 to 5 using the following code:
let transporter = nodemailer.createTransport({
host: 'your-smtp-relay.zoho.com',
port: 587,
secure: false,
auth: {
user: 'your-email@zoho.com',
pass: 'your-zoho-password'
}
});
Replace your-smtp-relay.zoho.com
with the SMTP relay details you noted down in step 3 and your-zoho-password
with your Zoho mail account password.