Files
expense/app/Services/BrevoService.php
T
2025-03-05 14:15:27 +07:00

223 lines
7.5 KiB
PHP

<?php
namespace App\Services;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use App\Mail\ExpenseCreated;
use App\Mail\ExpenseApproved;
use App\Mail\ExpenseApproved2;
use App\Mail\ExpenseRejected;
use App\Mail\FinalApprove;
use Illuminate\Support\Facades\Log;
class BrevoService
{
protected $client;
protected $apiKey;
protected $mailFromAddress;
protected $mailFromName;
public function __construct()
{
$this->client = new Client();
$this->apiKey = env('BREVO_API_KEY');
$this->mailFromAddress = env('BREVO_MAIL_FROM_ADDRESS');
$this->mailFromName = env('BREVO_MAIL_FROM_NAME');
}
public function expenseCreated($recipientEmail, $recipientName, ExpenseCreated $mail)
{
$htmlContent = $mail->renderHtml(); // Convert Blade view to HTML
try {
$response = $this->client->post('https://api.brevo.com/v3/smtp/email', [
'headers' => [
'accept' => 'application/json',
'api-key' => $this->apiKey,
'content-type' => 'application/json',
],
'json' => [
'sender' => [
'name' => $this->mailFromName,
'email' => $this->mailFromAddress,
],
'to' => [
[
'email' => $recipientEmail,
'name' => $recipientName,
],
],
'subject' => 'Expense Created',
'htmlContent' => $htmlContent,
],
'verify' => false,
]);
return json_decode($response->getBody(), true);
} catch (RequestException $e) {
Log::error('Brevo email failed to send', [
'error' => $e->getMessage(),
'recipient' => $recipientEmail,
]);
return null;
}
}
public function expenseApproved($recipientEmail, $recipientName, ExpenseApproved $mail)
{
$htmlContent = $mail->renderHtml(); // Convert Blade view to HTML
try {
$response = $this->client->post('https://api.brevo.com/v3/smtp/email', [
'headers' => [
'accept' => 'application/json',
'api-key' => $this->apiKey,
'content-type' => 'application/json',
],
'json' => [
'sender' => [
'name' => $this->mailFromName,
'email' => $this->mailFromAddress,
],
'to' => [
[
'email' => $recipientEmail,
'name' => $recipientName,
],
],
'subject' => 'Expense First Approval',
'htmlContent' => $htmlContent,
],
'verify' => false, // Disable SSL verification
]);
return json_decode($response->getBody(), true);
} catch (RequestException $e) {
Log::error('Brevo email failed to send', [
'error' => $e->getMessage(),
'recipient' => $recipientEmail,
]);
return null;
}
}
public function expenseApproved2($recipientEmail, $recipientName, ExpenseApproved2 $mail)
{
$htmlContent = $mail->renderHtml(); // Convert Blade view to HTML
try {
$response = $this->client->post('https://api.brevo.com/v3/smtp/email', [
'headers' => [
'accept' => 'application/json',
'api-key' => $this->apiKey,
'content-type' => 'application/json',
],
'json' => [
'sender' => [
'name' => $this->mailFromName,
'email' => $this->mailFromAddress,
],
'to' => [
[
'email' => $recipientEmail,
'name' => $recipientName,
],
],
'subject' => 'Expense Second Approval',
'htmlContent' => $htmlContent,
],
'verify' => false, // Disable SSL verification
]);
return json_decode($response->getBody(), true);
} catch (RequestException $e) {
Log::error('Brevo email failed to send', [
'error' => $e->getMessage(),
'recipient' => $recipientEmail,
]);
return null;
}
}
public function expenseFinalApproved($recipientEmail, $recipientName, FinalApprove $mail)
{
$htmlContent = $mail->renderHtml(); // Convert Blade view to HTML
try {
$response = $this->client->post('https://api.brevo.com/v3/smtp/email', [
'headers' => [
'accept' => 'application/json',
'api-key' => $this->apiKey,
'content-type' => 'application/json',
],
'json' => [
'sender' => [
'name' => $this->mailFromName,
'email' => $this->mailFromAddress,
],
'to' => [
[
'email' => $recipientEmail,
'name' => $recipientName,
],
],
'subject' => 'Expense Final Approval',
'htmlContent' => $htmlContent,
],
'verify' => false, // Disable SSL verification
]);
return json_decode($response->getBody(), true);
} catch (RequestException $e) {
Log::error('Brevo email failed to send', [
'error' => $e->getMessage(),
'recipient' => $recipientEmail,
]);
return null;
}
}
public function expenseRejected($recipientEmail, $recipientName, ExpenseRejected $mail)
{
$htmlContent = $mail->renderHtml(); // Convert Blade view to HTML
try {
$response = $this->client->post('https://api.brevo.com/v3/smtp/email', [
'headers' => [
'accept' => 'application/json',
'api-key' => $this->apiKey,
'content-type' => 'application/json',
],
'json' => [
'sender' => [
'name' => $this->mailFromName,
'email' => $this->mailFromAddress,
],
'to' => [
[
'email' => $recipientEmail,
'name' => $recipientName,
],
],
'subject' => 'Expense Rejected',
'htmlContent' => $htmlContent,
],
'verify' => false, // Disable SSL verification
]);
return json_decode($response->getBody(), true);
} catch (RequestException $e) {
Log::error('Brevo email failed to send', [
'error' => $e->getMessage(),
'recipient' => $recipientEmail,
]);
return null;
}
}
}