mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import * as nodemailer from 'nodemailer';
|
|
|
|
@Injectable()
|
|
export class EmailService {
|
|
private smtpConfig: any;
|
|
|
|
constructor(private readonly configService: ConfigService) {
|
|
this.smtpConfig = {
|
|
host: this.configService.get<string>('email-config.SMTP_HOST'),
|
|
port: this.configService.get<number>('email-config.SMTP_PORT'),
|
|
sender: this.configService.get<string>('email-config.SMTP_SENDER'),
|
|
secure: this.configService.get<boolean>('email-config.SMTP_SECURE'), // true for 465, false for other ports
|
|
auth: {
|
|
user: this.configService.get<string>('email-config.SMTP_USER'),
|
|
pass: this.configService.get<string>('email-config.SMTP_PASSWORD'),
|
|
},
|
|
};
|
|
}
|
|
|
|
async sendEmail(
|
|
email: string,
|
|
subject: string,
|
|
message: string,
|
|
): Promise<void> {
|
|
const transporter = nodemailer.createTransport(this.smtpConfig);
|
|
|
|
const mailOptions = {
|
|
from: this.smtpConfig.sender,
|
|
to: email,
|
|
subject,
|
|
text: message,
|
|
};
|
|
|
|
await transporter.sendMail(mailOptions);
|
|
}
|
|
}
|