@@ -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 { parseTlsOptions , 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 ( ) ;
@@ -292,16 +296,31 @@ async function deliver(
292
296
try {
293
297
payload = await preparePayload ( payload ) ;
294
298
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
299
if ( ! payload . to . length && ! payload . cc . length && ! payload . bcc . length ) {
300
300
throw new Error (
301
301
"Failed to deliver email. Expected at least 1 recipient."
302
302
) ;
303
303
}
304
304
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
+
305
324
const result = await transport . sendMail ( {
306
325
...Object . assign ( payload . message ?? { } , {
307
326
from : payload . from || config . defaultFrom ,
@@ -315,6 +334,7 @@ async function deliver(
315
334
mail_settings : payload . sendGrid ?. mailSettings || { } ,
316
335
} ) ,
317
336
} ) ;
337
+
318
338
const info = {
319
339
messageId : result . messageId || null ,
320
340
accepted : result . accepted || [ ] ,
@@ -325,6 +345,7 @@ async function deliver(
325
345
326
346
update [ "delivery.state" ] = "SUCCESS" ;
327
347
update [ "delivery.info" ] = info ;
348
+
328
349
logs . delivered ( ref , info ) ;
329
350
} catch ( e ) {
330
351
update [ "delivery.state" ] = "ERROR" ;
0 commit comments