Skip to content

Commit 6b6aae2

Browse files
authored
fix(firestore-send-email): SendGrid v3 issues (#2020)
* fix(firestore-send-email): sendgrid v3 issues * fix(firestore-send-email): remove sendgrid transporter test * fix(firestore-send-email): handle sendgrid template edge cases * chore: clean up comments & logs --------- Co-authored-by: Pavel <k>
1 parent 1fafd35 commit 6b6aae2

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

firestore-send-email/functions/__tests__/helpers.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ describe("set server credentials helper function", () => {
216216
expect(regex.test(config.smtpConnectionUri)).toBe(false);
217217
});
218218

219+
/* Test removed due to the new SendGrid transporter logic - see setSendGridTransport()
219220
test("return a SendGrid transporter if the host is smtp.sendgrid.net", () => {
220221
const config: Config = {
221222
smtpConnectionUri: "smtps://apikey@smtp.sendgrid.net:465",
@@ -227,4 +228,5 @@ describe("set server credentials helper function", () => {
227228
const credentials = setSmtpCredentials(config);
228229
expect(credentials.transporter.name === "nodemailer-sendgrid").toBe(true);
229230
});
231+
*/
230232
});

firestore-send-email/functions/src/helpers.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ export function setSmtpCredentials(config: Config) {
7070
pass: decodeURIComponent(url.password),
7171
},
7272
});
73-
} else if (checkSendGrid(url.hostname)) {
74-
const options: sg.SendgridOptions = {
75-
apiKey: decodeURIComponent(url.password),
76-
};
77-
78-
transport = createTransport(sg(options));
7973
} else {
8074
transport = createTransport(url.href, {
8175
tls: parseTlsOptions(config.tls),
@@ -84,3 +78,13 @@ export function setSmtpCredentials(config: Config) {
8478

8579
return transport;
8680
}
81+
82+
export function setSendGridTransport(config: Config) {
83+
const { smtpPassword } = config;
84+
85+
const options: sg.SendgridOptions = {
86+
apiKey: smtpPassword,
87+
};
88+
89+
return createTransport(sg(options));
90+
}

firestore-send-email/functions/src/index.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import * as logs from "./logs";
2323
import config from "./config";
2424
import Templates from "./templates";
2525
import { QueuePayload } from "./types";
26-
import { parseTlsOptions, setSmtpCredentials } from "./helpers";
26+
import {
27+
parseTlsOptions,
28+
setSendGridTransport,
29+
setSmtpCredentials,
30+
} from "./helpers";
2731
import * as events from "./events";
2832

2933
logs.init();
@@ -292,16 +296,31 @@ async function deliver(
292296
try {
293297
payload = await preparePayload(payload);
294298

295-
// If the SMTP provider is SendGrid, we need to check if the payload contains
296-
// either a text or html content, or if the payload contains a SendGrid Dynamic Template.
297-
verifySendGridContent(payload);
298-
299299
if (!payload.to.length && !payload.cc.length && !payload.bcc.length) {
300300
throw new Error(
301301
"Failed to deliver email. Expected at least 1 recipient."
302302
);
303303
}
304304

305+
// Switch to SendGrid transport if SendGrid config is provided.
306+
if (payload.sendGrid) {
307+
transport = setSendGridTransport(config);
308+
309+
// Convert text and html to undefined if they are null
310+
if (payload.message) {
311+
if (payload.message.text == null) {
312+
payload.message.text = undefined;
313+
}
314+
if (payload.message.html == null) {
315+
payload.message.text = undefined;
316+
}
317+
}
318+
319+
// If the SMTP provider is SendGrid, we need to check if the payload contains
320+
// either a text or html content, or if the payload contains a SendGrid Dynamic Template.
321+
verifySendGridContent(payload);
322+
}
323+
305324
const result = await transport.sendMail({
306325
...Object.assign(payload.message ?? {}, {
307326
from: payload.from || config.defaultFrom,
@@ -315,6 +334,7 @@ async function deliver(
315334
mail_settings: payload.sendGrid?.mailSettings || {},
316335
}),
317336
});
337+
318338
const info = {
319339
messageId: result.messageId || null,
320340
accepted: result.accepted || [],
@@ -325,6 +345,7 @@ async function deliver(
325345

326346
update["delivery.state"] = "SUCCESS";
327347
update["delivery.info"] = info;
348+
328349
logs.delivered(ref, info);
329350
} catch (e) {
330351
update["delivery.state"] = "ERROR";

0 commit comments

Comments
 (0)