-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #325 from liberu-billing/sweep/Enhance-Email-Notif…
…ication-and-Billing-Services-with-Robust-Email-Handling Enhance Email Notification and Billing Services with Robust Email Handling
- Loading branch information
Showing
4 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
|
||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Support\Facades\Mail; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class SendEmailNotification implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
protected $mailable; | ||
protected $recipient; | ||
|
||
public $tries = 3; | ||
public $backoff = 300; // 5 minutes | ||
|
||
public function __construct(Mailable $mailable, string $recipient) | ||
{ | ||
$this->mailable = $mailable; | ||
$this->recipient = $recipient; | ||
} | ||
|
||
public function handle() | ||
{ | ||
try { | ||
Mail::to($this->recipient)->send($this->mailable); | ||
|
||
Log::info('Queued email sent successfully', [ | ||
'recipient' => $this->recipient, | ||
'mailable_class' => get_class($this->mailable) | ||
]); | ||
} catch (\Exception $e) { | ||
Log::error('Failed to send queued email', [ | ||
'recipient' => $this->recipient, | ||
'mailable_class' => get_class($this->mailable), | ||
'error' => $e->getMessage(), | ||
'attempt' => $this->attempts() | ||
]); | ||
|
||
throw $e; | ||
} | ||
} | ||
|
||
public function failed(\Throwable $exception) | ||
{ | ||
Log::error('Email job failed permanently', [ | ||
'recipient' => $this->recipient, | ||
'mailable_class' => get_class($this->mailable), | ||
'error' => $exception->getMessage() | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
|
||
<?php | ||
|
||
namespace App\Mail; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Mail\Mailables\Content; | ||
use Illuminate\Mail\Mailables\Envelope; | ||
use Illuminate\Queue\SerializesModels; | ||
use App\Models\Payment; | ||
|
||
class PaymentConfirmation extends Mailable | ||
{ | ||
use Queueable, SerializesModels; | ||
|
||
public $payment; | ||
|
||
public function __construct(Payment $payment) | ||
{ | ||
$this->payment = $payment; | ||
} | ||
|
||
public function envelope(): Envelope | ||
{ | ||
return new Envelope( | ||
subject: 'Payment Confirmation - Invoice #' . $this->payment->invoice->invoice_number, | ||
); | ||
} | ||
|
||
public function content(): Content | ||
{ | ||
return new Content( | ||
view: 'emails.payment-confirmation', | ||
); | ||
} | ||
|
||
public function attachments(): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
|
||
|
||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use Illuminate\Support\Facades\Mail; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Support\Facades\Queue; | ||
use App\Jobs\SendEmailNotification; | ||
|
||
class EmailNotificationService | ||
{ | ||
protected $maxRetries = 3; | ||
protected $retryDelay = 300; // 5 minutes | ||
|
||
public function send(Mailable $mailable, string $recipient) | ||
{ | ||
try { | ||
Queue::push(new SendEmailNotification($mailable, $recipient)); | ||
|
||
Log::info('Email notification queued', [ | ||
'recipient' => $recipient, | ||
'mailable_class' => get_class($mailable) | ||
]); | ||
|
||
return true; | ||
} catch (\Exception $e) { | ||
Log::error('Failed to queue email notification', [ | ||
'recipient' => $recipient, | ||
'mailable_class' => get_class($mailable), | ||
'error' => $e->getMessage() | ||
]); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
public function sendNow(Mailable $mailable, string $recipient) | ||
{ | ||
$attempts = 0; | ||
|
||
while ($attempts < $this->maxRetries) { | ||
try { | ||
Mail::to($recipient)->send($mailable); | ||
|
||
Log::info('Email notification sent successfully', [ | ||
'recipient' => $recipient, | ||
'mailable_class' => get_class($mailable), | ||
'attempt' => $attempts + 1 | ||
]); | ||
|
||
return true; | ||
} catch (\Exception $e) { | ||
$attempts++; | ||
|
||
Log::warning('Email sending failed, retrying...', [ | ||
'recipient' => $recipient, | ||
'mailable_class' => get_class($mailable), | ||
'attempt' => $attempts, | ||
'error' => $e->getMessage() | ||
]); | ||
|
||
if ($attempts >= $this->maxRetries) { | ||
Log::error('Email sending failed after max retries', [ | ||
'recipient' => $recipient, | ||
'mailable_class' => get_class($mailable), | ||
'error' => $e->getMessage() | ||
]); | ||
return false; | ||
} | ||
|
||
sleep($this->retryDelay); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |