change to smtp mailer

This commit is contained in:
Ammar Qaffaf
2024-02-27 04:26:03 +03:00
parent 1bfcbf0904
commit 563278170c
4 changed files with 19 additions and 17 deletions

View File

@ -27,6 +27,6 @@ async function bootstrap() {
app.useGlobalPipes(new ValidationPipe()); app.useGlobalPipes(new ValidationPipe());
await app.listen(6001); await app.listen(7001);
} }
bootstrap(); bootstrap();

View File

@ -21,6 +21,6 @@ async function bootstrap() {
}), }),
); );
app.useGlobalPipes(new ValidationPipe()); app.useGlobalPipes(new ValidationPipe());
await app.listen(6000); await app.listen(7000);
} }
bootstrap(); bootstrap();

View File

@ -3,7 +3,10 @@ import { registerAs } from '@nestjs/config';
export default registerAs( export default registerAs(
'email-config', 'email-config',
(): Record<string, any> => ({ (): Record<string, any> => ({
EMAIL_ID: process.env.EMAIL_USER, SMTP_HOST: process.env.SMTP_HOST,
PASSWORD: process.env.EMAIL_PASSWORD, SMTP_PORT: parseInt(process.env.SMTP_PORT),
SMTP_SECURE: process.env.SMTP_SECURE === 'true',
SMTP_USER: process.env.SMTP_USER,
SMTP_PASSWORD: process.env.SMTP_PASSWORD,
}), }),
); );

View File

@ -4,12 +4,18 @@ import * as nodemailer from 'nodemailer';
@Injectable() @Injectable()
export class EmailService { export class EmailService {
private user: string; private smtpConfig: any;
private pass: string;
constructor(private readonly configService: ConfigService) { constructor(private readonly configService: ConfigService) {
this.user = this.configService.get<string>('email-config.EMAIL_ID'); this.smtpConfig = {
this.pass = this.configService.get<string>('email-config.PASSWORD'); host: this.configService.get<string>('email-config.SMTP_HOST'),
port: this.configService.get<number>('email-config.SMTP_PORT'),
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 sendOTPEmail( async sendOTPEmail(
@ -17,17 +23,10 @@ export class EmailService {
subject: string, subject: string,
message: string, message: string,
): Promise<void> { ): Promise<void> {
const transporter = nodemailer.createTransport(this.smtpConfig);
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: this.user,
pass: this.pass,
},
});
const mailOptions = { const mailOptions = {
from: this.user, from: this.smtpConfig.auth.user,
to: email, to: email,
subject, subject,
text: message, text: message,