AI da dat ten cho dong song

This commit is contained in:
2024-10-26 11:10:14 +07:00
parent 2b6d3869f9
commit 48305a3948
11 changed files with 2999 additions and 63 deletions

45
src/Mail/mail.module.ts Normal file
View 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
View 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');
}
}
}