Restore contact form and make it functional with email sending

- Replace mailto link with original contact form (name, email, message fields)
- Add contact API endpoint that sends email via mail() and saves to messages.json
- Restore .contact-form CSS styles and translation keys

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-08 11:28:26 +02:00
parent 5dfbbacf39
commit dcc1205244
4 changed files with 85 additions and 12 deletions

29
api.php
View File

@@ -909,6 +909,35 @@ switch ($action) {
}
ok(['loggedIn' => false]);
// ─── Yhteydenotto ──────────────────────────────────────────
case 'contact':
$name = trim($body['name'] ?? '');
$email = trim($body['email'] ?? '');
$message = trim($body['message'] ?? '');
if (!$name || !$email || !$message) err('Täytä kaikki kentät.');
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) err('Sähköpostiosoite ei kelpaa.');
if (mb_strlen($message) > 5000) err('Viesti on liian pitkä.');
// Tallenna varmuuskopio
$messages = readData('messages.json', []);
$messages[] = [
'name' => htmlspecialchars($name, ENT_QUOTES | ENT_HTML5, 'UTF-8'),
'email' => htmlspecialchars($email, ENT_QUOTES | ENT_HTML5, 'UTF-8'),
'message' => htmlspecialchars($message, ENT_QUOTES | ENT_HTML5, 'UTF-8'),
'date' => date('Y-m-d H:i:s'),
];
writeData('messages.json', $messages);
// Lähetä sähköposti
$to = 'info@tykkaa.fi';
$subject = "tykkää.fi: viesti käyttäjältä $name";
$body = "Nimi: $name\nSähköposti: $email\n\nViesti:\n$message";
$headers = "From: noreply@tykkaa.fi\r\nReply-To: $email\r\nContent-Type: text/plain; charset=UTF-8";
@mail($to, $subject, $body, $headers);
ok(['sent' => true]);
default:
err('Unknown action');
}