import { Config, Init, Provide } from '@midwayjs/core';
import * as nodemailer from 'nodemailer';
@Provide()
export class EmailService {
  @Config('email')
  emailSetting;

  client;
  @Init()
  init() {
    const config = this.emailSetting.config;
    this.client = nodemailer.createTransport(config);
  }

  sendPwdCode(email, code) {
    const sender = this.emailSetting.from;
    const senderEmail = this.emailSetting.config.auth.user;
    const text = `[${sender}] 您的验证码为: ${code}, 请勿泄露于他人!`;
    const mailContext = {
      from: `"${sender}" <${senderEmail}>`,
      to: email,
      subject: `找回密码`,
      text,
    };
    this.client.sendMail(mailContext, (error, info) => {
      if (error) console.log(error);
      else console.log(info);
    });
  }
}
