astro_avtourist/frontend/src/lib/email.ts

122 lines
5.1 KiB
TypeScript
Raw Normal View History

import type { EmailOptions } from '../globalInterfaces';
const isDev = import.meta.env.DEV;
2026-05-04 03:51:17 +05:00
const hasProductionFlag = import.meta.env.PROD === 'true';
const forceProduction = import.meta.env.PB_POCKETBASE_URL?.includes('avt-back') || false;
const isProduction = hasProductionFlag || forceProduction || !isDev;
2026-05-04 22:18:43 +05:00
const SMTP_BZ_API_KEY = isProduction ? (import.meta.env.SMTP_BZ_API_KEY || '') : '';
2026-05-04 21:01:24 +05:00
2026-05-04 02:45:06 +05:00
const FROM_EMAIL = isProduction
2026-05-04 04:05:35 +05:00
? (import.meta.env.FROM_EMAIL || 'noreply@localhost')
2026-05-04 02:45:06 +05:00
: 'noreply@localhost';
const FROM_NAME = isProduction ? (import.meta.env.FROM_NAME || 'Автоюрист Сургут') : 'Dev';
const SITE_URL = isProduction
? (import.meta.env.SITE_URL || 'https://avtourist-surgut.ru')
: 'http://localhost:4321';
2026-05-04 03:51:17 +05:00
console.log('[EMAIL] isDev:', isDev);
console.log('[EMAIL] isProduction:', isProduction);
2026-05-04 22:18:43 +05:00
async function sendViaSmtpBz(options: EmailOptions): Promise<boolean> {
2026-05-04 03:51:17 +05:00
try {
2026-05-04 22:18:43 +05:00
console.log('[SMTP.BZ] Sending to:', options.to);
const response = await fetch('https://api.smtp.bz/v1/smtp/send', {
2026-05-04 03:51:17 +05:00
method: 'POST',
headers: {
2026-05-04 22:18:43 +05:00
'Authorization': SMTP_BZ_API_KEY,
2026-05-04 03:51:17 +05:00
'Content-Type': 'application/json',
},
body: JSON.stringify({
2026-05-04 22:18:43 +05:00
from: FROM_EMAIL,
name: FROM_NAME,
2026-05-04 03:51:17 +05:00
to: options.to,
subject: options.subject,
html: options.html,
}),
});
const data = await response.json();
if (response.ok) {
2026-05-04 22:18:43 +05:00
console.log('[SMTP.BZ] Email sent:', data.id || 'ok');
2026-05-04 03:51:17 +05:00
return true;
} else {
2026-05-04 22:18:43 +05:00
console.error('[SMTP.BZ] Error:', data);
2026-05-04 03:51:17 +05:00
return false;
}
} catch (error) {
2026-05-04 22:18:43 +05:00
console.error('[SMTP.BZ] Error:', error);
2026-05-04 03:51:17 +05:00
return false;
}
}
export async function sendEmail(options: EmailOptions): Promise<boolean> {
2026-05-04 22:18:43 +05:00
if (SMTP_BZ_API_KEY) {
return sendViaSmtpBz(options);
2026-05-04 21:01:24 +05:00
}
2026-05-04 22:18:43 +05:00
2026-05-04 21:01:24 +05:00
console.log('[DEV] Email not sent:', options.to);
return false;
}
export function getSiteUrl(): string {
return SITE_URL;
}
export function generateVerifyEmailHtml(firstName: string, verifyLink: string): string {
return `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Подтверждение регистрации</title>
</head>
<body style="margin: 0; padding: 0; background-color: #f5f7fa; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;">
<table width="100%" cellpadding="0" cellspacing="0" style="background-color: #f5f7fa; padding: 40px 20px;">
<tr>
<td align="center">
<table width="100%" cellpadding="0" cellspacing="0" style="max-width: 600px; background-color: #ffffff; border-radius: 16px; overflow: hidden; box-shadow: 0 4px 20px rgba(0,0,0,0.08);">
<tr>
<td style="background: linear-gradient(135deg, #1e3050 0%, #2d4a6f 100%); padding: 30px 40px; text-align: center;">
<h1 style="color: #ffffff; margin: 0; font-size: 28px; font-weight: 700;">Автоюрист Сургут</h1>
<p style="color: rgba(255,255,255,0.8); margin: 10px 0 0 0; font-size: 16px;">Юридические услуги для автовладельцев</p>
</td>
</tr>
<tr>
<td style="padding: 40px;">
<h2 style="color: #1e3050; margin: 0 0 20px 0; font-size: 24px; font-weight: 700;">Добро пожаловать, ${firstName}!</h2>
<p style="color: #64748b; font-size: 16px; line-height: 1.6; margin: 0 0 20px 0;">
Благодарим за регистрацию на сайте <strong>avtourist-surgut.ru</strong>. Мы рады, что вы выбрали нас для решения юридических вопросов, связанных с автомобилем.
</p>
<p style="color: #64748b; font-size: 16px; line-height: 1.6; margin: 0 0 30px 0;">
Для завершения регистрации и активации вашего аккаунта, пожалуйста, подтвердите ваш email, нажав на кнопку ниже:
</p>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<a href="${verifyLink}" style="display: inline-block; background: linear-gradient(135deg, #eac26e 0%, #ce9f40 100%); color: #ffffff; text-decoration: none; padding: 16px 32px; border-radius: 8px; font-size: 16px; font-weight: 700;">
Подтвердить регистрацию
</a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="background-color: #1e3050; padding: 24px 40px; text-align: center;">
<p style="color: rgba(255,255,255,0.6); font-size: 14px; margin: 0;">
© 2026 Автоюрист Сургут. Все права защищены.
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
`;
}