$this->order,
'invoiceNumber' => $this->order->order_number,
])->render();
} catch (\InvalidArgumentException $e) {
// If the mail::message namespace isn't available, render without it
// This is a fallback for when the mail views aren't published
Log::warning('Mail views not published, using basic HTML template', [
'order_id' => $this->order->id,
]);
$htmlContent = $this->getBasicInvoiceHtml();
}
// Get customer name (fallback to email if name not available)
$customerName = $this->order->customer_name ?? explode('@', $this->order->customer_email)[0];
// Send via Mailjet
$success = $mailjetService->send(
toEmail: $this->order->customer_email,
toName: $customerName,
subject: 'Invoice #' . $this->order->order_number . ' - ADDITIONAL DESIGN',
htmlContent: $htmlContent,
attachments: [$this->invoicePath]
);
if (!$success) {
throw new \Exception('Mailjet send returned false');
}
Log::info('Invoice email sent successfully via Mailjet', [
'order_id' => $this->order->id,
'order_number' => $this->order->order_number,
'customer_email' => $this->order->customer_email,
]);
} catch (\Exception $e) {
Log::error('Failed to send invoice email via Mailjet', [
'order_id' => $this->order->id,
'order_number' => $this->order->order_number,
'customer_email' => $this->order->customer_email,
'error' => $e->getMessage(),
'attempt' => $this->attempts(),
]);
// Re-throw to trigger queue retry mechanism
throw $e;
}
}
/**
* Fallback basic HTML template for invoice email
*/
private function getBasicInvoiceHtml(): string
{
return <<
Dear {$this->order->customer_name ?: 'Customer'},
Please find your invoice attached to this email.
Order Details
| Order Number |
{$this->order->order_number} |
| Order Date |
{$this->order->created_at->format('d M Y')} |
| Total Amount |
R {$this->order->total} |
Thank you for your order!
HTML;
}
public function failed(\Throwable $exception): void
{
Log::critical('Invoice email job failed permanently after all retries', [
'order_id' => $this->order->id,
'order_number' => $this->order->order_number,
'customer_email' => $this->order->customer_email,
'error' => $exception->getMessage(),
]);
}
}