我正在尝试使用@ nestjs-modules / mailer nodemailer在Nestjs框架中发送电子邮件。电子邮件可以正常使用正常文本,但是在设置为电子邮件正文使用EJS模板时,它已停止工作。我使用[https://nest-modules.github.io/mailer/docs/mailer.html][1]作为参考,以下是我的源代码。App.module.ts
import { Module } from '@nestjs/common';
import { MailerModule } from '@nestjs-modules/mailer';
import { EjsAdapter } from '@nestjs-modules/mailer/dist/adapters/ejs.adapter';
import { join } from 'path';
const path = join(__dirname, '../../../apps/api/src/app/template');
@Module({
imports: [MailerModule.forRoot({
transport: environment.SmtpDetails,
defaults: {
from: environment.SmtpEmail,
},
template: {
dir: path,
adapter: new EjsAdapter(),
options: {
strict: true,
},
},
})
})
export class AppModule { }
电子邮件服务
import { Injectable, BadGatewayException } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';
@Injectable()
export class EmailService {
constructor(private readonly mailerService: MailerService) { }
async sendNotificationEmail(emailTo: string, data: object) {
console.log(__dirname);
try {
const emailData = await this.mailerService.sendMail({
to: emailTo,
from: '[email protected]',
subject: 'Testing Nest Mailermodule with template',
template: 'notification',
context: { // Data to be sent to template engine.
"code": 'cf1a3f828287',
"username": 'john doe',
},
});
if (emailData) return emailData;
} catch (e) {
console.log(e);
throw new BadGatewayException('Email send failed');
}
}
}
Notification.ejs
<p>Welcome , your activation code is <%= code %></p>
我收到未定义代码错误。
在main.ts中尝试“ app.setViewEngine('ejs');”