fix
All checks were successful
Deploy Nuxt App / deploy (push) Successful in 4m25s

This commit is contained in:
2025-06-22 09:25:44 +04:00
parent 925b6197f2
commit 1b211c3148
6 changed files with 74 additions and 33 deletions

2
.env
View File

@ -14,3 +14,5 @@ APP_URL=
SMTP_USER=levishub@yandex.com
SMTP_PASS=avhpihoudpyvibtx
DEFAULT_TO_EMAIL=miduway@yandex.ru
SMTP_HOST=smtp.yandex.ru
SMTP_PORT=465

View File

@ -16,3 +16,5 @@ APP_URL=
SMTP_USER=levishub@yandex.ru
SMTP_PASS=avhpihoudpyvibtx
DEFAULT_TO_EMAIL=miduway@yandex.ru
SMTP_HOST=smtp.yandex.ru
SMTP_PORT=465

View File

@ -30,8 +30,6 @@
<script setup>
import { ref, computed } from 'vue'
const mail = useMail()
const to = ref('')
const status = ref('')
const isLoading = ref(false)
@ -40,10 +38,10 @@ const statusClass = computed(() => {
if (status.value.includes('Ошибка')) {
return 'bg-red-100 text-red-700 border border-red-200'
}
if (status.value.includes('отправлено') || status.value.includes('подписка')) {
if (status.value.includes('Успех')) {
return 'bg-green-100 text-green-700 border border-green-200'
}
return 'bg-blue-100 text-blue-700 border border-blue-200'
return 'bg-blue-100 text-blue-700 border-blue-200'
})
async function onSubmit() {
@ -56,21 +54,16 @@ async function onSubmit() {
status.value = ''
try {
const emailData = {
to: to.value,
subject: 'Новая подписка на рассылку книги!',
text: `Пользователь с e-mail: ${to.value} подписался на рассылку книги.`,
}
await $fetch('/api/send-order', {
method: 'POST',
body: { to: to.value },
})
await mail.send(emailData)
status.value = 'Успешная подписка! Проверьте вашу почту.'
// Очищаем форму
status.value = 'Успех! Письмо с подтверждением отправлено на вашу почту.'
to.value = ''
} catch (error) {
console.error('Ошибка отправки письма:', error)
status.value = `Ошибка отправки: ${error.message || 'Неизвестная ошибка'}`
status.value = `Ошибка отправки: ${error?.data?.message || error.message || 'Неизвестная ошибка'}`
} finally {
isLoading.value = false
}

View File

@ -42,24 +42,15 @@ export default defineNuxtConfig({
},
},
runtimeConfig: {
smtpHost: process.env.SMTP_HOST,
smtpPort: process.env.SMTP_PORT,
smtpUser: process.env.SMTP_USER,
smtpPass: process.env.SMTP_PASS,
},
modules: [
...config.modules,
[
'nuxt-mail',
{
smtp: {
host: 'smtp.yandex.ru',
port: 465,
secure: true,
auth: {
user: process.env.SMTP_USER || 'levishub@yandex.com',
pass: process.env.SMTP_PASS || 'avhpihoudpyvibtx',
},
},
message: {
to: process.env.DEFAULT_TO_EMAIL || 'default@example.com',
},
},
],
// ['nuxt-mail', ... ] // Модуль больше не нужен
],
})

View File

@ -29,7 +29,6 @@
"husky": "^9.1.7",
"nodemailer": "^7.0.3",
"nuxt": "^3.17.5",
"nuxt-mail": "^6.0.2",
"nuxt-schema-org": "^5.0.5",
"pg": "^8.16.2",
"postgres": "^3.4.7",
@ -41,6 +40,7 @@
"@nuxt/test-utils": "^3.11.3",
"@nuxtjs/tailwindcss": "^6.11.4",
"@types/node": "^22.0.0",
"@types/nodemailer": "^6.4.17",
"@types/pg": "^8.15.4",
"@vue/eslint-config-prettier": "^10.2.0",
"@vue/eslint-config-typescript": "^14.5.0",

View File

@ -0,0 +1,53 @@
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'}`,
})
}
})