apiKey = getSetting('cybermail_api_key', defined('CYBERMAIL_API_KEY') ? CYBERMAIL_API_KEY : ''); $this->fromEmail = getSetting('cybermail_from_email', defined('SENDER_EMAIL') ? SENDER_EMAIL : 'noreply@tomsjavajive.com'); $this->fromName = getSetting('cybermail_from_name', defined('SENDER_NAME') ? SENDER_NAME : "Tom's Java Jive"); } private function logEmail(string $to, string $subject, string $html, array $result, array $options = []): void { try { $preview = strip_tags($html); $preview = preg_replace('/\s+/', ' ', trim($preview)); $preview = substr($preview, 0, 500); // Find customer_id by email $customer = db()->fetch( "SELECT customer_id FROM customers WHERE email = :email LIMIT 1", ['email' => $to] ); db()->insert('email_log', [ 'customer_id' => $customer['customer_id'] ?? null, 'recipient_email'=> $to, 'subject' => $subject, 'preview' => $preview, 'message_id' => $result['message_id'] ?? null, 'status' => $result['success'] ? 'sent' : 'failed', 'error_message' => $result['success'] ? null : ($result['error'] ?? null), 'tags' => isset($options['tags']) ? implode(',', $options['tags']) : null, ]); } catch (\Throwable $e) { // Never let logging break email sending } } public function checkDeliveryStatus(string $messageId): array { $ch = curl_init("https://platform.cyberpersons.com/email/v1/messages/" . urlencode($messageId)); curl_setopt_array($ch, [ CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $this->apiKey], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, ]); $response = curl_exec($ch); curl_close($ch); $body = json_decode($response, true); return $body['data'] ?? []; } public function send(string $to, string $subject, string $html, ?string $text = null, array $options = []): array { $payload = array_merge([ 'from' => $this->fromEmail, 'to' => $to, 'subject' => $subject, 'html' => $html, ], $options); if ($text) $payload['text'] = $text; $ch = curl_init('https://platform.cyberpersons.com/email/v1/send'); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($payload), CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $this->apiKey, 'Content-Type: application/json'], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, CURLOPT_SSL_VERIFYPEER => false, ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $body = json_decode($response, true); if ($httpCode === 202) { $result = ['success' => true, 'message_id' => $body['data']['message_id'] ?? null]; $this->logEmail($to, $subject, $html, $result, $options); return $result; } $errorMsg = $body['error']['message'] ?? 'Unknown error'; $result = ['success' => false, 'error' => $errorMsg, 'code' => $httpCode]; $this->logEmail($to, $subject, $html, $result, $options); return $result; } public function sendOrderConfirmation(array $order): array { $items = json_decode($order['items'], true); $itemsHtml = ''; foreach ($items as $item) { $itemsHtml .= sprintf( '
Hi {{customer_name}},
Thank you for your order! We\'ve received it and will begin processing right away.
{{order_date}}
| Item | Qty | Price |
|---|
Subtotal: {{subtotal}}
Tax: {{tax}}
Discount: {{discount}}
Total: {{total}}
Payment Method: {{payment_method}}
© ' . $year . ' Tom\'s Java Jive. All rights reserved.
Hi {{customer_name}},
Great news! Your order #{{order_number}} is on its way.
Tracking Number
{{tracking_number}}
Carrier: {{carrier}}
© ' . $year . ' Tom\'s Java Jive. All rights reserved.
Hi {{customer_name}},
We received a request to reset your password. Click the button below to create a new one:
This link expires in {{expires}}. If you didn\'t request this, ignore this email.
© ' . $year . ' Tom\'s Java Jive. All rights reserved.
Hi {{customer_name}},
Welcome to Tom\'s Java Jive! We\'re thrilled to have you join our community of coffee lovers.
Cheers,
The Tom\'s Java Jive Team
© ' . $year . ' Tom\'s Java Jive. All rights reserved.
Hey there!
We noticed you left some amazing items in your cart. Don\'t let them get away!
© ' . $year . ' Tom\'s Java Jive. All rights reserved.
Email template not found.
'; foreach ($vars as $key => $value) { $template = str_replace('{{' . $key . '}}', $value, $template); } return $template; } } function emailService(): Email { static $instance = null; if ($instance === null) { $instance = new Email(); } return $instance; }