From c88e60da0b914f2d4a514a75894168e9a3a9ef75 Mon Sep 17 00:00:00 2001 From: blake Date: Mon, 19 Oct 2026 14:40:18 -0500 Subject: [PATCH] feat: send contact form messages through SMTP2GO Replace the Formbold form endpoint with a local PHP handler so submissions stay on this site and the API key stays out of the repo. --- .gitignore | 1 + .htaccess | 6 +- contact-config.example.php | 13 ++ contact.php | 238 ++++++++++++++++++++++++++++ css/queries.css | 6 +- css/style.css | 23 +++ index.html | 13 +- privacy-policy/index.html | 21 ++- script.js | 21 +++ services/drone/index.html | 13 +- services/graphic-design/index.html | 17 +- services/support/index.html | 17 +- services/web-design/index.html | 17 +- services/web-development/index.html | 17 +- services/web-hosting/index.html | 17 +- 15 files changed, 424 insertions(+), 16 deletions(-) create mode 100644 contact-config.example.php create mode 100644 contact.php diff --git a/.gitignore b/.gitignore index ef334c0..6811ed6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .claude/ .vscode/ +contact-config.php diff --git a/.htaccess b/.htaccess index 927edba..4eb23e9 100644 --- a/.htaccess +++ b/.htaccess @@ -12,6 +12,10 @@ ErrorDocument 404 /404.html + + Require all denied + + AddType application/manifest+json .webmanifest AddType application/xml .xml @@ -21,5 +25,5 @@ AddType application/xml .xml Header always set X-Content-Type-Options "nosniff" Header always set Referrer-Policy "strict-origin-when-cross-origin" Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()" - Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://cloud.umami.is; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data:; connect-src 'self' https://cloud.umami.is; form-action https://formbold.com; frame-ancestors 'self'; base-uri 'self'" + Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://cloud.umami.is; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data:; connect-src 'self' https://cloud.umami.is; form-action 'self'; frame-ancestors 'self'; base-uri 'self'" diff --git a/contact-config.example.php b/contact-config.example.php new file mode 100644 index 0000000..cf7bbad --- /dev/null +++ b/contact-config.example.php @@ -0,0 +1,13 @@ + Verified Senders) before messages will go out. + */ +return [ + 'api_key' => 'api-your-key-here', + // 'sender' => 'Cosmically Creative ', + // 'to' => 'Blake ', +]; diff --git a/contact.php b/contact.php new file mode 100644 index 0000000..6ec6d0d --- /dev/null +++ b/contact.php @@ -0,0 +1,238 @@ + getenv('SMTP2GO_API_KEY') ?: '', + 'sender' => 'Cosmically Creative ', + 'to' => 'Blake ', + ]; + + $configFile = __DIR__ . '/contact-config.php'; + if (is_file($configFile)) { + $local = require $configFile; + if (is_array($local)) { + foreach ($local as $key => $value) { + if (is_string($value) && $value !== '') { + $config[$key] = $value; + } + } + } + } + + return $config; +} + +function contact_redirect(string $path, string $status): void +{ + if (!in_array($path, CONTACT_REDIRECTS, true)) { + $path = '/'; + } + + header('Location: ' . $path . '?sent=' . $status . '#contact', true, 303); + exit; +} + +function contact_host(): string +{ + $host = strtolower($_SERVER['HTTP_HOST'] ?? ''); + return preg_replace('/:\d+$/', '', $host) ?? ''; +} + +function contact_same_site(): bool +{ + $host = contact_host(); + if (!in_array($host, CONTACT_HOSTS, true)) { + return false; + } + + $source = $_SERVER['HTTP_ORIGIN'] ?? ($_SERVER['HTTP_REFERER'] ?? ''); + if ($source === '') { + return false; + } + + $sourceHost = strtolower((string) parse_url($source, PHP_URL_HOST)); + return $sourceHost === $host; +} + +function contact_clean(string $value, int $max): string +{ + $value = str_replace(["\r\n", "\r"], "\n", $value); + $value = str_replace("\0", '', $value); + $value = trim($value); + if (strlen($value) > $max) { + $value = substr($value, 0, $max); + } + + return $value; +} + +function contact_header_value(string $value): string +{ + return str_replace(["\r", "\n", "\0"], '', $value); +} + +function contact_too_fast(string $ip): bool +{ + $dir = sys_get_temp_dir() . '/cc-contact'; + if (!is_dir($dir) && !mkdir($dir, 0700, true) && !is_dir($dir)) { + return false; + } + + $file = $dir . '/' . hash('sha256', $ip); + $now = time(); + $last = is_file($file) ? (int) file_get_contents($file) : 0; + file_put_contents($file, (string) $now, LOCK_EX); + + return $last > 0 && ($now - $last) < 20; +} + +function contact_send(string $apiKey, array $payload): bool +{ + $body = json_encode($payload, JSON_UNESCAPED_UNICODE); + if ($body === false) { + return false; + } + + $headers = [ + 'Content-Type: application/json', + 'Accept: application/json', + 'X-Smtp2go-Api-Key: ' . $apiKey, + ]; + + if (function_exists('curl_init')) { + $ch = curl_init('https://api.smtp2go.com/v3/email/send'); + curl_setopt_array($ch, [ + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $body, + CURLOPT_HTTPHEADER => $headers, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 15, + ]); + $response = curl_exec($ch); + $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); + $error = curl_error($ch); + curl_close($ch); + } else { + $context = stream_context_create([ + 'http' => [ + 'method' => 'POST', + 'header' => implode("\r\n", $headers), + 'content' => $body, + 'timeout' => 15, + 'ignore_errors' => true, + ], + ]); + $response = file_get_contents('https://api.smtp2go.com/v3/email/send', false, $context); + $status = 0; + if (isset($http_response_header[0]) && preg_match('/\s(\d{3})\s/', $http_response_header[0], $match)) { + $status = (int) $match[1]; + } + $error = $response === false ? 'request failed' : ''; + } + + if ($response === false || $status < 200 || $status >= 300) { + error_log('SMTP2GO request failed (' . $status . '): ' . $error); + return false; + } + + $decoded = json_decode($response, true); + if (!is_array($decoded)) { + error_log('SMTP2GO returned an unreadable response'); + return false; + } + + $data = $decoded['data'] ?? []; + if (!empty($data['error'])) { + error_log('SMTP2GO: ' . $data['error']); + return false; + } + + if (($data['failed'] ?? 0) > 0) { + error_log('SMTP2GO failures: ' . json_encode($data['failures'] ?? [])); + return false; + } + + return ($data['succeeded'] ?? 0) > 0 || !empty($data['email_id']); +} + +if (PHP_SAPI === 'cli') { + return; +} + +if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') { + header('Location: /#contact', true, 303); + exit; +} + +$redirect = contact_clean((string) ($_POST['redirect'] ?? '/'), 80); +$redirect = str_replace(["\n", "\r"], '', $redirect); + +if (!contact_same_site()) { + contact_redirect($redirect, '0'); +} + +if (trim((string) ($_POST['cc_hp'] ?? '')) !== '') { + contact_redirect($redirect, '1'); +} + +$name = contact_clean((string) ($_POST['name'] ?? ''), 100); +$email = contact_clean((string) ($_POST['email'] ?? ''), 254); +$message = contact_clean((string) ($_POST['message'] ?? ''), 5000); + +if ($name === '' || $message === '' || filter_var($email, FILTER_VALIDATE_EMAIL) === false) { + contact_redirect($redirect, '0'); +} + +$config = contact_config(); +if ($config['api_key'] === '') { + error_log('SMTP2GO API key is not configured'); + contact_redirect($redirect, '0'); +} + +$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown'; +if (contact_too_fast($ip)) { + contact_redirect($redirect, '0'); +} + +$safeName = str_replace(['<', '>', '"', ','], '', contact_header_value($name)); +$safeEmail = contact_header_value($email); +$replyTo = $safeName !== '' ? $safeName . ' <' . $safeEmail . '>' : $safeEmail; + +$sent = contact_send($config['api_key'], [ + 'sender' => $config['sender'], + 'to' => [$config['to']], + 'subject' => 'Website message from ' . $safeName, + 'text_body' => "Name: {$name}\nEmail: {$email}\n\n{$message}\n", + 'custom_headers' => [ + ['header' => 'Reply-To', 'value' => $replyTo], + ], +]); + +contact_redirect($redirect, $sent ? '1' : '0'); diff --git a/css/queries.css b/css/queries.css index a04a8a3..f57580c 100644 --- a/css/queries.css +++ b/css/queries.css @@ -236,7 +236,8 @@ width: 40rem; } - .form-field { + .form-field, + .form-status { width: 40rem; } @@ -508,7 +509,8 @@ max-width: 80%; } - .form-field { + .form-field, + .form-status { width: 38rem; } diff --git a/css/style.css b/css/style.css index a0b265c..f343d6a 100644 --- a/css/style.css +++ b/css/style.css @@ -861,6 +861,29 @@ p { gap: 4rem; align-self: center; justify-self: center; + position: relative; +} + +.form-hp { + position: absolute; + left: -10000px; + width: 1px; + height: 1px; + overflow: hidden; +} + +.form-status { + font-size: 1.8rem; + line-height: 1.4; + max-width: 50rem; +} + +.form-status-success { + color: var(--color-yellow); +} + +.form-status-error { + color: #ffb4b4; } .form-field { diff --git a/index.html b/index.html index 4e45a48..f1b00a1 100644 --- a/index.html +++ b/index.html @@ -721,7 +721,7 @@
@@ -761,6 +761,17 @@ required >
+ + diff --git a/privacy-policy/index.html b/privacy-policy/index.html index 26c60ae..7f11dd1 100644 --- a/privacy-policy/index.html +++ b/privacy-policy/index.html @@ -166,7 +166,7 @@
diff --git a/services/graphic-design/index.html b/services/graphic-design/index.html index 96aaa4d..dd75a72 100644 --- a/services/graphic-design/index.html +++ b/services/graphic-design/index.html @@ -206,7 +206,7 @@
@@ -244,6 +244,21 @@ required >
+ +
diff --git a/services/support/index.html b/services/support/index.html index b00a0c9..c10bdc3 100644 --- a/services/support/index.html +++ b/services/support/index.html @@ -232,7 +232,7 @@
@@ -272,6 +272,21 @@ required >
+ + diff --git a/services/web-design/index.html b/services/web-design/index.html index 36a3d40..77d54d5 100644 --- a/services/web-design/index.html +++ b/services/web-design/index.html @@ -217,7 +217,7 @@
@@ -255,6 +255,21 @@ required >
+ +
diff --git a/services/web-development/index.html b/services/web-development/index.html index 188873f..e5cb16b 100644 --- a/services/web-development/index.html +++ b/services/web-development/index.html @@ -256,7 +256,7 @@
@@ -296,6 +296,21 @@ required >
+ + diff --git a/services/web-hosting/index.html b/services/web-hosting/index.html index ed357d6..2ab5d3b 100644 --- a/services/web-hosting/index.html +++ b/services/web-hosting/index.html @@ -231,7 +231,7 @@
@@ -271,6 +271,21 @@ required >
+ +