@tresdoce-nestjs-toolkit/mailer v1.0.3
Este módulo está pensado para ser utilizado en NestJS Starter, o cualquier proyecto que utilice una configuración centralizada, siguiendo la misma arquitectura del starter.
Glosario
- 🥳 Demo
- 📝 Requerimientos básicos
- 🛠️ Instalar dependencia
- ⚙️ Configuración
- 👨💻 Uso
- 📄 Changelog
- 📜 License MIT
📝 Requerimientos básicos
- NestJS Starter
- Node.js v18.17.0 or higher (Download)
- YARN v1.22.18 or higher
- NPM v9.6.7 or higher
- NestJS v10.3.0 or higher (Documentación)
🛠️ Instalar dependencia
npm install -S @tresdoce-nestjs-toolkit/mailer
yarn add @tresdoce-nestjs-toolkit/mailer
⚙️ Configuración
Agregar los datos de conexión SMTP en configuration.ts
utilizando el key mailer
que contenga los datos desde las
variables de entorno.
//./src/config/configuration.ts
import { Typings } from '@tresdoce-nestjs-toolkit/core';
import { registerAs } from '@nestjs/config';
export default registerAs('config', (): Typings.AppConfig => {
return {
//...
mailer: {
transport: {
host: process.env.EMAIL_HOST,
port: parseInt(process.env.EMAIL_PORT, 10) || 587,
secure: true,
requireTLS: true,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASSWORD,
},
},
defaults: {
from: process.env.EMAIL_USER,
},
},
//...
};
});
👨💻 Uso
Importar el MailerModule
en el archivo app.module.ts
, y el módulo se encargará de obtener la configuración
y realizar la connexion automáticamente.
//./src/app.module.ts
import { MailerModule } from '@tresdoce-nestjs-toolkit/mailer';
@Module({
//...
imports: [
//...
MailerModule,
//...
],
//...
})
export class AppModule {}
Inyectar el MailerService
para poder realizar el envío de mails.
//./src/app.service.ts
import { MailerService } from '@tresdoce-nestjs-toolkit/mailer';
@Injectable()
export class AppService {
constructor(private readonly mailerService: MailerService) {}
async sendMail() {
try {
return await this.mailerService.sendMail({
to: 'to <to@email.com>',
from: 'from <from@email.com>',
subject: 'Subject of mail',
text: 'this is a plain text',
html: '<b>this is a html email</b>',
});
} catch (error) {
throw new Error(error.message);
}
}
}
Template (ejs, pug o handlebars)
Para poder trabajar con templates de email, hay que agregarle la configuración templates
en el configuration.ts
y
especificar que adaptador vas a utilizar y la ruta de donde se encuentran los templates.
//./src/config/configuration.ts
import { Typings } from '@tresdoce-nestjs-toolkit/core';
import { registerAs } from '@nestjs/config';
import { HandlebarsAdapter } from '@tresdoce-nestjs-toolkit/mailer';
export default registerAs('config', (): Typings.AppConfig => {
return {
//...
mailer: {
transport: {
host: process.env.EMAIL_HOST,
port: parseInt(process.env.EMAIL_PORT, 10) || 587,
secure: true,
requireTLS: true,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASSWORD,
},
},
defaults: {
from: process.env.EMAIL_USER,
},
template: {
dir: join(__dirname, './templates'),
adapter: new HandlebarsAdapter(),
options: {
strict: true,
},
},
},
//...
};
});
Envío de mail con template
//./src/app.service.ts
import { MailerService } from '@tresdoce-nestjs-toolkit/mailer';
@Injectable()
export class AppService {
constructor(private readonly mailerService: MailerService) {}
async sendMail(email: string, name: string) {
try {
return await this.mailerService.sendMail({
to: email,
from: 'from <from@email.com>',
subject: 'Greeting from NestJS NodeMailer',
template: './email',
context: {
name: name,
},
});
} catch (error) {
throw new Error(error.message);
}
}
}
Email template
//templates/email.hbs
<p>Hi {{name}},</p>
<p>Hello from NestJS NodeMailer</p>
📄 Changelog
Todos los cambios notables de este paquete se documentarán en el archivo Changelog.
4 months ago
7 months ago
8 months ago
9 months ago
9 months ago
8 months ago
7 months ago
8 months ago
9 months ago
9 months ago
10 months ago
10 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
11 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago