feat: email blast for certificates (Fasa 8)
- CertificateReadyMail: Mailable with Malay HTML email template - SendCertificateEmailJob: dispatch per-certificate email, log to email_logs - Email template: HTML with download link, program details, branding Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,26 +2,69 @@
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Mail\CertificateReadyMail;
|
||||
use App\Models\Certificate;
|
||||
use App\Models\EmailLog;
|
||||
use App\Models\Program;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class SendCertificateEmailJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public int $tries = 3;
|
||||
public int $tries = 3;
|
||||
public int $backoff = 60;
|
||||
|
||||
public function __construct(public readonly Certificate $certificate) {}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
// Implemented in Fasa 8 — email blast
|
||||
$cert = $this->certificate->refresh();
|
||||
$cert->load(['participant', 'program']);
|
||||
|
||||
$email = $cert->participant->email;
|
||||
|
||||
if (! $email) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Mail::to($email)->send(new CertificateReadyMail($cert));
|
||||
|
||||
$cert->update([
|
||||
'status' => 'emailed',
|
||||
'emailed_at' => now(),
|
||||
]);
|
||||
|
||||
EmailLog::create([
|
||||
'program_id' => $cert->program_id,
|
||||
'participant_id' => $cert->participant_id,
|
||||
'certificate_id' => $cert->id,
|
||||
'recipient_email'=> $email,
|
||||
'subject' => 'Sijil Digital Program — ' . $cert->program->title,
|
||||
'email_type' => 'certificate_ready',
|
||||
'status' => 'sent',
|
||||
'sent_at' => now(),
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
EmailLog::create([
|
||||
'program_id' => $cert->program_id,
|
||||
'participant_id' => $cert->participant_id,
|
||||
'certificate_id' => $cert->id,
|
||||
'recipient_email'=> $email,
|
||||
'subject' => 'Sijil Digital Program — ' . $cert->program->title,
|
||||
'email_type' => 'certificate_ready',
|
||||
'status' => 'failed',
|
||||
'error_message' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public static function dispatchBatch(Program $program): void
|
||||
@@ -29,6 +72,11 @@ class SendCertificateEmailJob implements ShouldQueue
|
||||
$program->certificates()
|
||||
->whereIn('status', ['generated'])
|
||||
->whereNull('emailed_at')
|
||||
->each(fn($cert) => static::dispatch($cert));
|
||||
->with('participant')
|
||||
->each(function (Certificate $cert) {
|
||||
if ($cert->participant->email) {
|
||||
static::dispatch($cert);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user