2025-05-13 18:50:30 +02:00
|
|
|
import { analytics } from './analytics';
|
2025-10-31 20:17:37 +01:00
|
|
|
import { sendContactEmail, type ContactEmailData } from '../api/emailClient';
|
2025-05-13 18:50:30 +02:00
|
|
|
|
|
|
|
|
interface EmailData {
|
|
|
|
|
name: string;
|
|
|
|
|
email: string;
|
|
|
|
|
phone?: string;
|
|
|
|
|
company?: string;
|
|
|
|
|
message: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function validateEmail(email: string): boolean {
|
|
|
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
|
|
|
return emailRegex.test(email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatEmailContent(data: EmailData, type: 'contact' | 'offset'): { subject: string, body: string } {
|
2025-10-31 20:17:37 +01:00
|
|
|
const subject = type === 'contact'
|
2025-05-13 18:50:30 +02:00
|
|
|
? `Contact from ${data.name} - Puffin Offset`
|
|
|
|
|
: `Offset Request - ${data.name}`;
|
|
|
|
|
|
|
|
|
|
const body = `
|
|
|
|
|
Name: ${data.name}
|
|
|
|
|
Email: ${data.email}
|
|
|
|
|
Phone: ${data.phone || 'Not provided'}
|
|
|
|
|
Company: ${data.company || 'Not provided'}
|
|
|
|
|
|
|
|
|
|
Message:
|
|
|
|
|
${data.message}
|
|
|
|
|
`.trim();
|
|
|
|
|
|
|
|
|
|
return { subject, body };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function sendEmail(to: string, subject: string, body: string): void {
|
|
|
|
|
const mailtoUrl = `mailto:${to}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
|
|
|
|
|
window.location.href = mailtoUrl;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-31 20:17:37 +01:00
|
|
|
/**
|
|
|
|
|
* Send contact form email via SMTP backend
|
|
|
|
|
* @param data Contact form data
|
|
|
|
|
* @param type Email type (contact or offset) - currently only 'contact' is supported
|
|
|
|
|
*/
|
|
|
|
|
export async function sendContactFormEmail(data: EmailData, type: 'contact' | 'offset' = 'contact'): Promise<void> {
|
2025-05-13 18:50:30 +02:00
|
|
|
try {
|
2025-10-31 20:17:37 +01:00
|
|
|
const contactData: ContactEmailData = {
|
|
|
|
|
name: data.name,
|
|
|
|
|
email: data.email,
|
|
|
|
|
phone: data.phone,
|
|
|
|
|
company: data.company,
|
|
|
|
|
message: data.message,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await sendContactEmail(contactData);
|
2025-05-13 18:50:30 +02:00
|
|
|
analytics.event('email', 'sent', type);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
analytics.error(error as Error, 'Email sending failed');
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|