@@ -23,7 +23,11 @@ import * as logs from "./logs";
23
23
import config from "./config" ;
24
24
import Templates from "./templates" ;
25
25
import { QueuePayload } from "./types" ;
26
- import { setSendGridTransport , setSmtpCredentials } from "./helpers" ;
26
+ import {
27
+ parseTlsOptions ,
28
+ setSendGridTransport ,
29
+ setSmtpCredentials ,
30
+ } from "./helpers" ;
27
31
import * as events from "./events" ;
28
32
29
33
logs . init ( ) ;
@@ -282,6 +286,7 @@ async function deliver(
282
286
}
283
287
284
288
logs . attemptingDelivery ( ref ) ;
289
+ functions . logger . warn ( "here 0" ) ;
285
290
const update = {
286
291
"delivery.attempts" : FieldValue . increment ( 1 ) ,
287
292
"delivery.endTime" : FieldValue . serverTimestamp ( ) ,
@@ -290,29 +295,35 @@ async function deliver(
290
295
} ;
291
296
292
297
try {
298
+ functions . logger . warn ( "here 1" ) ;
293
299
payload = await preparePayload ( payload ) ;
294
300
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
-
299
301
if ( ! payload . to . length && ! payload . cc . length && ! payload . bcc . length ) {
300
302
throw new Error (
301
303
"Failed to deliver email. Expected at least 1 recipient."
302
304
) ;
303
305
}
304
306
307
+ functions . logger . warn ( "here 2" ) ;
308
+
305
309
// Switch to SendGrid transport if SendGrid config is provided
306
310
if ( payload . sendGrid ) {
311
+ functions . logger . warn ( "here 3" ) ;
307
312
transport = setSendGridTransport ( config ) ;
308
- }
309
313
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
+ }
313
323
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 ) ;
316
327
}
317
328
318
329
const result = await transport . sendMail ( {
@@ -328,6 +339,7 @@ async function deliver(
328
339
mail_settings : payload . sendGrid ?. mailSettings || { } ,
329
340
} ) ,
330
341
} ) ;
342
+ functions . logger . warn ( "here 6" ) ;
331
343
const info = {
332
344
messageId : result . messageId || null ,
333
345
accepted : result . accepted || [ ] ,
@@ -338,11 +350,15 @@ async function deliver(
338
350
339
351
update [ "delivery.state" ] = "SUCCESS" ;
340
352
update [ "delivery.info" ] = info ;
353
+ functions . logger . warn ( "here 7" ) ;
341
354
logs . delivered ( ref , info ) ;
355
+ functions . logger . warn ( "here 8" ) ;
342
356
} catch ( e ) {
357
+ functions . logger . warn ( "here 9" ) ;
343
358
update [ "delivery.state" ] = "ERROR" ;
344
359
update [ "delivery.error" ] = e . toString ( ) ;
345
360
logs . deliveryError ( ref , e ) ;
361
+ functions . logger . warn ( "here 10" ) ;
346
362
}
347
363
348
364
// Wrapping in transaction to allow for automatic retries (#48)
@@ -351,6 +367,7 @@ async function deliver(
351
367
// since the email sending will have been attempted regardless of what the
352
368
// delivery state was at that point, so we just update the state to reflect
353
369
// the result of the last attempt so as to not potentially cause duplicate sends.
370
+ functions . logger . warn ( "here 11" ) ;
354
371
transaction . update ( ref , update ) ;
355
372
return Promise . resolve ( ) ;
356
373
} ) ;
0 commit comments