Files

93 lines
4.9 KiB
PHP

<?php
/**
* TomTomGames Mailer — SendGrid HTTP API (cURL)
*/
function sendVerificationEmail(string $toEmail, string $toName, string $token): bool {
$siteName = defined('SITE_NAME') ? SITE_NAME : 'TomTomGames';
$siteUrl = defined('SITE_URL') ? SITE_URL : 'https://tomtomgames.com';
$verifyUrl = $siteUrl . '/verify.php?token=' . urlencode($token);
$subject = "Verify your {$siteName} account";
$text = "Welcome to {$siteName}, {$toName}!\n\n"
. "Verify your email address:\n{$verifyUrl}\n\n"
. "This link expires in 24 hours.\n\n"
. "If you did not create this account, ignore this email.\n\n"
. "— The {$siteName} Team";
$html = '<!DOCTYPE html><html><head><meta charset="UTF-8"></head>'
. '<body style="margin:0;padding:0;background:#0a0a12;font-family:Arial,sans-serif">'
. '<table width="100%" cellpadding="0" cellspacing="0" style="background:#0a0a12;padding:32px 16px">'
. '<tr><td align="center"><table width="520" cellpadding="0" cellspacing="0" '
. 'style="background:#1a1a2e;border:1px solid rgba(255,255,255,.08);border-radius:16px;overflow:hidden;max-width:520px;width:100%">'
. '<tr><td style="background:linear-gradient(135deg,#f0c040,#ff6b35);padding:28px 32px;text-align:center">'
. '<span style="font-weight:900;font-size:24px;color:#000">&#127918; ' . htmlspecialchars($siteName) . '</span>'
. '</td></tr>'
. '<tr><td style="padding:36px 32px;color:#e8e8f0">'
. '<h2 style="margin:0 0 16px;font-size:22px;color:#f0c040">Verify your account</h2>'
. '<p style="margin:0 0 12px;font-size:15px;color:#aaaacc;line-height:1.6">Hey <strong style="color:#e8e8f0">'
. htmlspecialchars($toName) . '</strong>,</p>'
. '<p style="margin:0 0 24px;font-size:15px;color:#aaaacc;line-height:1.6">Thanks for signing up! Click below to verify your email and activate your account.</p>'
. '<div style="text-align:center;margin-bottom:28px">'
. '<a href="' . htmlspecialchars($verifyUrl) . '" style="display:inline-block;background:linear-gradient(135deg,#f0c040,#d4a017);color:#000;font-weight:700;font-size:16px;padding:16px 40px;border-radius:10px;text-decoration:none;letter-spacing:.5px">VERIFY MY ACCOUNT</a>'
. '</div>'
. '<p style="font-size:12px;color:#666688;margin-bottom:8px">Or paste this into your browser:</p>'
. '<p style="font-size:12px;color:#00e5ff;word-break:break-all;margin:0 0 24px">' . htmlspecialchars($verifyUrl) . '</p>'
. '<p style="font-size:12px;color:#555577;border-top:1px solid rgba(255,255,255,.06);padding-top:16px;margin:0">'
. 'Link expires in 24 hours. Did not sign up? You can safely ignore this email.</p>'
. '</td></tr>'
. '<tr><td style="background:#111122;padding:16px 32px;text-align:center">'
. '<span style="font-size:11px;color:#444466">&copy; ' . htmlspecialchars($siteName)
. ' &middot; <a href="' . htmlspecialchars($siteUrl) . '" style="color:#f0c040;text-decoration:none">'
. htmlspecialchars($siteUrl) . '</a></span>'
. '</td></tr></table></td></tr></table></body></html>';
return sendgridSend($toEmail, $toName, $subject, $text, $html);
}
function sendgridSend(string $toEmail, string $toName, string $subject, string $textBody, string $htmlBody = ''): bool {
$apiKey = defined('SENDGRID_API_KEY') ? SENDGRID_API_KEY : '';
if (!$apiKey) {
error_log('[TomTomGames mailer] SENDGRID_API_KEY not defined');
return false;
}
$payload = json_encode([
'personalizations' => [['to' => [['email' => $toEmail, 'name' => $toName]]]],
'from' => [
'email' => defined('SMTP_FROM') ? SMTP_FROM : 'noreply@tomtomgames.com',
'name' => defined('SMTP_FROM_NAME') ? SMTP_FROM_NAME : 'TomTomGames',
],
'subject' => $subject,
'content' => array_values(array_filter([
['type' => 'text/plain', 'value' => $textBody],
$htmlBody ? ['type' => 'text/html', 'value' => $htmlBody] : null,
])),
]);
$ch = curl_init('https://api.sendgrid.com/v3/mail/send');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
],
CURLOPT_TIMEOUT => 20,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlErr = curl_error($ch);
curl_close($ch);
if ($httpCode === 202) return true;
error_log('[TomTomGames mailer] SendGrid HTTP ' . $httpCode . ' err=' . $curlErr . ' body=' . $response);
return false;
}