finance-watcher / app / mailService.ts
mailService.ts
Raw
import nodemailer from 'nodemailer';
import fs from 'fs';
import YAML from 'yaml';


const file = fs.readFileSync('config.yml', 'utf8')
const config = YAML.parse(file)

export class MailService { 
    transporter = nodemailer.createTransport({
    host: config.smtp.host,
    port: config.smtp.port,
    secure: false,
    requireTLS: true,
    auth: {
      user: config.smtp.username,
      pass: config.smtp.password,
    },
    logger: true
  });

    async sendEmail(email: string, subject: string, text: string, html: any): Promise<void> {
    const info = await this.transporter.sendMail({
        from: config.smtp.sender,
        to: email,
        subject: subject,
        text: text,
        html: html,
        headers: { 'x-myheader': 'test header' }
      });
    console.log("Message sent: %s", info.response);
  }
}