【问题标题】:Laravel Email Attach From Storage File Not Found ExceptionLaravel 电子邮件从存储文件附加未找到异常
【发布时间】:2020-08-09 18:20:25
【问题描述】:

最近我正在尝试从本地存储发送带有附件的电子邮件。 文件放在storage/app/reservation/this-is.pdf

这是我的邮件

public function build()
{
    return $this->markdown('email.reservation_info')
        ->subject('Your Hotel Reservation')
        ->attachFromStorage($this->pdfPath,$this->pdfName,[
            'mime' => 'application/pdf'
        ])
        ->with([
            'result' => $this->result
        ]);
}

作为上面的代码,我输入了 pdf 路径以及 pdf 名称。但我遇到了这样的错误

Illuminate\Contracts\Filesystem\FileNotFoundException    
/Applications/XAMPP/xamppfiles/htdocs/kema/storage/app/reservation/DEV2008070001.pdf

我也尝试检查文件是否存在,但是当我检查文件是否已经存在时。

你能帮帮我吗?

【问题讨论】:

    标签: laravel


    【解决方案1】:

    您在attachFromStorage 中的参数似乎有误 来自 laravel 的源码

     /**
     * Attach a file to the message from storage.
     *
     * @param  string  $disk
     * @param  string  $path
     * @param  string|null  $name
     * @param  array  $options
     * @return $this
     */
    public function attachFromStorageDisk($disk, $path, $name = null, array $options = [])
    {
        $this->diskAttachments = collect($this->diskAttachments)->push([
            'disk' => $disk,
            'path' => $path,
            'name' => $name ?? basename($path),
            'options' => $options,
        ])->unique(function ($file) {
            return $file['name'].$file['disk'].$file['path'];
        })->all();
    
        return $this;
    }
    

    这应该可以修复您的代码

    return $this->markdown('email.reservation_info')
        ->subject('Your Hotel Reservation')
        ->attachFromStorageDisk('local', $this->pdfPath,$this->pdfName,[
            'mime' => 'application/pdf'
        ])
        ->with([
            'result' => $this->result
        ]);
    

    $this->pdfPath 应该是 pdf 文件的相对路径,而不是绝对路径。如果您有完整路径,则使用attach 方法(完整签名public function attach($file, array $options = [])

    【讨论】:

    • 您好,感谢您的评论。我收到此错误 Symfony\Component\Debug\Exception\FatalThrowableError 传递给 Illuminate\Mail\Mailable::attachFromStorage() 的参数 3 必须是数组类型,给定字符串,在 /Applications/XAMPP/xamppfiles/htdocs/kema/ 中调用app/Mail/SendInfoReservation.php 第 37 行
    • 你运行的是什么版本的 laravel?
    • 目前使用6.0
    • 我打错了,用attachFromStorageDisk
    • 仍然找不到错误文件..即使我使用绝对或相对文件路径仍然找不到。 $pdfPath = storage_path('app/reservation/'.$pdfName);或 $pdfPath = storage_path('app/reservation');
    【解决方案2】:

    我遇到了同样的问题。您正在使用attachFromStorage,正如 Dusan Malusav 指出的那样,它需要一个相对路径。 All your arguments are correct 用于 attachFromStorage 方法。因此,您要么只使用:
    app/reservation/DEV2008070001.pdf 作为相对路径,因为 attachFromStorage 将填充其余部分,

    或者使用绝对路径:

    ->attach('/Applications/XAMPP/xamppfiles/htdocs/kema/storage/app/reservation/DEV2008070001.pdf', [ //absolute path
           'as' => 'DEV2008070001.pdf',
           'mime' => 'application/pdf'
     ]);
    

    【讨论】:

      猜你喜欢
      • 2011-12-18
      • 2017-07-27
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多