Add Mailtrap API token and invitation template functionality

This commit is contained in:
faris Aljohari
2024-12-28 19:41:24 -06:00
parent 769c9be73d
commit d56b26d3ea
3 changed files with 45 additions and 1 deletions

View File

@ -1,6 +1,8 @@
import { Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as nodemailer from 'nodemailer';
import axios from 'axios';
import { SEND_EMAIL_API_URL } from '../constants/mail-trap';
@Injectable()
export class EmailService {
@ -35,4 +37,42 @@ export class EmailService {
await transporter.sendMail(mailOptions);
}
async sendEmailWithInvitationTemplate(
email: string,
emailInvitationData: any,
): Promise<void> {
const API_TOKEN = this.configService.get<string>(
'email-config.MAILTRAP_API_TOKEN',
);
const TEMPLATE_UUID = this.configService.get<string>(
'email-config.MAILTRAP_INVITATION_TEMPLATE_UUID',
);
const emailData = {
from: {
email: this.configService.get<string>('email-config.SMTP_SENDER'),
},
to: [
{
email: email,
},
],
template_uuid: TEMPLATE_UUID,
template_variables: emailInvitationData,
};
try {
await axios.post(SEND_EMAIL_API_URL, emailData, {
headers: {
Authorization: `Bearer ${API_TOKEN}`,
'Content-Type': 'application/json',
},
});
} catch (error) {
throw new HttpException(
error.message || 'Error sending email using Mailtrap template',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}