What is @nestjs-modules/mailer?
@nestjs-modules/mailer is a mailer module for NestJS applications. It provides a simple and flexible way to send emails using various transport methods such as SMTP, sendmail, and more. The module integrates seamlessly with NestJS, allowing developers to easily configure and use it within their applications.
What are @nestjs-modules/mailer's main functionalities?
Basic Email Sending
This feature allows you to send basic emails using SMTP configuration. The code sample demonstrates how to configure the mailer module and send a simple email.
const mailerConfig = {
transport: {
host: 'smtp.example.com',
port: 587,
auth: {
user: 'username',
pass: 'password'
}
}
};
@Module({
imports: [MailerModule.forRoot(mailerConfig)],
providers: [AppService],
})
export class AppModule {}
@Injectable()
export class AppService {
constructor(private readonly mailerService: MailerService) {}
async sendEmail() {
await this.mailerService.sendMail({
to: 'recipient@example.com',
subject: 'Test Email',
text: 'This is a test email',
});
}
}
Template-based Email Sending
This feature allows you to send emails using templates. The code sample demonstrates how to configure the mailer module with a template engine (Handlebars in this case) and send an email using a template.
const mailerConfig = {
transport: {
host: 'smtp.example.com',
port: 587,
auth: {
user: 'username',
pass: 'password'
}
},
template: {
dir: __dirname + '/templates',
adapter: new HandlebarsAdapter(),
options: {
strict: true
}
}
};
@Module({
imports: [MailerModule.forRoot(mailerConfig)],
providers: [AppService],
})
export class AppModule {}
@Injectable()
export class AppService {
constructor(private readonly mailerService: MailerService) {}
async sendTemplateEmail() {
await this.mailerService.sendMail({
to: 'recipient@example.com',
subject: 'Test Email',
template: 'welcome',
context: {
name: 'John Doe'
}
});
}
}
Email Attachments
This feature allows you to send emails with attachments. The code sample demonstrates how to configure the mailer module and send an email with an attachment.
const mailerConfig = {
transport: {
host: 'smtp.example.com',
port: 587,
auth: {
user: 'username',
pass: 'password'
}
}
};
@Module({
imports: [MailerModule.forRoot(mailerConfig)],
providers: [AppService],
})
export class AppModule {}
@Injectable()
export class AppService {
constructor(private readonly mailerService: MailerService) {}
async sendEmailWithAttachment() {
await this.mailerService.sendMail({
to: 'recipient@example.com',
subject: 'Test Email with Attachment',
text: 'Please find the attachment.',
attachments: [
{
filename: 'test.txt',
path: './test.txt'
}
]
});
}
}
Other packages similar to @nestjs-modules/mailer
nodemailer
Nodemailer is a module for Node.js applications to allow easy email sending. It is a popular choice for sending emails in Node.js applications and provides a wide range of features including support for various transport methods, attachments, and HTML content. Compared to @nestjs-modules/mailer, Nodemailer is more general-purpose and not specifically designed for NestJS, but it can be integrated into NestJS applications with some additional setup.
email-templates
Email-templates is a Node.js module for rendering email templates using various template engines like Handlebars, Pug, and EJS. It is often used in conjunction with Nodemailer to send templated emails. Compared to @nestjs-modules/mailer, email-templates focuses specifically on template rendering and does not handle the actual sending of emails, which would require an additional module like Nodemailer.
sendgrid
SendGrid is a cloud-based service that provides an API for sending emails. It offers features like email tracking, analytics, and high deliverability. Compared to @nestjs-modules/mailer, SendGrid is a third-party service that requires an API key and provides additional features like email analytics and tracking, which are not available out-of-the-box with @nestjs-modules/mailer.
A mailer module for Nest framework (node.js) using Nodemailer library
Installation
npm install --save @nestjs-modules/mailer nodemailer
npm install --save-dev @types/nodemailer
yarn add @nestjs-modules/mailer nodemailer
yarn add -D @types/nodemailer
Hint: handlebars and pug is an optional dependency, if you want to use the template, you must install it.
with npm
npm install --save handlebars
npm install --save pug
npm install --save ejs
npm install --save mjml
with yarn
yarn add handlebars
yarn add pug
yarn add ejs
yarn add mjml
Documentation
you can find all the documentation here for the email module
Starter kit
Contributing
License
MIT