-
Notifications
You must be signed in to change notification settings - Fork 6
/
mail.ts
43 lines (36 loc) · 943 Bytes
/
mail.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import wretch from 'wretch'
import { env } from '~/env.mjs'
import sendgrid from '@sendgrid/mail'
type SendMailParams = {
recipient: string
body: string
subject: string
}
if (env.SENDGRID_API_KEY) {
sendgrid.setApiKey(env.SENDGRID_API_KEY)
}
export const sgClient = env.SENDGRID_API_KEY ? sendgrid : null
export const sendMail = async (params: SendMailParams): Promise<void> => {
if (env.POSTMAN_API_KEY) {
return await wretch(
'https://api.postman.gov.sg/v1/transactional/email/send',
)
.auth(`Bearer ${env.POSTMAN_API_KEY}`)
.post(params)
.res()
}
if (sgClient && env.SENDGRID_FROM_ADDRESS) {
await sgClient.send({
from: env.SENDGRID_FROM_ADDRESS,
to: params.recipient,
subject: params.subject,
html: params.body,
})
return
}
console.warn(
'POSTMAN_API_KEY or SENDGRID_API_KEY missing. Logging the following mail: ',
params,
)
return
}