Laravel Mailable 中的“无法打开文件进行读取”(Swift_IoException)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48568739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Unable to open file for reading" (Swift_IoException) in Laravel Mailable
提问by Matthew
I'm trying to use Mailable in Laravel.
我正在尝试在 Laravel 中使用 Mailable。
In developing a new Mailable, I have everything working except attaching an EXISTING file to the mailable.
在开发新的 Mailable 时,除了将 EXISTING 文件附加到 mailable 之外,我一切正常。
An error returns as such:
错误返回如下:
"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,
But if you go through the folders and files, there is in fact a file there and I can open it, I can even open it through an ajax popup to view details.
但是如果你浏览文件夹和文件,实际上那里有一个文件,我可以打开它,我什至可以通过ajax弹出窗口打开它以查看详细信息。
Here is my mailable:
这是我的邮件:
<?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);
}
}
And here is my controller that leads to the mailable:
这是我的控制器,它导致可邮寄:
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();
}
So I'm not sure where this could be coming from.
所以我不确定这可能来自哪里。
回答by cau
Try to use public_path() laravel helper function instead of '/public'.
尝试使用 public_path() laravel 辅助函数而不是 '/public'。
$this->attachmentFile = public_path() . '/' . $storagePath;
Maybe you need to change this variable in public/index.php. I have right below the require bootstrap:
也许您需要在 public/index.php 中更改此变量。我在 require bootstrap 的正下方:
$app->bind('path.public', function() {
return __DIR__;
});
Make some tests.
做一些测试。
dd(public_path());
dd(public_path() . '/' . $storagePath);
Or maybe verify if file exist with FileSystem class.
或者可以使用 FileSystem 类验证文件是否存在。
Hope this help you!
希望这对你有帮助!
回答by user9707667
I was serching a lot about that, it happens the same when you are tryng to build a PDF on dompdf, just exactly the same, you normaly could write this:
我对此进行了很多搜索,当您尝试在 dompdf 上构建 PDF 时也会发生同样的情况,完全相同,您通常可以这样写:
('/image/'.$file) and will not work , so you can solve it adding a dot just behind the rout ".", just like this:
('/image/'.$file) 并且不会工作,所以你可以解决它在rout“.”后面添加一个点,就像这样:
('./image/'.$file)
('./image/'.$file)
It works when you want to add a attach in a mail sending or when you want to make a PDF including images in it.
当您想在发送的邮件中添加附件或想要制作包含图像的 PDF 时,它会起作用。