generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command to retry failed sent emails
- Loading branch information
Showing
6 changed files
with
232 additions
and
79 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
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,83 @@ | ||
<?php | ||
|
||
namespace MailCarrier\Actions\Logs; | ||
|
||
use Filament\Notifications\Notification; | ||
use Illuminate\Support\Collection; | ||
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile; | ||
use MailCarrier\Actions\Action; | ||
use MailCarrier\Actions\SendMail; | ||
use MailCarrier\Dto\AttachmentDto; | ||
use MailCarrier\Dto\SendMailDto; | ||
use MailCarrier\Enums\LogStatus; | ||
use MailCarrier\Models\Attachment; | ||
use MailCarrier\Models\Log; | ||
use Throwable; | ||
|
||
class ResendEmail extends Action | ||
{ | ||
public function run(Log $log, array $data = []): void | ||
{ | ||
$attachments = $data['attachments'] ?? Collection::make([]); | ||
|
||
// Update the status to Failed just to send email again | ||
if ($log->isSent()) { | ||
$log->update([ | ||
'status' => LogStatus::Failed, | ||
]); | ||
} | ||
|
||
try { | ||
SendMail::resolve()->run( | ||
new SendMailDto([ | ||
'template' => $log->template->slug, | ||
'subject' => $log->subject, | ||
'sender' => $log->sender, | ||
'recipient' => $log->recipient, | ||
'cc' => $log->cc->all(), | ||
'bcc' => $log->bcc->all(), | ||
'variables' => $log->variables, | ||
'trigger' => $log->trigger, | ||
'tags' => $log->tags ?: [], | ||
'metadata' => $log->metadata ?: [], | ||
'attachments' => $log->attachments | ||
->map( | ||
fn (Attachment $attachment) => !$attachment->canBeDownloaded() | ||
? null | ||
: new AttachmentDto( | ||
name: $attachment->name, | ||
content: $attachment->content, | ||
size: $attachment->size | ||
) | ||
) | ||
->filter() | ||
->merge( | ||
Collection::make($attachments)->map( | ||
fn (TemporaryUploadedFile $file) => new AttachmentDto( | ||
name: $file->getClientOriginalName(), | ||
content: base64_encode(file_get_contents($file->getRealPath())), | ||
size: $file->getSize() | ||
) | ||
) | ||
) | ||
->all(), | ||
]), | ||
$log | ||
); | ||
} catch (Throwable $e) { | ||
Notification::make() | ||
->title('Error while sending email') | ||
->body($e->getMessage()) | ||
->danger() | ||
->send(); | ||
|
||
return; | ||
} | ||
|
||
Notification::make() | ||
->icon('heroicon-o-paper-airplane') | ||
->title('Email sent correctly') | ||
->success() | ||
->send(); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace MailCarrier\Commands; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Str; | ||
use MailCarrier\Actions\Logs\ResendEmail; | ||
use MailCarrier\Enums\LogStatus; | ||
use MailCarrier\Models\Log; | ||
|
||
class LogRetryCommand extends Command | ||
{ | ||
public $signature = 'mailcarrier:log-retry {--date=}'; | ||
|
||
public $description = 'Retry sending emails from log that failed.'; | ||
|
||
public function handle(): int | ||
{ | ||
$this->info('Retrying failed logs...'); | ||
|
||
$date = $this->option('date') | ||
? Carbon::parse($this->option('date')) | ||
: Carbon::now()->subDay(); | ||
|
||
$resendEmailAction = new ResendEmail(); | ||
|
||
$retriedCount = tap( | ||
Log::query() | ||
->whereDate('created_at', '=', $date) | ||
->where('status', LogStatus::Failed) | ||
->get() | ||
) | ||
->each(fn (Log $log) => $resendEmailAction->run($log)) | ||
->count(); | ||
|
||
$this->info($retriedCount . ' ' . Str::plural('log', $retriedCount) . ' retried.'); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
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
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,92 @@ | ||
<?php | ||
|
||
use Carbon\CarbonImmutable as Carbon; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Mail; | ||
use MailCarrier\Enums\LogStatus; | ||
use MailCarrier\Mail\GenericMail; | ||
use MailCarrier\Models\Log; | ||
|
||
beforeEach(function () { | ||
// Disable auth | ||
Config::set('mailcarrier.api_endpoint.auth_guard', null); | ||
|
||
Config::set('mailcarrier.queue.force', false); | ||
Mail::fake(); | ||
}); | ||
|
||
it('resends emails from failed logs', function () { | ||
$log = Log::factory()->create([ | ||
'status' => LogStatus::Failed, | ||
'created_at' => Carbon::now()->subDay(), | ||
]); | ||
|
||
$log2 = Log::factory()->create([ // This log should not be resent | ||
'status' => LogStatus::Failed, | ||
'created_at' => Carbon::now()->subDays(2), | ||
]); | ||
|
||
$this->artisan('mailcarrier:log-retry') | ||
->expectsOutput('Retrying failed logs...') | ||
->expectsOutput('1 log retried.') | ||
->assertExitCode(0); | ||
|
||
Mail::assertSent(GenericMail::class, function (GenericMail $mail) use ($log) { | ||
$mail->build(); | ||
|
||
return $mail->hasTo($log->recipient) && | ||
$mail->hasSubject($log->subject) && | ||
$mail->hasFrom($log->sender->email); | ||
}); | ||
|
||
Mail::assertNotSent(GenericMail::class, function (GenericMail $mail) use ($log2) { | ||
$mail->build(); | ||
|
||
return $mail->hasTo($log2->recipient) && | ||
$mail->hasSubject($log2->subject) && | ||
$mail->hasFrom($log2->sender->email); | ||
}); | ||
|
||
expect($log->refresh()->status)->toBe(LogStatus::Sent); | ||
expect($log->error)->toBeNull(); | ||
|
||
expect($log2->refresh()->status)->toBe(LogStatus::Failed); | ||
}); | ||
|
||
it('resends emails from failed logs with a specific date', function () { | ||
$log = Log::factory()->create([ | ||
'status' => LogStatus::Failed, | ||
'created_at' => '2021-01-01 00:00:00', | ||
]); | ||
|
||
$log2 = Log::factory()->create([ // This log should not be resent | ||
'status' => LogStatus::Failed, | ||
'created_at' => '2021-01-03 00:00:00', | ||
]); | ||
|
||
$this->artisan('mailcarrier:log-retry --date=2021-01-01') | ||
->expectsOutput('Retrying failed logs...') | ||
->expectsOutput('1 log retried.') | ||
->assertExitCode(0); | ||
|
||
Mail::assertSent(GenericMail::class, function (GenericMail $mail) use ($log) { | ||
$mail->build(); | ||
|
||
return $mail->hasTo($log->recipient) && | ||
$mail->hasSubject($log->subject) && | ||
$mail->hasFrom($log->sender->email); | ||
}); | ||
|
||
Mail::assertNotSent(GenericMail::class, function (GenericMail $mail) use ($log2) { | ||
$mail->build(); | ||
|
||
return $mail->hasTo($log2->recipient) && | ||
$mail->hasSubject($log2->subject) && | ||
$mail->hasFrom($log2->sender->email); | ||
}); | ||
|
||
expect($log->refresh()->status)->toBe(LogStatus::Sent); | ||
expect($log->error)->toBeNull(); | ||
|
||
expect($log2->refresh()->status)->toBe(LogStatus::Failed); | ||
}); |