Laravel mail's attachment missing from production but works in localhost

166 Views Asked by At

I have created mail job with attachment which works very fine from localhost (window) but when I run same code in server (ubuntu 18.4) I am receiving email but without attachment. Couldn't figure out the actual issue. No error log is found in storage or Apache logs.

Mail::send('client.email', $body, function ($message) use ($to, $subject, $from, $cc)
{
    $message->to($to);
    $message->from($from);
    $message->subject($subject);

    //attach if attachmentUrl exists
    if(!empty($this->email['attachmentUrl ']))
    {
      //get file full path
      // output in ubuntu =
      // "/var/www/html/storage/app/public/emails/rz8l61vOkLliGbJ.jpg"
      $file = storage_path('app/public/'.$this->email['attachmentUrl']);

      //get attachement's extension
      // output = jpg
      $fileExt = File::extension($file);

      //give new name to attachement
      // output = attachement.jpg
      $filename = 'attachement.'.$fileExt;

      //Get attachement mime type
      // output = image/jpg
      $mimeType = MimeType::get_mime_type($file);

      $message->attachData(file_get_contents($file),$filename,['mime' => $mimeType]);
    }
});

As checked, file exist in said storage location of server. Not sure what I am missing here. Even i tried with file permission: chmod -R 775 /var/www/html/storage

Using Laravel 8.0.4

0

There are 0 best solutions below