Help is on the way! To mark it as solved, use the /solve command. In the meantime, here are some existing threads that may help you:
#Order confirmation
17 messages · Page 1 of 1 (latest)
Yeah @limber burrow nailed it with the email functionality. Just to add to it, I would look at using Payload’s hook system to send the emails. For example the AfterChange hook would be a good time to send the email if the operation === ‘create’
You can access the current user too via the req object, this will provide their ID (or any custom info you need)
You can send the email via the local API methods, or hooks
I placed example of order confirmation with email via hook in that thread: https://discord.com/channels/967097582721572934/1214378743078850630 ID of that order you will get from 'doc'. Look at my example. I was get user email address and total order amount from 'doc' object.
Thanks lads, appreciate it
so i directly placed in it inside the c ollections
`async ({ doc, operation }) => {
if (operation === 'create') {
const { customerEmail, items, total, id: orderId } = doc
try {
await payload.sendEmail({
from: process.env.EMAIL_USER,
to: customerEmail,
subject: 'Order Confirmation',
html: `
"html content"
`,
})
console.log('Order confirmation email sent successfully to:', customerEmail)
} catch (error: unknown) {
console.error('Error sending email:', error)
}
}
},`
and i get this error in the terminal
`[18:46:37] ERROR (payload): Failed to send mail to undefined, subject: Order Confirmation
err: {
"type": "Error",
"message": "No recipients defined",
"stack":
Error: No recipients defined
at SMTPConnection._formatError (B:\VSCode Projects\personal-projects\web-dev\official-nyla\node_modules\nodemailer\lib\smtp-connection\index.js:790:19)
at SMTPConnection._setEnvelope (B:\VSCode Projects\personal-projects\web-dev\official-nyla\node_modules\nodemailer\lib\smtp-connection\index.js:1023:34)
at SMTPConnection.send (B:\VSCode Projects\personal-projects\web-dev\official-nyla\node_modules\nodemailer\lib\smtp-connection\index.js:621:14)
at sendMessage (B:\VSCode Projects\personal-projects\web-dev\official-nyla\node_modules\nodemailer\lib\smtp-transport\index.js:228:28)
at B:\VSCode Projects\personal-projects\web-dev\official-nyla\node_modules\nodemailer\lib\smtp-transport\index.js:286:25
at SMTPConnection._actionAUTHComplete (B:\VSCode Projects\personal-projects\web-dev\official-nyla\node_modules\nodemailer\lib\smtp-connection\index.js:1578:9) at SMTPConnection.<anonymous> (B:\VSCode Projects\personal-projects\web-dev\official-nyla\node_modules\nodemailer\lib\smtp-connection\index.js:1757:22) at SMTPConnection._processResponse (B:\VSCode Projects\personal-projects\web-dev\official-nyla\node_modules\nodemailer\lib\smtp-connection\index.js:969:20) at SMTPConnection._onData (B:\VSCode Projects\personal-projects\web-dev\official-nyla\node_modules\nodemailer\lib\smtp-connection\index.js:755:14) at TLSSocket.SMTPConnection._onSocketData (B:\VSCode Projects\personal-projects\web-dev\official-nyla\node_modules\nodemailer\lib\smtp-connection\index.js:193:44) at TLSSocket.emit (node:events:514:28) at TLSSocket.emit (node:domain:488:12) at addChunk (node:internal/streams/readable:545:12) at readableAddChunkPushByteMode (node:internal/streams/readable:495:3) at TLSSocket.Readable.push (node:internal/streams/readable:375:5) at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) at TLSWrap.callbackTrampoline (node:internal/async_hooks:130:17) "code": "EENVELOPE", "command": "API" }
i tried creating a seperate hook for it but i kept getting type errors
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { margin: 0; padding: 0; box-sizing: border-box; background-color: #F4F4F5; color: #18181B; } .email-container { max-width: 600px; margin: 40px auto; padding: 20px; background-color: #FFFFFF; border-radius: 8px; text-align: center; box-shadow: 0 4px 8px rgba(0,0,0,0.1); } .email-header { font-size: 28px; color: #18181B; margin-bottom: 20px; } .email-content { font-size: 16px; line-height: 1.5; margin-bottom: 30px; } ul { text-align: left; list-style-type: none; padding: 0; } li { margin: 10px 0; } @media (max-width: 600px) { .email-header { font-size: 24px; } } </style>
</head> <body> <div class="email-container"> <h1 class="email-header">NYLA Order Confirmation - Order ID: ${orderId}</h1> <div class="email-content"> <p>Hi,</p> <p>Thank you for your order. Here is your order summary:</p> <ul> ${items .map( (item: { name: string; quantity: number; price: number }) => <li>${item.name} - ${item.quantity} x $${item.price}</li>, ) .join('')} </ul> <p><strong>Total: $${total}</strong></p> <p>If you have any questions or need to make changes to your order, please contact us.</p> </div> </div> </body> </html> ,
the html content
what am i doing wrong?
@high haven do you see anything wrong?