apiKey = getSetting('sendgrid_api_key', 'YOUR_SENDGRID_API_KEY_HERE'); $this->fromEmail = getSetting('sendgrid_from_email', 'noreply@tomsjavajive.com'); $this->fromName = getSetting('sendgrid_from_name', "Tom's Java Jive"); } /** * Send email via SendGrid API */ public function send(string $to, string $subject, string $htmlContent, ?string $textContent = null): array { $data = [ 'personalizations' => [ [ 'to' => [['email' => $to]], 'subject' => $subject ] ], 'from' => [ 'email' => $this->fromEmail, 'name' => $this->fromName ], 'content' => [] ]; if ($textContent) { $data['content'][] = ['type' => 'text/plain', 'value' => $textContent]; } $data['content'][] = ['type' => 'text/html', 'value' => $htmlContent]; $ch = curl_init('https://api.sendgrid.com/v3/mail/send'); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $this->apiKey, 'Content-Type: application/json' ], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30 ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($error) { return ['success' => false, 'error' => $error]; } // SendGrid returns 202 for accepted if ($httpCode >= 200 && $httpCode < 300) { return ['success' => true]; } return ['success' => false, 'error' => $response, 'code' => $httpCode]; } /** * Send order confirmation email */ 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}}
If you have any questions, reply to this email or visit our website.
© ' . date('Y') . ' Tom\'s Java Jive. All rights reserved.
Hi {{customer_name}},
Great news! Your order #{{order_number}} is on its way to you.
Tracking Number
{{tracking_number}}
Carrier: {{carrier}}
Please allow 24-48 hours for tracking information to update.
© ' . date('Y') . ' 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 password:
This link will expire in {{expires}}. If you didn\'t request this, you can safely ignore this email.
© ' . date('Y') . ' 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
© ' . date('Y') . ' 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!
Need help? Just reply to this email and we\'ll assist you!
© ' . date('Y') . ' Tom\'s Java Jive. All rights reserved.
Email template not found.
'; foreach ($vars as $key => $value) { $template = str_replace('{{' . $key . '}}', $value, $template); } return $template; } } // Helper function for easy access function sendEmail(): SendGridEmail { static $instance = null; if ($instance === null) { $instance = new SendGridEmail(); } return $instance; }