问题
I'm trying to use Mailable in Laravel.
In developing a new Mailable, I have everything working except attaching an EXISTING file to the 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.
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('billing@cmxtrucking.com')
->cc('billing@cmxtrucking.com')
->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.
回答1:
Try to use public_path() laravel helper function instead of '/public'.
$this->attachmentFile = public_path() . '/' . $storagePath;
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__;
});
Make some tests.
dd(public_path());
dd(public_path() . '/' . $storagePath);
Or maybe verify if file exist with FileSystem class.
Hope this help you!
回答2:
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:
('/image/'.$file) and will not work , so you can solve it adding a dot just behind the rout ".", just like this:
('./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.
回答3:
If you use Storage, and you are trying to export xlsx files, using Laravel Notifications:
in your notification class:
public function toMail($notifiable) {
$path = Storage::disk('export')->getAdapter()->getPathPrefix();
return (new MailMessage)
->greeting(language_data('Your file is ready', $this->user->language_id).$this->user->name)
->line(language_data('Please, check your Email attachments.', $this->user->language_id))
->subject(language_data('Export Contacts', $this->user->language_id))
->attach($path.$notifiable->filename, [ 'as' => $notifiable->filename, 'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', ])
->line(language_data('If you did not request this file, please contact us.', $this->user->language_id));
}
It works fine for me.
来源:https://stackoverflow.com/questions/48568739/unable-to-open-file-for-reading-swift-ioexception-in-laravel-mailable