email.service.ts 1.87 KB
// mail/mail.service.ts
import { Injectable, Logger } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';
import * as nodemailer from 'nodemailer';
import { EmailQueryDto } from 'src/dto/email.dto';
@Injectable()
export class EmailService {
  // private transporter;
  constructor(private readonly mailerService: MailerService) {}
  // constructor() {
  //   this.transporter = nodemailer.createTransport({
  //     host: process.env.SMTP_HOST,
  //     port: +process.env.SMTP_PORT,
  //     secure: true, // 如果是 SMTPS 连接,设置为 true
  //     auth: {
  //       user: process.env.SMTP_USER,
  //       pass: process.env.SMTP_PASSWORD,
  //     },
  //   });
  // }
  async sendMail(query: EmailQueryDto): Promise<void> {
    const { to, subject, mentorship_page, supervisor_daily_page, extraction, reportData, template, sales_content, training_materials, scene_market, aiAgentColumns, ai_agent } = query
    Logger.log(aiAgentColumns, 'aiAgentColumns')
    const mailOptions = {
      from: process.env.SMTP_FROM,
      to,
      subject,
      template: './function.point.ejs',
      context: {
        code: 1, //验证码
        date: '2025-05-28', //日期
        sign: '系统邮件,回复无效。', //发送的签名,当然也可以不要
        mentorship_page,
        supervisor_daily_page,
        extraction,
        sales_content,
        training_materials,
        scene_market,
        reportData,
        template,
        aiAgentColumns,
        ai_agent
        // mentorship_page: mentorship_page,
        // supervisor_daily_page: JSON.parse(supervisor_daily_page),
        // extraction: JSON.parse(extraction),
        // sales_content: JSON.parse(sales_content),
        // training_materials: JSON.parse(training_materials),
        // scene_market: JSON.parse(scene_market)
      }
    };

    await this.mailerService.sendMail(mailOptions);
  }
}