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.
This commit is contained in:
blake committed 2026-10-19 14:40:18 -05:00
1 parent f292bc1a67
commit c88e60da0b
15 files changed
+424 -16

No files matched your search

+1
View File
@@ -1,2 +1,3 @@
.claude/
.vscode/
contact-config.php
+5 -1
View File
@@ -12,6 +12,10 @@
ErrorDocument 404 /404.html
<FilesMatch "^contact-config">
Require all denied
</FilesMatch>
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'"
</IfModule>
+13
View File
@@ -0,0 +1,13 @@
<?php
/**
* Copy this file to contact-config.php and paste your SMTP2GO API key.
* contact-config.php is ignored by git.
*
* The sender address must be a verified sender in SMTP2GO
* (Sending > Verified Senders) before messages will go out.
*/
return [
'api_key' => 'api-your-key-here',
// 'sender' => 'Cosmically Creative <blake@cosmicallycreative.com>',
// 'to' => 'Blake <blake@cosmicallycreative.com>',
];
+238
View File
@@ -0,0 +1,238 @@
<?php
/**
* Contact form handler. Posts are emailed through SMTP2GO.
*
* Put the API key in contact-config.php (copy contact-config.example.php)
* or in the SMTP2GO_API_KEY environment variable. The From address has to
* be a verified sender in SMTP2GO.
*/
declare(strict_types=1);
const CONTACT_REDIRECTS = [
'/',
'/services/drone/',
'/services/graphic-design/',
'/services/support/',
'/services/web-design/',
'/services/web-development/',
'/services/web-hosting/',
];
const CONTACT_HOSTS = [
'www.cosmicallycreative.com',
'cosmicallycreative.com',
'localhost',
'127.0.0.1',
];
function contact_config(): array
{
$config = [
'api_key' => getenv('SMTP2GO_API_KEY') ?: '',
'sender' => 'Cosmically Creative <blake@cosmicallycreative.com>',
'to' => 'Blake <blake@cosmicallycreative.com>',
];
$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');
+4 -2
View File
@@ -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;
}
+23
View File
@@ -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 {
+12 -1
View File
@@ -721,7 +721,7 @@
<form
class="contact-form"
name="contact"
action="https://formbold.com/s/67LMK"
action="/contact.php"
method="POST"
>
<div>
@@ -761,6 +761,17 @@
required
></textarea>
</div>
<div class="form-hp" aria-hidden="true">
<label for="cc-hp">Leave this field empty</label>
<input
id="cc-hp"
type="text"
name="cc_hp"
tabindex="-1"
autocomplete="off"
/>
</div>
<input type="hidden" name="redirect" value="/" />
<button type="submit" class="btn btn-form">
Send Message
</button>
+15 -6
View File
@@ -166,7 +166,7 @@
</div>
<div class="legal-content">
<p class="legal-updated">
<strong>Last updated:</strong> July 7, 2026
<strong>Last updated:</strong> October 19, 2026
</p>
<h2>Privacy Policy</h2>
@@ -175,14 +175,23 @@
<p>
When you submit the contact form on this site, I collect
the information you provide — your name, email address,
and message. That information is delivered to me through
and message. The form is delivered to this website, and
the message is emailed to me through
<a
href="https://formbold.com"
href="https://www.smtp2go.com"
target="_blank"
rel="noopener noreferrer"
>FormBold</a
>, the form processing service that powers this site's
contact form, so that I can respond to your inquiry.
>SMTP2GO</a
>
so that I can respond to your inquiry. SMTP2GO processes
that email in order to deliver it. You can review their
privacy practices at
<a
href="https://www.smtp2go.com/privacy/"
target="_blank"
rel="noopener noreferrer"
>smtp2go.com/privacy</a
>.
</p>
<h3>Analytics</h3>
+21
View File
@@ -1,5 +1,26 @@
"use strict";
const contactForm = document.querySelector(".contact-form");
if (contactForm) {
const sent = new URLSearchParams(window.location.search).get("sent");
if (sent === "1" || sent === "0") {
const note = document.createElement("p");
note.className =
"form-status " +
(sent === "1" ? "form-status-success" : "form-status-error");
note.setAttribute("role", "status");
note.textContent =
sent === "1"
? "Message sent. I’ll get back to you soon."
: "That message didn’t send. Please try again, or email blake@cosmicallycreative.com.";
contactForm.prepend(note);
const contactSection = document.getElementById("contact");
if (window.location.hash !== "#contact" && contactSection) {
contactSection.scrollIntoView();
}
}
}
// Mobile navigation
const btnNavEl = document.querySelector(".btn-mobile-nav");
const headerEl = document.querySelector(".header");
+12 -1
View File
@@ -205,7 +205,7 @@
<form
class="contact-form"
name="contact"
action="https://formbold.com/s/67LMK"
action="/contact.php"
method="POST"
>
<div>
@@ -243,6 +243,17 @@
required
></textarea>
</div>
<div class="form-hp" aria-hidden="true">
<label for="cc-hp">Leave this field empty</label>
<input
id="cc-hp"
type="text"
name="cc_hp"
tabindex="-1"
autocomplete="off"
/>
</div>
<input type="hidden" name="redirect" value="/services/drone/" />
<button type="submit" class="btn btn-form">Send Message</button>
</form>
</div>
+16 -1
View File
@@ -206,7 +206,7 @@
<form
class="contact-form animate-on-scroll"
name="contact"
action="https://formbold.com/s/67LMK"
action="/contact.php"
method="POST"
>
<div>
@@ -244,6 +244,21 @@
required
></textarea>
</div>
<div class="form-hp" aria-hidden="true">
<label for="cc-hp">Leave this field empty</label>
<input
id="cc-hp"
type="text"
name="cc_hp"
tabindex="-1"
autocomplete="off"
/>
</div>
<input
type="hidden"
name="redirect"
value="/services/graphic-design/"
/>
<button type="submit" class="btn btn-form">Send Message</button>
</form>
</div>
+16 -1
View File
@@ -232,7 +232,7 @@
<form
class="contact-form"
name="contact"
action="https://formbold.com/s/67LMK"
action="/contact.php"
method="POST"
>
<div>
@@ -272,6 +272,21 @@
required
></textarea>
</div>
<div class="form-hp" aria-hidden="true">
<label for="cc-hp">Leave this field empty</label>
<input
id="cc-hp"
type="text"
name="cc_hp"
tabindex="-1"
autocomplete="off"
/>
</div>
<input
type="hidden"
name="redirect"
value="/services/support/"
/>
<button type="submit" class="btn btn-form">
Send Message
</button>
+16 -1
View File
@@ -217,7 +217,7 @@
<form
class="contact-form"
name="contact"
action="https://formbold.com/s/67LMK"
action="/contact.php"
method="POST"
>
<div>
@@ -255,6 +255,21 @@
required
></textarea>
</div>
<div class="form-hp" aria-hidden="true">
<label for="cc-hp">Leave this field empty</label>
<input
id="cc-hp"
type="text"
name="cc_hp"
tabindex="-1"
autocomplete="off"
/>
</div>
<input
type="hidden"
name="redirect"
value="/services/web-design/"
/>
<button type="submit" class="btn btn-form">Send Message</button>
</form>
</div>
+16 -1
View File
@@ -256,7 +256,7 @@
<form
class="contact-form"
name="contact"
action="https://formbold.com/s/67LMK"
action="/contact.php"
method="POST"
>
<div>
@@ -296,6 +296,21 @@
required
></textarea>
</div>
<div class="form-hp" aria-hidden="true">
<label for="cc-hp">Leave this field empty</label>
<input
id="cc-hp"
type="text"
name="cc_hp"
tabindex="-1"
autocomplete="off"
/>
</div>
<input
type="hidden"
name="redirect"
value="/services/web-development/"
/>
<button type="submit" class="btn btn-form">
Send Message
</button>
+16 -1
View File
@@ -231,7 +231,7 @@
<form
class="contact-form"
name="contact"
action="https://formbold.com/s/67LMK"
action="/contact.php"
method="POST"
>
<div>
@@ -271,6 +271,21 @@
required
></textarea>
</div>
<div class="form-hp" aria-hidden="true">
<label for="cc-hp">Leave this field empty</label>
<input
id="cc-hp"
type="text"
name="cc_hp"
tabindex="-1"
autocomplete="off"
/>
</div>
<input
type="hidden"
name="redirect"
value="/services/web-hosting/"
/>
<button type="submit" class="btn btn-form">
Send Message
</button>