问题描述
我正在尝试在Laravel中使用Mailable.
I'm trying to use Mailable in Laravel.
在开发新的Mailable时,除了将EXISTING文件附加到mailable之外,其他所有工作都可以进行.
In developing a new Mailable, I have everything working except attaching an EXISTING file to the 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弹出窗口打开它以查看详细信息.
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.
这是我的可邮寄地址:
<?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.
推荐答案
尝试使用public_path()laravel帮助函数代替"/public".
Try to use public_path() laravel helper function instead of '/public'.
$this->attachmentFile = public_path() . '/' . $storagePath;
也许您需要在public/index.php中更改此变量.我在require引导程序的正下方:
Maybe you need to change this variable in public/index.php. I have right below the require bootstrap:
$app->bind('path.public', function() {
return __DIR__;
});
进行一些测试.
dd(public_path());
dd(public_path() . '/' . $storagePath);
或者可以验证FileSystem类是否存在文件.
Or maybe verify if file exist with FileSystem class.
希望这对您有所帮助!
这篇关于“无法打开文件进行读取"Laravel Mailable中的(Swift_IoException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!