| 1 | import { Injectable, OnModuleInit, Logger } from '@nestjs/common'; |
| 2 | import * as nodemailer from 'nodemailer'; |
| 3 | |
| 4 | @Injectable() |
| 5 | export class EmailService implements OnModuleInit { |
| 6 | private transporter: nodemailer.Transporter; |
| 7 | private readonly logger = new Logger(EmailService.name); |
| 8 | |
| 9 | onModuleInit() { |
| 10 | this.transporter = nodemailer.createTransport({ |
| 11 | host: process.env.SMTP_HOST || 'localhost', |
| 12 | port: parseInt(process.env.SMTP_PORT || '1025', 10), |
| 13 | secure: false, |
| 14 | }); |
| 15 | |
| 16 | this.logger.log('Email service initialized'); |
| 17 | } |
| 18 | |
| 19 | async sendEmail(to: string, subject: string, html: string): Promise<void> { |
| 20 | try { |
| 21 | await this.transporter.sendMail({ |
| 22 | from: '"EventflowApp" <noreply@eventflowapp.com>', |
| 23 | to, |
| 24 | subject, |
| 25 | html, |
| 26 | }); |
| 27 | |
| 28 | this.logger.log(`Email sent to ${to}`); |
| 29 | } catch (error) { |
| 30 | this.logger.error(`Error sending email to ${to}: ${error}`); |
| 31 | } |
| 32 | } |
| 33 | } |