AI da dat ten cho dong song
This commit is contained in:
45
src/Mail/mail.module.ts
Normal file
45
src/Mail/mail.module.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import * as path from 'path';
|
||||
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
|
||||
import { MailService } from './mail.service';
|
||||
import { MailerModule } from '@nestjs-modules/mailer';
|
||||
import { OpenaiModule } from '../OpenAI/openai.module';
|
||||
import { PugAdapter } from '@nestjs-modules/mailer/dist/adapters/pug.adapter';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [
|
||||
MailerModule.forRootAsync({
|
||||
useFactory: () => ({
|
||||
transport: {
|
||||
host: process.env.MAILU_HOST,
|
||||
port: parseInt(process.env.MAILU_PORT || '587'),
|
||||
secure: false,
|
||||
pool: true,
|
||||
authMethod: 'login',
|
||||
path: '/',
|
||||
auth: {
|
||||
user: process.env.MAILU_USER,
|
||||
pass: process.env.MAILU_PASSWORD,
|
||||
},
|
||||
verify: true,
|
||||
},
|
||||
defaults: {
|
||||
from: process.env.MAILU_FROM,
|
||||
},
|
||||
// template: {
|
||||
// dir: path.join(__dirname, 'templates'),
|
||||
// adapter: new PugAdapter(),
|
||||
// options: {
|
||||
// strict: true,
|
||||
// },
|
||||
// },
|
||||
}),
|
||||
}),
|
||||
OpenaiModule,
|
||||
],
|
||||
providers: [MailService],
|
||||
exports: [MailService],
|
||||
})
|
||||
export class MailModule {}
|
||||
51
src/Mail/mail.service.ts
Normal file
51
src/Mail/mail.service.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
|
||||
import { MailerService } from '@nestjs-modules/mailer';
|
||||
import { OpenaiService } from '../OpenAI/openai.service';
|
||||
|
||||
@Injectable()
|
||||
export class MailService {
|
||||
constructor(
|
||||
private readonly mailerService: MailerService,
|
||||
private readonly openaiService: OpenaiService,
|
||||
) {}
|
||||
|
||||
async sendEmail(to: string, subject: string, text: string) {
|
||||
try {
|
||||
const mailContent =
|
||||
await this.openaiService.generateInvitationMailContent(
|
||||
to,
|
||||
'John Doe',
|
||||
'https://epess.org',
|
||||
);
|
||||
|
||||
const result = await this.mailerService.sendMail({
|
||||
to,
|
||||
subject,
|
||||
text: mailContent ?? text,
|
||||
});
|
||||
Logger.log(result, 'MailService');
|
||||
} catch (error) {
|
||||
Logger.error(error, 'MailService');
|
||||
}
|
||||
}
|
||||
|
||||
async sendTemplateEmail(
|
||||
to: string,
|
||||
subject: string,
|
||||
template: string,
|
||||
context: any,
|
||||
) {
|
||||
try {
|
||||
const result = await this.mailerService.sendMail({
|
||||
to,
|
||||
subject,
|
||||
template,
|
||||
context,
|
||||
});
|
||||
Logger.log(result, 'MailService');
|
||||
} catch (error) {
|
||||
Logger.error(error, 'MailService');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user