1import * as _ from 'lodash-es'2import { tsSec } from '../secretsF/tsSecF.mts'3import { getReqJsonSecretH } from '../secretsF/getReqJsonSecretHF.mts'4import { getCachedEtherealCreds } from './etherealF.mts'5import { genContext } from '../genContextF.mts'6import { throwDebugH } from '../throwErrorF/throwDebugHF.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} = secretVal16 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.user24 if (isPostmark) {25 fromEmail = null26 }28 return {...restSec, isEthereal, isPostmark, fromEmail, isGmail, auth}29}31export const getTransportProps = async ({isPostmarkBroadcast=false}) => {32 const mailProps = await getMailProps()33 const {isEthereal, isPostmark, isGmail, auth, host} = mailProps35 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, auth54 }55 }56 if (host) {57 return {host, auth}58 }60 throwDebugH({mailProps})61}