1
- import type { EmailAddress , EmailMessage , EmailResult , RenderOptions , SendGridConfig } from '@stacksjs/types'
1
+ import type { EmailAddress , EmailMessage , EmailResult , RenderOptions } from '@stacksjs/types'
2
2
import { Buffer } from 'node:buffer'
3
+ import { config } from '@stacksjs/config'
3
4
import { log } from '@stacksjs/logging'
4
5
import { template } from '../template'
5
6
import { BaseEmailDriver } from './base'
@@ -8,9 +9,9 @@ export class SendGridDriver extends BaseEmailDriver {
8
9
public name = 'sendgrid'
9
10
private apiKey : string
10
11
11
- constructor ( config : SendGridConfig ) {
12
- super ( config )
13
- this . apiKey = config . apiKey
12
+ constructor ( ) {
13
+ super ( )
14
+ this . apiKey = config . email . drivers ?. sendgrid ?. apiKey ?? ''
14
15
}
15
16
16
17
public async send ( message : EmailMessage , options ?: RenderOptions ) : Promise < EmailResult > {
@@ -26,7 +27,6 @@ export class SendGridDriver extends BaseEmailDriver {
26
27
this . validateMessage ( message )
27
28
const templ = await template ( message . template , options )
28
29
29
- // Convert our message format to SendGrid format
30
30
const sendgridPayload = {
31
31
personalizations : [
32
32
{
@@ -37,7 +37,7 @@ export class SendGridDriver extends BaseEmailDriver {
37
37
} ,
38
38
] ,
39
39
from : {
40
- email : message . from . address ,
40
+ email : message . from . address || config . email . from ?. address ,
41
41
...( message . from . name && { name : message . from . name } ) ,
42
42
} ,
43
43
content : [
@@ -127,12 +127,15 @@ export class SendGridDriver extends BaseEmailDriver {
127
127
return response
128
128
}
129
129
catch ( error ) {
130
- if ( attempt < this . config . maxRetries ) {
131
- log . warn ( `[${ this . name } ] Email send failed, retrying (${ attempt } /${ this . config . maxRetries } )` )
132
- await new Promise ( resolve => setTimeout ( resolve , this . config . retryTimeout ) )
130
+ if ( attempt < ( config . email . drivers ?. sendgrid ?. maxRetries ?? 3 ) ) {
131
+ const retryTimeout = config . email . drivers ?. sendgrid ?. retryTimeout ?? 1000
132
+ log . warn ( `[${ this . name } ] Email send failed, retrying (${ attempt } /${ config . email . drivers ?. sendgrid ?. maxRetries ?? 3 } )` )
133
+ await new Promise ( resolve => setTimeout ( resolve , retryTimeout ) )
133
134
return this . sendWithRetry ( payload , attempt + 1 )
134
135
}
135
136
throw error
136
137
}
137
138
}
138
139
}
140
+
141
+ export default SendGridDriver
0 commit comments