115 lines
3.6 KiB
TypeScript
115 lines
3.6 KiB
TypeScript
|
||
import nodemailer from 'nodemailer'
|
||
|
||
interface EmailParams {
|
||
name: string
|
||
phone: string
|
||
email?: string | null
|
||
serviceType?: string | null
|
||
message?: string | null
|
||
}
|
||
|
||
export async function sendEmailNotification(params: EmailParams) {
|
||
try {
|
||
// Создаем транспорт для Mail.ru
|
||
const transporter = nodemailer.createTransport({
|
||
host: process.env.SMTP_HOST || 'smtp.mail.ru',
|
||
port: 465,
|
||
secure: true, // использовать SSL
|
||
auth: {
|
||
user: process.env.SMTP_USER,
|
||
pass: process.env.SMTP_PASSWORD,
|
||
},
|
||
})
|
||
|
||
// Формируем текст письма
|
||
const serviceTypeText = params.serviceType
|
||
? getServiceTypeLabel(params.serviceType)
|
||
: 'Не указан'
|
||
|
||
const emailBody = `
|
||
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<style>
|
||
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
|
||
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
|
||
.header { background: #2563eb; color: white; padding: 20px; text-align: center; border-radius: 5px 5px 0 0; }
|
||
.content { background: #f9fafb; padding: 20px; border: 1px solid #e5e7eb; }
|
||
.field { margin-bottom: 15px; }
|
||
.label { font-weight: bold; color: #1f2937; }
|
||
.value { color: #4b5563; margin-top: 5px; }
|
||
.footer { margin-top: 20px; padding: 15px; text-align: center; font-size: 12px; color: #6b7280; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="container">
|
||
<div class="header">
|
||
<h2>🔔 Новая заявка с сайта Global-IT24</h2>
|
||
</div>
|
||
<div class="content">
|
||
<div class="field">
|
||
<div class="label">👤 Имя клиента:</div>
|
||
<div class="value">${params.name}</div>
|
||
</div>
|
||
|
||
<div class="field">
|
||
<div class="label">📱 Телефон:</div>
|
||
<div class="value"><a href="tel:${params.phone}">${params.phone}</a></div>
|
||
</div>
|
||
|
||
${params.email ? `
|
||
<div class="field">
|
||
<div class="label">📧 Email:</div>
|
||
<div class="value"><a href="mailto:${params.email}">${params.email}</a></div>
|
||
</div>
|
||
` : ''}
|
||
|
||
<div class="field">
|
||
<div class="label">⚙️ Тип услуги:</div>
|
||
<div class="value">${serviceTypeText}</div>
|
||
</div>
|
||
|
||
${params.message ? `
|
||
<div class="field">
|
||
<div class="label">💬 Сообщение:</div>
|
||
<div class="value">${params.message}</div>
|
||
</div>
|
||
` : ''}
|
||
</div>
|
||
<div class="footer">
|
||
Это автоматическое уведомление с сайта video.mscsrv.ru
|
||
</div>
|
||
</div>
|
||
</body>
|
||
</html>
|
||
`
|
||
|
||
// Отправляем письмо
|
||
const info = await transporter.sendMail({
|
||
from: `"Global-IT24 Website" <${process.env.SMTP_USER}>`,
|
||
to: process.env.NOTIFICATION_EMAIL || process.env.SMTP_USER,
|
||
subject: `🔔 Новая заявка от ${params.name}`,
|
||
html: emailBody,
|
||
})
|
||
|
||
console.log('Email sent:', info.messageId)
|
||
return { success: true, messageId: info.messageId }
|
||
} catch (error) {
|
||
console.error('Error sending email:', error)
|
||
return { success: false, error: error instanceof Error ? error.message : 'Unknown error' }
|
||
}
|
||
}
|
||
|
||
function getServiceTypeLabel(type: string): string {
|
||
const labels: Record<string, string> = {
|
||
'general': 'Общая консультация',
|
||
'residential': 'Видеонаблюдение для дома',
|
||
'commercial': 'Коммерческие системы',
|
||
'industrial': 'Промышленное видеонаблюдение',
|
||
'maintenance': 'Обслуживание систем',
|
||
}
|
||
return labels[type] || type
|
||
}
|