我正在尝试在Laravel中使用Mailable。

在开发新的Mailable时,除了将EXISTING文件附加到mailable之外,其他所有工作都可以进行。

错误返回如下:

   "message": "Unable to open file for reading [/public/storage/shipments/CJ2K4u6S6uluEGd8spOdYgwNkg8NgLFoC6cF6fm5.pdf]",
    "exception": "Swift_IoException",
    "file": "E:\\webserver\\htdocs\\truckin\\vendor\\swiftmailer\\swiftmailer\\lib\\classes\\Swift\\ByteStream\\FileByteStream.php",
    "line": 131,

但是,如果您浏览文件夹和文件,实际上那里是一个文件,我可以打开它,甚至可以通过ajax弹出窗口打开它以查看详细信息。

这是我的邮件:
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

use App\Shipment;
use App\Shipment_Attachment;

class shipmentAttachments extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $shipment, $attachment, $storagePath;

    public function __construct($shipment, $attachment, $storagePath)
    {
        $this->shipment = $shipment;
        $this->attachment = $attachment;
        $this->attachmentFile = '/public'.$storagePath;
        $this->proNumber = $shipment->pro_number;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
         return $this->from('[email protected]')
                    ->cc('[email protected]')
                    ->subject('New Attachment(s) - '. $this->proNumber)
                    ->view('emails.shipments.shipmentAttachments',['shipment'=> $this->shipment])
                    ->attach($this->attachmentFile);
    }
}

这是我的 Controller ,它导致可发送邮件:
public function attachmentsEmail(Request $request){
        $shipment = Shipment::findOrFail($request->shipmentID);
        $attachment = Shipment_Attachment::findOrFail($request->attachmentID);
        $storagePath = Storage::url($attachment->attachmentPath);
        $email = $request->email;

             Mail::to($email)->send(new shipmentAttachments($shipment, $attachment, $storagePath));  //maybe try to use queue instead of send...
        return back();
    }

因此,我不确定这可能来自何处。

最佳答案

尝试使用public_path()laravel帮助器函数代替“/public”。

$this->attachmentFile = public_path() . '/' . $storagePath;

也许您需要在public/index.php中更改此变量。我在require bootstrap 正下方:
$app->bind('path.public', function() {
    return __DIR__;
});

做一些测试。
dd(public_path());
dd(public_path() . '/' . $storagePath);

或者,可以使用FileSystem类来验证文件是否存在。

希望这对您有所帮助!

09-15 17:23