🌳
pt0/serverF/emailF/getMailPropsF.mts
1import * as _ from 'lodash-es'
2import { tsSec } from '../secretsF/tsSecF.mts'
7import { getPubEnv } from '../getPubEnvF.mts'
9export const forceEtherealCtx = genContext()
11const isLocalDevHost = () => (getPubEnv().gpSiteHost as string | undefined)?.includes('.localhost:')
13export const getMailProps = async () => {
14 let {secretVal, mappedSecretName} = getReqJsonSecretH(tsSec('smtpSecretName'))
15 let {auth, ...restSec} = secretVal
16 const isEthereal = isLocalDevHost() || forceEtherealCtx.getStore() || _.includes(mappedSecretName, 'ethereal')
17 const isPostmark = _.includes(mappedSecretName, 'postmark')
18 const isGmail = _.includes(mappedSecretName, 'gmail')
19 if (isEthereal) {
20 auth = await getCachedEtherealCreds()
21 }
23 let fromEmail = auth.user
24 if (isPostmark) {
25 fromEmail = null
26 }
28 return {...restSec, isEthereal, isPostmark, fromEmail, isGmail, auth}
31export const getTransportProps = async ({isPostmarkBroadcast=false}) => {
32 const mailProps = await getMailProps()
33 const {isEthereal, isPostmark, isGmail, auth, host} = mailProps
35 if (isEthereal) {
36 return {
37 host: 'smtp.ethereal.email',
38 port: 587, auth,
39 }
40 }
42 if (isGmail) {
43 return {
44 host: 'smtp.gmail.com',
45 port: 465, secure: true,
46 auth,
47 }
48 }
50 if (isPostmark) {
51 return {
52 host: isPostmarkBroadcast ? 'smtp-broadcasts.postmarkapp.com' : 'smtp.postmarkapp.com',
53 port: 587, auth
54 }
55 }
56 if (host) {
57 return {host, auth}
58 }
60 throwDebugH({mailProps})