Switch sendEmail() in functions.php to CyberMail API

This commit is contained in:
2026-05-29 15:02:17 +00:00
parent d30851e3ec
commit 25e96a3d5f
+20 -32
View File
@@ -326,48 +326,36 @@ function getCartTotal() {
} }
/** /**
* Send email using SendGrid * Send email via CyberMail API
*/ */
function sendEmail($to, $subject, $htmlContent, $textContent = '') { function sendEmail($to, $subject, $htmlContent, $textContent = '') {
if (empty(SENDGRID_API_KEY)) { $apiKey = defined('CYBERMAIL_API_KEY') ? CYBERMAIL_API_KEY : '';
return false; if (!$apiKey) return false;
}
$data = [ $payload = [
'personalizations' => [ 'from' => ['email' => SENDER_EMAIL, 'name' => SENDER_NAME],
[
'to' => [['email' => $to]], 'to' => [['email' => $to]],
'subject' => $subject 'subject' => $subject,
] 'html' => $htmlContent,
],
'from' => [
'email' => SENDER_EMAIL,
'name' => SENDER_NAME
],
'content' => [
['type' => 'text/html', 'value' => $htmlContent]
]
]; ];
if ($textContent) $payload['text'] = $textContent;
if ($textContent) { $ch = curl_init('https://platform.cyberpersons.com/email/v1/send');
array_unshift($data['content'], ['type' => 'text/plain', 'value' => $textContent]); curl_setopt_array($ch, [
} CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
$ch = curl_init(); CURLOPT_POSTFIELDS => json_encode($payload),
curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/v3/mail/send'); CURLOPT_HTTPHEADER => [
curl_setopt($ch, CURLOPT_POST, true); 'Authorization: Bearer ' . $apiKey,
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 'Content-Type: application/json',
curl_setopt($ch, CURLOPT_HTTPHEADER, [ ],
'Authorization: Bearer ' . SENDGRID_API_KEY, CURLOPT_TIMEOUT => 20,
'Content-Type: application/json' CURLOPT_SSL_VERIFYPEER => false,
]); ]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); $response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); curl_close($ch);
return $httpCode === 202;
return $httpCode >= 200 && $httpCode < 300;
} }
/** /**