70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
|
|
import { analytics } from './analytics';
|
||
|
|
|
||
|
|
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 } {
|
||
|
|
const subject = type === 'contact'
|
||
|
|
? `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;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function sendFormspreeEmail(data: EmailData, type: 'contact' | 'offset'): Promise<void> {
|
||
|
|
const FORMSPREE_CONTACT_ID = 'xkgovnby'; // Contact form
|
||
|
|
const FORMSPREE_OFFSET_ID = 'xvgzbory'; // Offset request form
|
||
|
|
|
||
|
|
const formId = type === 'contact' ? FORMSPREE_CONTACT_ID : FORMSPREE_OFFSET_ID;
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await fetch(`https://formspree.io/f/${formId}`, {
|
||
|
|
method: 'POST',
|
||
|
|
headers: {
|
||
|
|
'Content-Type': 'application/json',
|
||
|
|
'Accept': 'application/json'
|
||
|
|
},
|
||
|
|
body: JSON.stringify({
|
||
|
|
...data,
|
||
|
|
_subject: type === 'contact'
|
||
|
|
? `Contact from ${data.name} - Puffin Offset`
|
||
|
|
: `Offset Request - ${data.name}`
|
||
|
|
}),
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
const errorData = await response.json();
|
||
|
|
throw new Error(errorData.error || 'Failed to send message');
|
||
|
|
}
|
||
|
|
|
||
|
|
analytics.event('email', 'sent', type);
|
||
|
|
} catch (error) {
|
||
|
|
analytics.error(error as Error, 'Email sending failed');
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|