2025-03-05 13:44:58 +07:00
|
|
|
<?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,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-03-05 14:13:07 +07:00
|
|
|
dd(json_decode($response->getBody(), true));
|
|
|
|
|
|
2025-03-05 13:44:58 +07:00
|
|
|
return json_decode($response->getBody(), true);
|
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
|
Log::error('Brevo email failed to send', [
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'recipient' => $recipientEmail,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-03-05 14:13:07 +07:00
|
|
|
dd($e->getMessage());
|
2025-03-05 13:44:58 +07:00
|
|
|
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,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return json_decode($response->getBody(), true);
|
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
|
Log::error('Brevo email failed to send', [
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'recipient' => $recipientEmail,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|