diff --git a/includes/functions.php b/includes/functions.php index 7a13742..4452d86 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -326,48 +326,36 @@ function getCartTotal() { } /** - * Send email using SendGrid + * Send email via CyberMail API */ function sendEmail($to, $subject, $htmlContent, $textContent = '') { - if (empty(SENDGRID_API_KEY)) { - return false; - } - - $data = [ - 'personalizations' => [ - [ - 'to' => [['email' => $to]], - 'subject' => $subject - ] - ], - 'from' => [ - 'email' => SENDER_EMAIL, - 'name' => SENDER_NAME - ], - 'content' => [ - ['type' => 'text/html', 'value' => $htmlContent] - ] + $apiKey = defined('CYBERMAIL_API_KEY') ? CYBERMAIL_API_KEY : ''; + if (!$apiKey) return false; + + $payload = [ + 'from' => ['email' => SENDER_EMAIL, 'name' => SENDER_NAME], + 'to' => [['email' => $to]], + 'subject' => $subject, + 'html' => $htmlContent, ]; - - if ($textContent) { - array_unshift($data['content'], ['type' => 'text/plain', 'value' => $textContent]); - } - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/v3/mail/send'); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); - curl_setopt($ch, CURLOPT_HTTPHEADER, [ - 'Authorization: Bearer ' . SENDGRID_API_KEY, - 'Content-Type: application/json' + if ($textContent) $payload['text'] = $textContent; + + $ch = curl_init('https://platform.cyberpersons.com/email/v1/send'); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => json_encode($payload), + CURLOPT_HTTPHEADER => [ + 'Authorization: Bearer ' . $apiKey, + 'Content-Type: application/json', + ], + CURLOPT_TIMEOUT => 20, + CURLOPT_SSL_VERIFYPEER => false, ]); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); - - return $httpCode >= 200 && $httpCode < 300; + return $httpCode === 202; } /**