feather_mailer
v1.0.3
Published
A powerful and flexible Node.js module for sending emails with templating, attachments, queuing, and logging.
Maintainers
Readme
FeatherMailer
FeatherMailer is a powerful and flexible Node.js module for sending emails with a wide range of features and customization options.
Installation
npm install feather_mailerUsage
Basic Usage
const Mailer = require('feather_mailer');
const mailer = Mailer({
host: 'smtp.example.com',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: '[email protected]',
pass: 'your_password'
}
});
async function sendTestEmail() {
try {
await mailer.sendMail({
from: '"Sender Name" <[email protected]>',
to: '[email protected]',
subject: 'Hello from EasyMailer',
text: 'This is a test email sent using EasyMailer!',
html: '<b>This is a test email sent using EasyMailer!</b>'
});
console.log('Email sent successfully!');
} catch (error) {
console.error('Failed to send email:', error);
}
}
sendTestEmail();Using Templates
First, create a templates directory in your project root and add a Handlebars file (e.g., welcome.hbs):
templates/welcome.hbs:
<h1>Welcome, {{name}}!</h1>
<p>Thank you for signing up.</p>Then, initialize the Mailer with the template directory and use the template option in sendMail:
const path = require('path');
const Mailer = require('feather_mailer');
const mailer = Mailer({
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: '[email protected]',
pass: 'your_password'
}
}, path.join(__dirname, 'templates')); // Pass the template directory
async function sendTemplatedEmail() {
try {
await mailer.sendMail({
from: '"Sender Name" <[email protected]>',
to: '[email protected]',
subject: 'Welcome to Our Service!',
template: {
name: 'welcome',
data: {
name: 'John Doe'
}
}
});
console.log('Templated email sent successfully!');
} catch (error) {
console.error('Failed to send templated email:', error);
}
}
sendTemplatedEmail();Customization Options
You can provide default options when initializing the Mailer, which will be merged with the options provided in sendMail:
const Mailer = require('feather_mailer');
const mailer = Mailer({
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: '[email protected]',
pass: 'your_password'
}
}, null, { // Pass defaultOptions here
from: '"Default Sender" <[email protected]>',
headers: {
'X-Mailer-App': 'EasyMailer'
}
});
async function sendCustomizedEmail() {
try {
await mailer.sendMail({
to: '[email protected]',
subject: 'Customized Email',
text: 'This email uses default sender and custom headers.'
});
console.log('Customized email sent successfully!');
} catch (error) {
console.error('Failed to send customized email:', error);
}
}
sendCustomizedEmail();Email Queuing
EasyMailer supports email queuing using a provided queue instance (e.g., BullMQ). When a queue is provided, emails are added to the queue instead of being sent immediately, allowing for background processing and improved application responsiveness.
const Mailer = require('feather_mailer');
const Queue = require('bull'); // Example using BullMQ
const emailQueue = new Queue('emails', 'redis://127.0.0.1:6379'); // Initialize your queue
const mailer = Mailer({
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: '[email protected]',
pass: 'your_password'
}
}, null, null, null, null, emailQueue); // Pass the queue instance
async function sendQueuedEmail() {
try {
await mailer.sendMail({
from: '"Sender Name" <[email protected]>',
to: '[email protected]',
subject: 'Queued Email',
text: 'This email will be sent via the queue.'
});
console.log('Email added to queue!');
} catch (error) {
console.error('Failed to add email to queue:', error);
}
}
sendQueuedEmail();
// To process the queue (e.g., in a separate worker process):
mailer.processQueue();Features
- Easy to Use: Simple API for sending emails.
- Templating: Supports Handlebars for dynamic email content.
- Attachments: Send files as attachments.
- Customization: Extensive options for configuring mail transport and email headers.
- Email Queuing: Asynchronously send emails using a provided queue.
- Logging: Basic logging for email sending events.
- Email Validation: Email Validation
- HTML to Text Conversion: HTML to Text Conversion
- Customizable Default Options: Customizable Default Options
- Error Handling: Robust error handling for reliable email delivery.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
ISC
