Skip to content

Commit 49cf296

Browse files
chore: wip
1 parent 1acfb83 commit 49cf296

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

config/email.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export default {
3737

3838
sendgrid: {
3939
apiKey: env.SENDGRID_API_KEY,
40+
maxRetries: env.SENDGRID_MAX_RETRIES ? Number.parseInt(env.SENDGRID_MAX_RETRIES) : 3,
41+
retryTimeout: env.SENDGRID_RETRY_TIMEOUT ? Number.parseInt(env.SENDGRID_RETRY_TIMEOUT) : 1000,
4042
},
4143

4244
mailtrap: {

storage/framework/core/email/src/drivers/sendgrid.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { EmailAddress, EmailMessage, EmailResult, RenderOptions, SendGridConfig } from '@stacksjs/types'
1+
import type { EmailAddress, EmailMessage, EmailResult, RenderOptions } from '@stacksjs/types'
22
import { Buffer } from 'node:buffer'
3+
import { config } from '@stacksjs/config'
34
import { log } from '@stacksjs/logging'
45
import { template } from '../template'
56
import { BaseEmailDriver } from './base'
@@ -8,9 +9,9 @@ export class SendGridDriver extends BaseEmailDriver {
89
public name = 'sendgrid'
910
private apiKey: string
1011

11-
constructor(config: SendGridConfig) {
12-
super(config)
13-
this.apiKey = config.apiKey
12+
constructor() {
13+
super()
14+
this.apiKey = config.email.drivers?.sendgrid?.apiKey ?? ''
1415
}
1516

1617
public async send(message: EmailMessage, options?: RenderOptions): Promise<EmailResult> {
@@ -26,7 +27,6 @@ export class SendGridDriver extends BaseEmailDriver {
2627
this.validateMessage(message)
2728
const templ = await template(message.template, options)
2829

29-
// Convert our message format to SendGrid format
3030
const sendgridPayload = {
3131
personalizations: [
3232
{
@@ -37,7 +37,7 @@ export class SendGridDriver extends BaseEmailDriver {
3737
},
3838
],
3939
from: {
40-
email: message.from.address,
40+
email: message.from.address || config.email.from?.address,
4141
...(message.from.name && { name: message.from.name }),
4242
},
4343
content: [
@@ -127,12 +127,15 @@ export class SendGridDriver extends BaseEmailDriver {
127127
return response
128128
}
129129
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))
133134
return this.sendWithRetry(payload, attempt + 1)
134135
}
135136
throw error
136137
}
137138
}
138139
}
140+
141+
export default SendGridDriver

storage/framework/core/types/src/email.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface EmailOptions {
2727
}
2828
sendgrid?: {
2929
apiKey?: string
30+
maxRetries?: number
31+
retryTimeout?: number
3032
}
3133
mailtrap?: {
3234
token?: string

0 commit comments

Comments
 (0)