Skip to content

Commit 89c89e2

Browse files
committed
fix(emails): Intercept SMTP to Resend to try with REST API instead
Better debugging Resend email errors
1 parent 107f294 commit 89c89e2

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

packages/emails/src/providers/smtp/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
} from '../../../types/index';
88
import type SMTPTransport from 'nodemailer/lib/smtp-transport';
99
import nodemailer from 'nodemailer';
10+
import axios from 'axios';
1011
import parseTemplateToHtml from '../../parse-template-to-html';
1112

1213
const parseEmailAddrs = (emails: EmailAdrress | EmailAdrress[]) => {
@@ -79,6 +80,31 @@ const sendEmail = async (
7980
if (!transporter && smtpConfig) {
8081
setConfigSmtp(smtpConfig);
8182
}
83+
if (smtpConfig?.host === 'smtp.resend.com') {
84+
try {
85+
const { data } = await axios.post(
86+
'https://api.resend.io/v1/email/send',
87+
{
88+
...mailOptions,
89+
replyTo: undefined,
90+
reply_to: emailHeaders.replyTo,
91+
},
92+
{
93+
headers: { Authorization: `Bearer ${smtpConfig.auth.pass}` },
94+
},
95+
);
96+
return { status: 202, message: `emailId: #${data.id}` };
97+
} catch (err: any) {
98+
if (err.response) {
99+
const error: any = new Error('Error sending email with Resend API');
100+
error.request = err.config;
101+
error.status = err.response.status;
102+
error.response = err.response.data;
103+
throw new Error(error);
104+
}
105+
throw err;
106+
}
107+
}
82108
if (transporter) {
83109
const info = await transporter.sendMail(mailOptions);
84110
return { status: 202, message: `messageId: #${info.messageId}` };

packages/emails/types/index.d.ts

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DataEmailSendGrid } from './sendgrid'
44
type EmailAdrress = {
55
name: string,
66
email: string,
7-
}
7+
};
88

99
type EmailHeaders = {
1010
from: EmailAdrress,
@@ -14,9 +14,9 @@ type EmailHeaders = {
1414
sender?: EmailAdrress,
1515
replyTo?: EmailAdrress | EmailAdrress[]
1616
bcc?: EmailAdrress[],
17-
}
17+
};
1818

19-
type TemplateData = { [key: string]: any }
19+
type TemplateData = { [key: string]: any };
2020

2121
// TODO: in the future the template can be an object, to accept other types of template besides EJS
2222
type Template = string
@@ -32,46 +32,18 @@ type EmailConfig = {
3232
templateData?: TemplateData,
3333
templateId?: string,
3434
template?: string,
35-
}
35+
};
3636

3737
// https://nodemailer.com/smtp/oauth2/
38-
39-
type SmtpAuth = {
40-
user: string,
41-
pass: string,
42-
}
43-
44-
type SmtpAuthToken = {
45-
type: 'OAuth2',
46-
user: string,
47-
accessToken: string,
48-
}
49-
50-
type SmtpAuth3LO = {
51-
type: 'OAuth2',
52-
user: string,
53-
clientId: string,
54-
clientSecret: string,
55-
refreshToken: string,
56-
accessToken: string,
57-
expires: number,
58-
}
59-
60-
type SmtpAuth2LO = {
61-
type: 'OAuth2',
62-
user: string,
63-
serviceClient: string,
64-
privateKey?: string,
65-
accessToken: string,
66-
expires: number,
67-
}
68-
6938
type SmtpConfig = {
7039
host: string,
7140
port: number,
7241
secure: boolean,
73-
auth: SmtpAuth | SmtpAuthToken | SmtpAuth2LO | SmtpAuth3LO
74-
}
42+
auth: {
43+
user: string,
44+
pass: string,
45+
},
46+
};
7547

7648
export type {
7749
EmailAdrress,

0 commit comments

Comments
 (0)