This chapter fully explores Nodemailer and its options, configuration, advanced features, limitations and best practices. It has been designed for reference by developers at every level but particularly by senior developers who want guidance in detail.
Nodemailer is as popular among developers as the Node.js library for sending emails. Nodemailer makes email communication much easier to manage by offering an intuitive API to use with email servers. Nodemailer has really broad features, so it is suitable for sending both single and bulk emails - there is truly something for everyone here.
1. Installing Nodemailer:
npm install nodemailer
2. Creating a Transporter: This is the first step in configuring Nodemailer.
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail', // Example: Gmail as the email service
auth: {
user: 'your-email@gmail.com',
pass: 'your-email-password',
},
});
3. Verifying the Connection: Ensure the transporter is configured correctly.
transporter.verify((error, success) => {
if (error) {
console.error('Transporter configuration failed:', error);
} else {
console.log('Transporter is ready to send emails:', success);
}
});
1. Sending HTML Emails:
const mailOptions = {
from: 'sender@oneclickitsolution.com',
to: 'recipient@oneclickitsolution.com',
subject: 'HTML Email Example',
html: '<h1>Welcome!</h1><p>This email contains HTML content.</p>',
};
2. Adding Attachments:
const mailOptions = {
from: 'sender@oneclickitsolution.com',
to: 'recipient@oneclickitsolution.com',
subject: 'Email with Attachments',
text: 'Please find the attached file.',
attachments: [
{ filename: 'sampleFile.txt', content: 'Welcome to Node Mailer Documentation' },
{ path: './path-to-file.jpg' }, // Only File path
],
};
3. DKIM Signing:
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your-email@gmail.com',
pass: 'your-email-password',
},
dkim: {
domainName: 'oneclickitsolution.com',
keySelector: 'default',
privateKey: 'private-key-content',
},
});
4. Using Plugins:
const smtpTransport = require('nodemailer-smtp-transport');
const transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
user: 'your-email@gmail.com',
pass: 'your-email-password',
},
}));
1. Sending Emails in Batches:
const recipients = ['customer1@oneclickitsolution.com', 'agent1@example.com', 'admin1@oneclickitsolution.com'];
recipients.forEach(email => {
const mailOptions = {
from: 'sender@oneclickitsolution.com',
to: email,
subject: 'Bulk Email',
text: 'This is a bulk email example.',
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
console.error('Failed to send email:', err);
} else {
console.log('Email sent to', email, ':', info.response);
}
});
});
2. Using Queues for Bulk Emails: For large-scale email sending, use libraries like Bull or Agenda to manage email jobs in a queue.
Limitations of Nodemailer
Memory Usage and Optimization
Nodemailer is a very versatile, robust library for sending emails in Node.js for small to medium scale cases. For high volume delivery, it might be best put into thirdparty services such as AWS SES or SendGrid. With rich functionalities, secured options and cost effective, it tops the bucket list for developers.
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our NodeJS Expertise.
0