Those following my blog know I recently started working on a digital gift card web app development project. In the meantime I’ve found a need for an automated email delivery feature implementation. This article details the process, focusing on the integration of Nodemailer with Zoho SMTP to send gift cards directly to recipients.
The Need for Automated Emails
My plan is to build a Digital Gift Card web application that will send the generated gift card to the email the user submits. I found this approach the best from many perspectives, including efficiency as well as security. I will document the app idea in more detail in an upcoming article. All in all, the app requires a feature to email gift cards upon purchase directly. This enhances the user experience by providing immediate delivery and a digital record of the gift card. To achieve this, I chose Nodemailer, a popular Node.js library for sending emails, and integrated it with Zoho SMTP for reliable delivery.
Why should you use Zoho Mail?
Zoho Mail presents a compelling alternative for users seeking a robust, privacy-focused email solution. Unlike larger, ad-driven providers, Zoho emphasizes user privacy and offers a suite of business-oriented features. It provides a clean, ad-free interface, strong security measures, including two-factor authentication and encryption, and seamlessly integrates with other Zoho applications. For businesses, Zoho Mail offers custom domain support, collaborative tools, and flexible plans, making it a cost-effective and feature-rich option that prioritizes user control and data protection.
Setting Up the Environment
First, I installed Nodemailer:
npm install nodemailer
Then, I configured our .env
file with the necessary Zoho SMTP credentials:
[email protected]
EMAIL_PASS=your_zoho_password
Integrating Nodemailer into the process-payment
Route
The core of the email functionality resides within our process-payment
route. After the gift card image is generated and stored, I added the following Nodemailer configuration:
const nodemailer = require('nodemailer');
// ... (existing code)
// Nodemailer setup for Zoho SMTP
const transporter = nodemailer.createTransport({
host: 'smtp.zoho.eu',
port: 465,
secure: true,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
const mailOptions = {
from: process.env.EMAIL_USER,
to: email, // Recipient's email from the request
subject: 'Your Gift Card',
text: `Dear ${recipient},\n\nHere is your gift card.`,
attachments: [{
filename: imageFileName,
content: giftCardImageBuffer,
encoding: 'base64',
}],
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error('❌ Error sending email:', error);
} else {
console.log('✅ Email sent:', info.response);
}
});
Key Components Explained
nodemailer.createTransport()
:- This creates a transporter object, which handles the email sending process.
host: 'smtp.zoho.eu'
specifies the Zoho SMTP server.port: 465
sets the port for SSL encryption.secure: true
enables SSL.auth
contains the user’s Zoho email credentials from the.env
file.
mailOptions
:- This object defines the email content.
from
specifies the sender’s email address.to
uses the recipient’s email from the request body.subject
sets the email subject.text
provides the email body.attachments
attaches the generated gift card image.filename
sets the attachment’s name.content
provides the image buffer.encoding: 'base64'
ensures the image is correctly encoded.
transporter.sendMail()
:- This method sends the email using the provided
mailOptions
. - It includes a callback function to handle errors and log successful deliveries.
- This method sends the email using the provided
Connecting with Zoho SMTP
- Credentials: Ensure your Zoho credentials are correctly stored in your
.env
file. - Port Selection: Port 465 is used for SSL, which is the secure and recommended option.
- App Passwords (if 2FA is enabled): For enhanced security, generate an app-specific password in your Zoho account and use it in your
.env
file.
How it Works (in my Gift Card app example)
- Gift Card Generation: When a user completes a purchase, the application generates a unique gift card number, creates a visual representation (PNG image), and saves it to the file system and database.
- Nodemailer Integration: After saving the card, the
process-payment
route initializes Nodemailer with the Zoho SMTP configuration. - Email Sending: Nodemailer uses the transporter to send an email to the recipient with the gift card image attached.
- Delivery Confirmation: The callback function logs the email sending status, providing feedback on successful deliveries or errors.
Important Considerations
- Error Handling: Implement robust error handling to manage potential issues during email sending.
- Security: Never commit your email credentials directly to your code. Use environment variables.
- Zoho Configuration: Always refer to the official Zoho Mail documentation for the most up-to-date SMTP settings.
- Testing: Thoroughly test the email sending functionality to ensure reliable delivery.
By following these steps, I successfully integrated automated email delivery into our gift card application, enhancing the user experience and providing a valuable feature.