feat: implement stdout email adapter, use if no adapter configured
This commit is contained in:
23
packages/payload/src/email/getStringifiedToAddress.ts
Normal file
23
packages/payload/src/email/getStringifiedToAddress.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { SendMailOptions } from 'nodemailer'
|
||||
|
||||
export const getStringifiedToAddress = (message: SendMailOptions): string | undefined => {
|
||||
let stringifiedTo: string | undefined
|
||||
|
||||
if (typeof message.to === 'string') {
|
||||
stringifiedTo = message.to
|
||||
} else if (Array.isArray(message.to)) {
|
||||
stringifiedTo = message.to
|
||||
.map((to) => {
|
||||
if (typeof to === 'string') {
|
||||
return to
|
||||
} else if (to.address) {
|
||||
return to.address
|
||||
}
|
||||
return ''
|
||||
})
|
||||
.join(', ')
|
||||
} else if (message.to.address) {
|
||||
stringifiedTo = message.to.address
|
||||
}
|
||||
return stringifiedTo
|
||||
}
|
||||
@@ -2,30 +2,15 @@ import type { SendMailOptions } from 'nodemailer'
|
||||
|
||||
import type { Payload } from '../types/index.js'
|
||||
|
||||
import { getStringifiedToAddress } from './getStringifiedToAddress.js'
|
||||
|
||||
export async function sendEmail(this: Payload, message: SendMailOptions): Promise<unknown> {
|
||||
let result
|
||||
|
||||
try {
|
||||
result = await this.email.sendEmail(message)
|
||||
} catch (err: unknown) {
|
||||
let stringifiedTo: string | undefined
|
||||
|
||||
if (typeof message.to === 'string') {
|
||||
stringifiedTo = message.to
|
||||
} else if (Array.isArray(message.to)) {
|
||||
stringifiedTo = message.to
|
||||
.map((to) => {
|
||||
if (typeof to === 'string') {
|
||||
return to
|
||||
} else if (to.address) {
|
||||
return to.address
|
||||
}
|
||||
return ''
|
||||
})
|
||||
.join(', ')
|
||||
} else if (message.to.address) {
|
||||
stringifiedTo = message.to.address
|
||||
}
|
||||
const stringifiedTo = getStringifiedToAddress(message)
|
||||
|
||||
this.logger.error({
|
||||
err,
|
||||
|
||||
23
packages/payload/src/email/stdoutAdapter.ts
Normal file
23
packages/payload/src/email/stdoutAdapter.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { SendMailOptions } from 'nodemailer'
|
||||
|
||||
import type { Payload } from '../index.js'
|
||||
import type { EmailAdapter } from './types.js'
|
||||
|
||||
import { emailDefaults } from './defaults.js'
|
||||
import { getStringifiedToAddress } from './getStringifiedToAddress.js'
|
||||
|
||||
export type StdoutAdapter = EmailAdapter<SendMailOptions, void>
|
||||
|
||||
export const createStdoutAdapter = (payload: Payload) => {
|
||||
const stdoutAdapter: StdoutAdapter = {
|
||||
defaultFromAddress: emailDefaults.defaultFromAddress,
|
||||
defaultFromName: emailDefaults.defaultFromName,
|
||||
sendEmail: async (message) => {
|
||||
const stringifiedTo = getStringifiedToAddress(message)
|
||||
const res = `EMAIL NON-DELIVERY. To: '${stringifiedTo}', Subject: '${message.subject}'`
|
||||
payload.logger.info({ msg: res })
|
||||
return Promise.resolve()
|
||||
},
|
||||
}
|
||||
return stdoutAdapter
|
||||
}
|
||||
@@ -49,6 +49,7 @@ import { APIKeyAuthentication } from './auth/strategies/apiKey.js'
|
||||
import { JWTAuthentication } from './auth/strategies/jwt.js'
|
||||
import localOperations from './collections/operations/local/index.js'
|
||||
import { validateSchema } from './config/validate.js'
|
||||
import { createStdoutAdapter } from './email/stdoutAdapter.js'
|
||||
import { fieldAffectsData } from './exports/types.js'
|
||||
import localGlobalOperations from './globals/operations/local/index.js'
|
||||
import flattenFields from './utilities/flattenTopLevelFields.js'
|
||||
@@ -366,8 +367,14 @@ export class BasePayload<TGeneratedTypes extends GeneratedTypes> {
|
||||
// Load email adapter
|
||||
if (this.config.email instanceof Promise) {
|
||||
this.email = await this.config.email
|
||||
} else {
|
||||
} else if (this.config.email) {
|
||||
this.email = this.config.email
|
||||
} else {
|
||||
this.logger.warn(
|
||||
`No email adapter provided. Email will be written to stdout. More info at https://payloadcms.com/docs/email/overview.`,
|
||||
)
|
||||
|
||||
this.email = createStdoutAdapter(this)
|
||||
}
|
||||
|
||||
this.sendEmail = this.email.sendEmail
|
||||
|
||||
Reference in New Issue
Block a user