@asjas/fastify-nodemailer
v1.0.8
Published
A scoped Fastify plugin wrapping Nodemailer for easy email sending.
Maintainers
Readme
@asjas/fastify-nodemailer
A Fastify plugin to integrate Nodemailer for sending emails. This plugin allows you to share a single Nodemailer transporter instance across your Fastify server, simplifying email functionality in your application.
Features
- Seamless integration with Fastify's plugin system.
- Share a single Nodemailer transporter across your server.
- Support for SMTP connection pooling for improved performance.
- Compatibility with Fastify's encapsulation for multiple transports.
- TypeScript support for type-safe development.
Install
npm i --save-exact @asjas/fastify-nodemailerCompatibility
The plugin supports the following Fastify and Nodemailer versions.
NPM Version | Branch | Fastify | Nodemailer | End of support --------|--------|---------|------------|--------------- 1.x | main | 5.x | 7.x | TBD
Usage
Register the plugin with your Fastify server and provide Nodemailer transport options. The options passed to register are forwarded to Nodemailer's createTransport function. Refer to the Nodemailer documentation for detailed configuration options.
Example
import Fastify from 'fastify';
import fastifyNodemailerPlugin from '@asjas/fastify-nodemailer';
const fastify = Fastify();
fastify.register(fastifyNodemailerPlugin, {
host: 'smtp.example.com',
port: 587,
secure: false, // Use true for port 465, false for other ports
auth: {
user: '[email protected]',
pass: 'your-password',
},
});
// Example route to send an email
fastify.post('/send-email', async (request, reply) => {
try {
const info = await fastify.nodemailer.sendMail({
from: '[email protected]',
to: '[email protected]',
subject: 'Test Email',
text: 'This is a test email!',
});
return { message: 'Email sent', info };
} catch (err) {
fastify.log.error('Error sending email:', err);
reply.status(500).send({ error: 'Failed to send email' });
}
});
fastify.listen({ port: 3000 }, (err) => {
if (err) throw err;
console.log('Server running on http://localhost:3000');
});Connection Pooling
For applications sending multiple emails, you can enable SMTP connection pooling to improve performance by reusing connections. Configure pooling by setting pool: true and related options:
fastify.register(fastifyNodemailerPlugin, {
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: '[email protected]',
pass: 'your-password',
},
pool: true, // Enable connection pooling
maxConnections: 5, // Maximum concurrent connections (default: 5)
maxMessages: 50, // Maximum messages per connection (default: 100)
rateLimit: 5, // Maximum 5 emails per second
rateDelta: 1000, // Rate limit window (1 second)
});See the Nodemailer SMTP documentation for more details on pooling options.
Multiple Transports
Thanks to Fastify's encapsulation, you can register multiple instances of the plugin with different transporters in separate contexts. For example:
fastify.register(async (instance) => {
instance.register(fastifyNodemailerPlugin, {
host: 'smtp.service1.com',
port: 587,
secure: false,
auth: {
user: '[email protected]',
pass: 'password1',
},
});
instance.post('/send-email-service1', async (request, reply) => {
// Use service1 transporter
await instance.nodemailer.sendMail({ /* email options */ });
});
});
fastify.register(async (instance) => {
instance.register(fastifyNodemailerPlugin, {
host: 'smtp.service2.com',
port: 587,
secure: false,
auth: {
user: '[email protected]',
pass: 'password2',
},
});
instance.post('/send-email-service2', async (request, reply) => {
// Use service2 transporter
await instance.nodemailer.sendMail({ /* email options */ });
});
});License
Licensed under MIT.
