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
+23 -35
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;
}
$payload = [
$data = [ 'from' => ['email' => SENDER_EMAIL, 'name' => SENDER_NAME],
'personalizations' => [ 'to' => [['email' => $to]],
[ 'subject' => $subject,
'to' => [['email' => $to]], 'html' => $htmlContent,
'subject' => $subject
]
],
'from' => [
'email' => SENDER_EMAIL,
'name' => SENDER_NAME
],
'content' => [
['type' => 'text/html', 'value' => $htmlContent]
]
]; ];
if ($textContent) $payload['text'] = $textContent;
if ($textContent) {
array_unshift($data['content'], ['type' => 'text/plain', 'value' => $textContent]); $ch = curl_init('https://platform.cyberpersons.com/email/v1/send');
} curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
$ch = curl_init(); CURLOPT_POST => true,
curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/v3/mail/send'); CURLOPT_POSTFIELDS => json_encode($payload),
curl_setopt($ch, CURLOPT_POST, true); CURLOPT_HTTPHEADER => [
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 'Authorization: Bearer ' . $apiKey,
curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json',
'Authorization: Bearer ' . SENDGRID_API_KEY, ],
'Content-Type: application/json' CURLOPT_TIMEOUT => 20,
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;
} }
/** /**