Files
ebook/server/api/send-order.post.ts
koziavin 1b211c3148
All checks were successful
Deploy Nuxt App / deploy (push) Successful in 4m25s
fix
2025-06-22 09:25:44 +04:00

53 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import nodemailer from 'nodemailer'
export default defineEventHandler(async (event) => {
const { to } = await readBody(event)
if (!to) {
throw createError({
statusCode: 400,
statusMessage: 'Bad Request: "to" field is required',
})
}
const config = useRuntimeConfig(event)
// Убедимся, что все переменные окружения на месте
if (!config.smtpHost || !config.smtpPort || !config.smtpUser || !config.smtpPass) {
console.error('SMTP configuration is missing in runtime config.')
throw createError({
statusCode: 500,
statusMessage: 'Internal Server Error: SMTP configuration is incomplete.',
})
}
const transporter = nodemailer.createTransport({
host: config.smtpHost,
port: parseInt(config.smtpPort, 10),
secure: parseInt(config.smtpPort, 10) === 465, // true for 465, false for other ports
auth: {
user: config.smtpUser,
pass: config.smtpPass,
},
})
const mailOptions = {
from: `"Vino Galante" <${config.smtpUser}>`,
to: to, // Отправляем на email, полученный от клиента
bcc: config.smtpUser, // Отправляем скрытую копию самому себе для сохранения в "Отправленных"
subject: 'Ваш заказ принят!',
text: 'Спасибо за ваш заказ! Мы скоро свяжемся с вами для уточнения деталей.',
html: '<b>Спасибо за ваш заказ!</b><p>Мы скоро свяжемся с вами для уточнения деталей.</p>',
}
try {
await transporter.sendMail(mailOptions)
return { success: true, message: 'Email sent' }
} catch (error) {
console.error('Error sending email:', error)
throw createError({
statusCode: 500,
statusMessage: `Failed to send email: ${(error as Error).message || 'Unknown error'}`,
})
}
})