Skip to content

Commit 349a223

Browse files
feat/support smtp email provider
Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com>
1 parent c1c9356 commit 349a223

3 files changed

Lines changed: 66 additions & 3 deletions

File tree

libs/common/src/email.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Injectable, Logger } from '@nestjs/common';
33
import { CommonConstants } from './common.constant';
44
import { EmailDto } from './dtos/email.dto';
55
import { sendWithResend } from './resend-helper-file';
6+
import { sendWithSMTP } from './smtp-helper-file';
67
import { sendWithSendGrid } from './send-grid-helper-file';
78

89
@Injectable()
@@ -23,6 +24,9 @@ export class EmailService {
2324
case 'resend':
2425
result = await sendWithResend(emailDto);
2526
break;
27+
case 'smtp':
28+
result = await sendWithSMTP(emailDto);
29+
break;
2630
default:
2731
this.logger.warn(`Unknown email provider: ${provider}, defaulting to SendGrid.`);
2832
result = await sendWithSendGrid(emailDto);

libs/common/src/resend-helper-file.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ import { Resend } from 'resend';
66

77
dotenv.config();
88

9+
const emailProvider = process.env.EMAIL_PROVIDER;
910
const apiKey = process.env.RESEND_API_KEY;
10-
if (!apiKey) {
11-
throw new Error('Missing RESEND_API_KEY in environment variables.');
11+
12+
let resend: Resend | null = null;
13+
14+
if ('resend' === emailProvider) {
15+
if (!apiKey) {
16+
throw new Error('Missing RESEND_API_KEY in environment variables.');
17+
}
18+
resend = new Resend(apiKey);
1219
}
13-
const resend = new Resend(apiKey);
1420

1521
export const sendWithResend = async (emailDto: EmailDto): Promise<boolean> => {
1622
try {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as dotenv from 'dotenv';
2+
import * as nodemailer from 'nodemailer';
3+
4+
import { EmailDto } from './dtos/email.dto';
5+
import { Logger } from '@nestjs/common';
6+
7+
dotenv.config();
8+
9+
const emailProvider = process.env.EMAIL_PROVIDER?.toLowerCase();
10+
11+
let transporter: nodemailer.Transporter | null = null;
12+
13+
if ('smtp' === emailProvider) {
14+
const { SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS } = process.env;
15+
16+
if (!SMTP_HOST || !SMTP_PORT || !SMTP_USER || !SMTP_PASS) {
17+
throw new Error('Missing SMTP configuration. Required: SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS');
18+
}
19+
20+
transporter = nodemailer.createTransport({
21+
host: SMTP_HOST,
22+
port: Number(SMTP_PORT),
23+
secure: 465 === Number(SMTP_PORT),
24+
auth: {
25+
user: SMTP_USER,
26+
pass: SMTP_PASS
27+
},
28+
requireTLS: 587 === Number(SMTP_PORT)
29+
});
30+
}
31+
32+
export const sendWithSMTP = async (emailDto: EmailDto): Promise<boolean> => {
33+
if (!transporter) {
34+
Logger.error('SMTP email provider is not initialized');
35+
return false;
36+
}
37+
38+
try {
39+
await transporter.sendMail({
40+
from: emailDto.emailFrom,
41+
to: emailDto.emailTo,
42+
subject: emailDto.emailSubject,
43+
text: emailDto.emailText,
44+
html: emailDto.emailHtml,
45+
attachments: emailDto.emailAttachments
46+
});
47+
48+
return true;
49+
} catch (error) {
50+
Logger.error('Error while sending email with SMTP', error);
51+
return false;
52+
}
53+
};

0 commit comments

Comments
 (0)