ExportNotification.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Mail;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Mail\Mailable;
  5. use Illuminate\Queue\SerializesModels;
  6. use Illuminate\Support\Facades\Log;
  7. class ExportNotification extends Mailable
  8. {
  9. use Queueable, SerializesModels;
  10. public $emailData;
  11. private $filePath;
  12. private $fileName;
  13. /**
  14. * Create a new message instance.
  15. */
  16. public function __construct($emailData, $filePath, $fileName)
  17. {
  18. $this->emailData = $emailData;
  19. $this->filePath = $filePath;
  20. $this->fileName = $fileName;
  21. }
  22. /**
  23. * Build the message.
  24. */
  25. public function build()
  26. {
  27. try {
  28. $email = $this->subject($this->emailData['subject'])
  29. ->view('emails.export-notification')
  30. ->with('data', $this->emailData);
  31. if (file_exists($this->filePath)) {
  32. $email->attach($this->filePath, [
  33. 'as' => $this->fileName,
  34. 'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  35. ]);
  36. Log::info('Email attachment added', [
  37. 'file_path' => $this->filePath,
  38. 'file_name' => $this->fileName,
  39. 'file_size' => filesize($this->filePath)
  40. ]);
  41. } else {
  42. Log::warning('Export file not found for email attachment', [
  43. 'file_path' => $this->filePath
  44. ]);
  45. }
  46. return $email;
  47. } catch (\Exception $e) {
  48. Log::error('Error building export notification email', [
  49. 'error' => $e->getMessage(),
  50. 'file_path' => $this->filePath,
  51. 'file_name' => $this->fileName
  52. ]);
  53. throw $e;
  54. }
  55. }
  56. }