send emails by gmail

This commit is contained in:
Mariela 2022-07-15 18:36:02 -06:00
parent 2f5dca589f
commit 1b6f2b2f63
15 changed files with 5515 additions and 182 deletions

View File

@ -296,4 +296,21 @@ export class AppController {
) {
return this.appService.findReport(paramReport);
}
@Post('email/sendMail')
senMail(
@Body('email') email: string,
) {
return this.appService.sendMail(email);
}
@Post('email/html')
html(
@Body('email') email: string,
@Body('name') name: string,
) {
return this.appService.html(email, name);
}
}

View File

@ -419,4 +419,25 @@ export class AppService {
map((message: string) => ({ message })),
);
}
sendMail(email: string) {
const pattern = { cmd: 'sendMail' };
const payload = { email: email};
return this.clientNotificationtApp
.send<string>(pattern, payload)
.pipe(
map((message: string) => ({ message })),
);
}
html(email: string, name: string) {
const pattern = { cmd: 'html' };
const payload = { email: email, name: name};
return this.clientNotificationtApp
.send<string>(pattern, payload)
.pipe(
map((message: string) => ({ message })),
);
}
}

View File

@ -0,0 +1,8 @@
# mail
MAIL_HOST=smtp.gmail.com
MAIL_USER=mbonilla.guti@gmail.com
MAIL_PASSWORD=laofghlofgffmyry
MAIL_FROM=noreply@example.com
# optional
MAIL_TRANSPORT=smtp://${MAIL_USER}:${MAIL_PASSWORD}@${MAIL_HOST}

View File

@ -1,5 +1,9 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src"
"sourceRoot": "src",
"compilerOptions": {
"assets": ["mails/**/*"],
"watchAssets": true
}
}

File diff suppressed because it is too large Load Diff

View File

@ -21,12 +21,16 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs-modules/mailer": "^1.7.1",
"@nestjs/common": "^8.0.0",
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^8.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/microservices": "^8.4.7",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/swagger": "^5.2.1",
"handlebars": "^4.7.7",
"nodemailer": "^6.7.7",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",

View File

@ -1,11 +1,45 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { NotificationsModule } from './notifications/notifications.module';
import { MailerModule } from '@nestjs-modules/mailer';
import { ClientsModule, Transport } from "@nestjs/microservices";
import { AuthModule } from './auth/auth.module';
import { EmailController } from './email.controller';
import { join } from 'path';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
MailerModule.forRootAsync({
// imports: [ConfigModule], // import module if not enabled globally
useFactory: async (config: ConfigService) => ({
// transport: config.get("MAIL_TRANSPORT"),
// or
transport: {
host: config.get('MAIL_HOST'),
secure: false,
auth: {
user: config.get('MAIL_USER'),
pass: config.get('MAIL_PASSWORD'),
},
},
defaults: {
from: `"No Reply" <${config.get('MAIL_USER')}>`,
},
template: {
dir: join(__dirname, 'mails'),
adapter: new HandlebarsAdapter(),
options: {
strict: true,
},
},
}),
inject: [ConfigService],
}),
ConfigModule.forRoot({
isGlobal: true, // no need to import into other modules
}),
ClientsModule.register([
{
name: "SERVICIO_NOTIFICACIONES",
@ -16,8 +50,8 @@ import { ClientsModule, Transport } from "@nestjs/microservices";
}
}
]),
NotificationsModule],
controllers: [AppController],
AuthModule],
controllers: [AppController, EmailController],
providers: [],
})
export class AppModule {}

View File

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
//import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
@Module({
//controllers: [AuthController],
providers: [AuthService],
})
export class AuthModule {}

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';
describe('AuthService', () => {
let service: AuthService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
}).compile();
service = module.get<AuthService>(AuthService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@ -0,0 +1,13 @@
import { Injectable } from '@nestjs/common';
import { User } from './../user/user.entity';
@Injectable()
export class AuthService {
async signUp(user: User) {
const token = Math.floor(1000 + Math.random() * 9000).toString();
// create user in db
// ...
// send confirmation mail
}
}

View File

@ -0,0 +1,38 @@
import { Controller, Get, Query } from '@nestjs/common';
import { MessagePattern, Payload } from '@nestjs/microservices';
import { MailerService } from '@nestjs-modules/mailer';
import { User } from './user/user.entity';
@Controller()
export class EmailController {
constructor(private mailService: MailerService) { }
@MessagePattern({ cmd: 'sendMail' })
sendMail(@Payload() toEmail: string) {
var response = this.mailService.sendMail({
to: toEmail["email"],
from: "mbonilla.guti@gmail.com",
subject: 'Plain Text Email ✔',
text: 'Welcome NestJS Email Sending Tutorial',
});
return response;
}
@MessagePattern({ cmd: 'html' })
async postHTMLEmail(@Payload() user: any) {
const url = "http://localhost:3000/";
var response = await this.mailService.sendMail({
to: user["email"],
from: "mbonilla.guti@gmail.com",
subject: 'HTML Dynamic Template',
template: 'confirmation',
context: {
name: user["name"],
url,
},
});
return response;
}
}

View File

@ -0,0 +1,7 @@
<p>Hey {{ name }},</p>
<p>Please click below to confirm your email</p>
<p>
<a href="{{ url }}">Confirm</a>
</p>
<p>If you did not request this email you can safely ignore it.</p>

View File

@ -0,0 +1,4 @@
export interface User {
email: string;
name: string;
}

View File

@ -5,6 +5,7 @@
"requires": true,
"packages": {
"": {
"name": "servicio-usuarios",
"version": "0.0.1",
"license": "UNLICENSED",
"dependencies": {

View File

@ -7,7 +7,7 @@ import { UsersModule } from './users/users.module';
imports: [
ClientsModule.register([
{
name: "SERVICIO_USUARIOS",
name: "SERVICIO_NOTIFICACIONES",
transport: Transport.TCP,
options: {
host: "127.0.0.1",