Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
nest-mailer-module
Advanced tools
Readme
$ npm i --save nest-mailer
Create mailer module in you application:
import { Global, Module } from '@nestjs/common';
import { MailerModule, SendgridMailer } from 'nest-mailer-module';
import { MailerEventEmitter } from './mailer.emitter';
@Global()
@Module({
imports: [MailerModule.forRoot({ mailer: new SendgridMailer(/* sendgrid api key*/) })],
providers: [MailerEventEmitter],
exports: [MailerEventEmitter],
})
export class AppMailerModule {}
Then create our MailerEventEmitter
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { SendEmailEvent, SEND_EMAIL_EVENT } from 'nest-mailer-module';
@Injectable()
export class MailerEventEmitter {
constructor(private eventEmitter: EventEmitter2) {}
emit(event: SendEmailEvent) {
this.eventEmitter.emit(SEND_EMAIL_EVENT, new SendEmailEvent({
from: 'from@mydomain.com',
...event.message,
}));
}
}
After that we should import our newly created module in app.module and we can inject it in any service since it was configured as global module with private mailerEventEmitter: MailerEventEmitter
.
Now we can send plain emails with:
this.mailerEventEmitter.emit({
subject: 'Reset Password',
to: [{ email: 'someone@example.com', type: 'to' }],
text: `Reset password`,
});
We can write our custom mailer by implementing interface Mailer
and using it in mailer.module as mailer property for configuration. Here we have an example of mandrill client:
import { Logger } from '@nestjs/common';
import { Mandrill } from 'mandrill-api';
import { Mailer, Message } from 'nest-mailer-module';
export class MandrillMailer implements Mailer {
private logger = new Logger(MandrillMailer.name);
mandrillClient: any;
constructor(mandrillApiKey: string) {
this.mandrillClient = new Mandrill(mandrillApiKey);
this.mandrillClient.users.ping(
{},
result => this.logger.log('ping "mandrill" success'),
e => this.logger.error('ping "mandrill" failed'),
);
}
send(message: Message) {
const loggerContext = { subject: message.subject, to: message.to };
this.logger.log(loggerContext, 'Sending email');
return this.mandrillClient.messages.send(
{
message: {
auto_text: true,
from_email: message.from?.email,
from_name: message.from?.name,
html: message.html,
important: true,
subject: message.subject,
to: message.to,
text: message.text,
},
async: true,
},
() => {
this.logger.log(loggerContext, `Mail sent`);
},
error => {
this.logger.error(error, error.message);
},
);
}
}
Let's start with creating mailer module in you application like in first example
Now we can create ReactMailerEventEmitter
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { SendTemplatedEmailEvent, SEND_TEMPLATED_EMAIL_EVENT } from 'nest-mailer-module';
@Injectable()
export class ReactMailerEventEmitter {
constructor(private eventEmitter: EventEmitter2) {}
emit(event: SendTemplatedEmailEvent) {
this.eventEmitter.emit(SEND_TEMPLATED_EMAIL_EVENT,
new SendTemplatedEmailEvent(event.template, {
from: 'from@mydomain.com',
...event.message,
}, event.mergeVars),
);
}
}
FAQs
NestJS Discovery Module
The npm package nest-mailer-module receives a total of 1 weekly downloads. As such, nest-mailer-module popularity was classified as not popular.
We found that nest-mailer-module demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.