Skip to content

Commit 3fe8e86

Browse files
Pavelcabljac
authored andcommitted
fix(firestore-send-email): handle sendgrid template edge cases
1 parent 9d897d1 commit 3fe8e86

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function setSendGridTransport(config: Config) {
9191
const { smtpPassword } = config;
9292

9393
const options: sg.SendgridOptions = {
94-
apiKey: decodeURIComponent(smtpPassword),
94+
apiKey: smtpPassword,
9595
};
9696

9797
return createTransport(sg(options));

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

Lines changed: 28 additions & 11 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 { setSendGridTransport, setSmtpCredentials } from "./helpers";
26+
import {
27+
parseTlsOptions,
28+
setSendGridTransport,
29+
setSmtpCredentials,
30+
} from "./helpers";
2731
import * as events from "./events";
2832

2933
logs.init();
@@ -282,6 +286,7 @@ async function deliver(
282286
}
283287

284288
logs.attemptingDelivery(ref);
289+
functions.logger.warn("here 0");
285290
const update = {
286291
"delivery.attempts": FieldValue.increment(1),
287292
"delivery.endTime": FieldValue.serverTimestamp(),
@@ -290,29 +295,35 @@ async function deliver(
290295
};
291296

292297
try {
298+
functions.logger.warn("here 1");
293299
payload = await preparePayload(payload);
294300

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-
299301
if (!payload.to.length && !payload.cc.length && !payload.bcc.length) {
300302
throw new Error(
301303
"Failed to deliver email. Expected at least 1 recipient."
302304
);
303305
}
304306

307+
functions.logger.warn("here 2");
308+
305309
// Switch to SendGrid transport if SendGrid config is provided
306310
if (payload.sendGrid) {
311+
functions.logger.warn("here 3");
307312
transport = setSendGridTransport(config);
308-
}
309313

310-
if (payload.message?.text == null) {
311-
delete payload.message.text;
312-
}
314+
// Convert text and html to undefined if they are null
315+
if (payload.message) {
316+
if (payload.message.text == null) {
317+
payload.message.text = undefined;
318+
}
319+
if (payload.message.html == null) {
320+
payload.message.text = undefined;
321+
}
322+
}
313323

314-
if (payload.message?.html == null) {
315-
delete payload.message.html;
324+
// If the SMTP provider is SendGrid, we need to check if the payload contains
325+
// either a text or html content, or if the payload contains a SendGrid Dynamic Template.
326+
verifySendGridContent(payload);
316327
}
317328

318329
const result = await transport.sendMail({
@@ -328,6 +339,7 @@ async function deliver(
328339
mail_settings: payload.sendGrid?.mailSettings || {},
329340
}),
330341
});
342+
functions.logger.warn("here 6");
331343
const info = {
332344
messageId: result.messageId || null,
333345
accepted: result.accepted || [],
@@ -338,11 +350,15 @@ async function deliver(
338350

339351
update["delivery.state"] = "SUCCESS";
340352
update["delivery.info"] = info;
353+
functions.logger.warn("here 7");
341354
logs.delivered(ref, info);
355+
functions.logger.warn("here 8");
342356
} catch (e) {
357+
functions.logger.warn("here 9");
343358
update["delivery.state"] = "ERROR";
344359
update["delivery.error"] = e.toString();
345360
logs.deliveryError(ref, e);
361+
functions.logger.warn("here 10");
346362
}
347363

348364
// Wrapping in transaction to allow for automatic retries (#48)
@@ -351,6 +367,7 @@ async function deliver(
351367
// since the email sending will have been attempted regardless of what the
352368
// delivery state was at that point, so we just update the state to reflect
353369
// the result of the last attempt so as to not potentially cause duplicate sends.
370+
functions.logger.warn("here 11");
354371
transaction.update(ref, update);
355372
return Promise.resolve();
356373
});

0 commit comments

Comments
 (0)