astro_avtourist/frontend/src/lib/email.ts
2026-05-04 22:18:43 +05:00

122 lines
No EOL
5.1 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 type { EmailOptions } from '../globalInterfaces';
const isDev = import.meta.env.DEV;
const hasProductionFlag = import.meta.env.PROD === 'true';
const forceProduction = import.meta.env.PB_POCKETBASE_URL?.includes('avt-back') || false;
const isProduction = hasProductionFlag || forceProduction || !isDev;
const SMTP_BZ_API_KEY = isProduction ? (import.meta.env.SMTP_BZ_API_KEY || '') : '';
const FROM_EMAIL = isProduction
? (import.meta.env.FROM_EMAIL || 'noreply@localhost')
: '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';
console.log('[EMAIL] isDev:', isDev);
console.log('[EMAIL] isProduction:', isProduction);
async function sendViaSmtpBz(options: EmailOptions): Promise<boolean> {
try {
console.log('[SMTP.BZ] Sending to:', options.to);
const response = await fetch('https://api.smtp.bz/v1/smtp/send', {
method: 'POST',
headers: {
'Authorization': SMTP_BZ_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
from: FROM_EMAIL,
name: FROM_NAME,
to: options.to,
subject: options.subject,
html: options.html,
}),
});
const data = await response.json();
if (response.ok) {
console.log('[SMTP.BZ] Email sent:', data.id || 'ok');
return true;
} else {
console.error('[SMTP.BZ] Error:', data);
return false;
}
} catch (error) {
console.error('[SMTP.BZ] Error:', error);
return false;
}
}
export async function sendEmail(options: EmailOptions): Promise<boolean> {
if (SMTP_BZ_API_KEY) {
return sendViaSmtpBz(options);
}
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>
`;
}