import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import * as nodemailer from 'nodemailer'; @Injectable() export class MailService { private readonly logger = new Logger(MailService.name); private transporter: nodemailer.Transporter; constructor(private readonly configService: ConfigService) { this.transporter = nodemailer.createTransport({ host: this.configService.get('SMTP_HOST', ''), port: this.configService.get('SMTP_PORT', 587), secure: this.configService.get('SMTP_SECURE', 'false') === 'true', auth: { user: this.configService.get('SMTP_USER', ''), pass: this.configService.get('SMTP_PASS', ''), }, }); } async sendMail(options: { to: string; subject: string; body: string; html?: string; attachments?: { filename: string; content: Buffer }[]; }): Promise { const from = this.configService.get('SMTP_FROM', 'paperless@localhost'); await this.transporter.sendMail({ from, to: options.to, subject: options.subject, text: options.body, html: options.html, attachments: options.attachments?.map(a => ({ filename: a.filename, content: a.content, })), }); this.logger.log(`Mail gesendet an ${options.to}: "${options.subject}"`); } }