Proton Mail (SMTP submission) * * Receives the form as JSON from js/main.js and sends it to your inbox * through smtp.protonmail.ch:587 (STARTTLS). * No secret lives in this file: the SMTP token is read from * ../portfolio-private/smtp-config.php, outside the web root. */ declare(strict_types=1); use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception as MailException; header('Content-Type: application/json; charset=utf-8'); header('X-Content-Type-Options: nosniff'); header('Cache-Control: no-store'); function reply(int $code, bool $ok, string $msg): never { http_response_code($code); echo json_encode(['success' => $ok, 'message' => $msg]); exit; } // ---------- Private folder (outside the web root) ---------- $private = getenv('PORTFOLIO_PRIVATE_DIR') ?: dirname(__DIR__) . '/portfolio-private'; $configFile = $private . '/smtp-config.php'; if (!is_readable($configFile)) { error_log("contact.php: config not readable at $configFile"); reply(500, false, 'The contact form is not configured yet.'); } $cfg = require $configFile; require $private . '/PHPMailer/Exception.php'; require $private . '/PHPMailer/PHPMailer.php'; require $private . '/PHPMailer/SMTP.php'; // ---------- Request checks ---------- if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') { reply(405, false, 'Method not allowed.'); } $origin = $_SERVER['HTTP_ORIGIN'] ?? ''; if ($origin !== '' && !in_array($origin, $cfg['allowed_origins'] ?? [], true)) { reply(403, false, 'Forbidden.'); } $raw = (string) file_get_contents('php://input', false, null, 0, 20000); $in = json_decode($raw, true); if (!is_array($in)) { reply(400, false, 'Invalid request.'); } // Honeypot: bots tick the hidden box. Pretend it worked, send nothing. if (!empty($in['botcheck'])) { reply(200, true, 'Message sent.'); } // ---------- Validation ---------- function field(array $in, string $key, int $max): string { return mb_substr(trim((string) ($in[$key] ?? '')), 0, $max); } $name = field($in, 'name', 100); $email = field($in, 'email', 150); $company = field($in, 'company', 100); $subject = field($in, 'subject', 150); $message = field($in, 'message', 4000); // Header-bound fields must not contain line breaks (header injection). foreach ([$name, $email, $company, $subject] as $v) { if (preg_match('/[\r\n]/', $v)) { reply(400, false, 'Invalid characters in the form.'); } } if ($name === '' || $subject === '' || $message === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) { reply(422, false, 'Fill in your name, a valid email, a subject and a message.'); } // ---------- Rate limit per IP ---------- $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown'; $store = sys_get_temp_dir() . '/portfolio-contact-' . hash('sha256', $ip . ($cfg['rate_salt'] ?? '')); $now = time(); $hits = []; if (is_readable($store)) { foreach (explode(',', (string) file_get_contents($store)) as $t) { if ((int) $t > $now - 3600) { $hits[] = (int) $t; } } } if (count($hits) >= (int) ($cfg['max_per_hour'] ?? 5)) { reply(429, false, 'Too many messages from your connection. Try again in an hour.'); } // ---------- Send through Proton ---------- $mail = new PHPMailer(true); try { $mail->isSMTP(); $mail->Host = $cfg['smtp_host'] ?? 'smtp.protonmail.ch'; $mail->Port = (int) ($cfg['smtp_port'] ?? 587); $mail->SMTPSecure = $cfg['smtp_secure'] ?? PHPMailer::ENCRYPTION_STARTTLS; $mail->SMTPAutoTLS = $mail->SMTPSecure !== ''; $mail->SMTPAuth = $cfg['smtp_auth'] ?? true; $mail->Username = $cfg['smtp_user']; $mail->Password = $cfg['smtp_token']; $mail->CharSet = PHPMailer::CHARSET_UTF8; $mail->Timeout = 15; // Proton only accepts the address tied to the token as sender. $mail->setFrom($cfg['smtp_user'], 'Portfolio: ' . $name); $mail->addAddress($cfg['to']); $mail->addReplyTo($email, $name); // "Reply" answers the visitor directly $mail->Subject = '[Portfolio] ' . $subject; $mail->isHTML(false); $mail->Body = "New message from your portfolio\n" . "-------------------------------\n" . "Name: $name\n" . "Email: $email\n" . ($company !== '' ? "Company: $company\n" : '') . 'Date: ' . date('Y-m-d H:i:s T') . "\n\n" . $message . "\n"; $mail->send(); $hits[] = $now; file_put_contents($store, implode(',', $hits), LOCK_EX); reply(200, true, "Message sent. Thanks, I'll get back to you soon."); } catch (MailException $e) { error_log('contact.php SMTP error: ' . $mail->ErrorInfo); reply(502, false, 'The message could not be sent.'); }