send emails by gmail
This commit is contained in:
parent
2f5dca589f
commit
1b6f2b2f63
|
@ -296,4 +296,21 @@ export class AppController {
|
||||||
) {
|
) {
|
||||||
return this.appService.findReport(paramReport);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -419,4 +419,25 @@ export class AppService {
|
||||||
map((message: string) => ({ message })),
|
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 })),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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}
|
|
@ -1,5 +1,9 @@
|
||||||
{
|
{
|
||||||
"$schema": "https://json.schemastore.org/nest-cli",
|
"$schema": "https://json.schemastore.org/nest-cli",
|
||||||
"collection": "@nestjs/schematics",
|
"collection": "@nestjs/schematics",
|
||||||
"sourceRoot": "src"
|
"sourceRoot": "src",
|
||||||
|
"compilerOptions": {
|
||||||
|
"assets": ["mails/**/*"],
|
||||||
|
"watchAssets": true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -21,12 +21,16 @@
|
||||||
"test:e2e": "jest --config ./test/jest-e2e.json"
|
"test:e2e": "jest --config ./test/jest-e2e.json"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@nestjs-modules/mailer": "^1.7.1",
|
||||||
"@nestjs/common": "^8.0.0",
|
"@nestjs/common": "^8.0.0",
|
||||||
|
"@nestjs/config": "^2.2.0",
|
||||||
"@nestjs/core": "^8.0.0",
|
"@nestjs/core": "^8.0.0",
|
||||||
"@nestjs/mapped-types": "*",
|
"@nestjs/mapped-types": "*",
|
||||||
"@nestjs/microservices": "^8.4.7",
|
"@nestjs/microservices": "^8.4.7",
|
||||||
"@nestjs/platform-express": "^8.0.0",
|
"@nestjs/platform-express": "^8.0.0",
|
||||||
"@nestjs/swagger": "^5.2.1",
|
"@nestjs/swagger": "^5.2.1",
|
||||||
|
"handlebars": "^4.7.7",
|
||||||
|
"nodemailer": "^6.7.7",
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
"rimraf": "^3.0.2",
|
"rimraf": "^3.0.2",
|
||||||
"rxjs": "^7.2.0",
|
"rxjs": "^7.2.0",
|
||||||
|
|
|
@ -1,11 +1,45 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { AppController } from './app.controller';
|
import { AppController } from './app.controller';
|
||||||
import { NotificationsModule } from './notifications/notifications.module';
|
import { MailerModule } from '@nestjs-modules/mailer';
|
||||||
import { ClientsModule, Transport } from "@nestjs/microservices";
|
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({
|
@Module({
|
||||||
imports: [
|
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([
|
ClientsModule.register([
|
||||||
{
|
{
|
||||||
name: "SERVICIO_NOTIFICACIONES",
|
name: "SERVICIO_NOTIFICACIONES",
|
||||||
|
@ -16,8 +50,8 @@ import { ClientsModule, Transport } from "@nestjs/microservices";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]),
|
]),
|
||||||
NotificationsModule],
|
AuthModule],
|
||||||
controllers: [AppController],
|
controllers: [AppController, EmailController],
|
||||||
providers: [],
|
providers: [],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
|
|
@ -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 {}
|
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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>
|
|
@ -0,0 +1,4 @@
|
||||||
|
export interface User {
|
||||||
|
email: string;
|
||||||
|
name: string;
|
||||||
|
}
|
|
@ -5,6 +5,7 @@
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
|
"name": "servicio-usuarios",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { UsersModule } from './users/users.module';
|
||||||
imports: [
|
imports: [
|
||||||
ClientsModule.register([
|
ClientsModule.register([
|
||||||
{
|
{
|
||||||
name: "SERVICIO_USUARIOS",
|
name: "SERVICIO_NOTIFICACIONES",
|
||||||
transport: Transport.TCP,
|
transport: Transport.TCP,
|
||||||
options: {
|
options: {
|
||||||
host: "127.0.0.1",
|
host: "127.0.0.1",
|
||||||
|
|
Loading…
Reference in New Issue