laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题

举报
lxw1844912514 发表于 2022/03/27 23:04:58 2022/03/27
【摘要】 https://stackoverflow.com/questions/48568739/unable-to-open-file-for-reading-swift-ioexception-in-laravel-mailable “Unable to open file for reading” (Swift_IoExcept...

https://stackoverflow.com/questions/48568739/unable-to-open-file-for-reading-swift-ioexception-in-laravel-mailable

“Unable to open file for reading” (Swift_IoException) in Laravel Mailable

 

I'm trying to use Mailable in Laravel, I have run into an issue that I haven't come across before and it appears nothing currently out there can help.

In developing a new Mailable, I have everything working except attaching an EXISTING file to the mailable.

An error returns as such:


      
  1. "message": "Unable to open file for reading [/public/storage/shipments/CJ2K4u6S6uluEGd8spOdYgwNkg8NgLFoC6cF6fm5.pdf]",
  2. "exception": "Swift_IoException",
  3. "file": "E:\\webserver\\htdocs\\truckin\\vendor\\swiftmailer\\swiftmailer\\lib\\classes\\Swift\\ByteStream\\FileByteStream.php",
  4. "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:


      
  1. <?php
  2. namespace App\Mail;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Mail\Mailable;
  5. use Illuminate\Queue\SerializesModels;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use App\Shipment;
  8. use App\Shipment_Attachment;
  9. class shipmentAttachments extends Mailable
  10. {
  11. use Queueable, SerializesModels;
  12. /**
  13. * Create a new message instance.
  14. *
  15. * @return void
  16. */
  17. public $shipment, $attachment, $storagePath;
  18. public function __construct($shipment, $attachment, $storagePath)
  19. {
  20. $this->shipment = $shipment;
  21. $this->attachment = $attachment;
  22. $this->attachmentFile = '/public'.$storagePath;
  23. $this->proNumber = $shipment->pro_number;
  24. }
  25. /**
  26. * Build the message.
  27. *
  28. * @return $this
  29. */
  30. public function build()
  31. {
  32. return $this->from('billing@cmxtrucking.com')
  33. ->cc('billing@cmxtrucking.com')
  34. ->subject('New Attachment(s) - '. $this->proNumber)
  35. ->view('emails.shipments.shipmentAttachments',['shipment'=> $this->shipment])
  36. ->attach($this->attachmentFile);
  37. }
  38. }

 

And here is my controller that leads to the mailable:


      
  1. public function attachmentsEmail(Request $request){
  2. $shipment = Shipment::findOrFail($request->shipmentID);
  3. $attachment = Shipment_Attachment::findOrFail($request->attachmentID);
  4. $storagePath = Storage::url($attachment->attachmentPath);
  5. $email = $request->email;
  6. Mail::to($email)->send(new shipmentAttachments($shipment, $attachment, $storagePath)); //maybe try to use queue instead of send...
  7. return back();
  8. }

So I'm not sure where this could be coming from.

解决方法:

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:


       
  1. $app->bind('path.public', function() {
  2. return __DIR__;
  3. });

Make some tests.


       
  1. dd(public_path());
  2. dd(public_path() . '/' . $storagePath);

Or maybe verify if file exist with FileSystem class.

Hope this help you!

 

 

邮件中的图片问题:

方法一:
 <img src="{{ $message->embed(public_path().$user->avatar) }}">

   

 方法二:

<img src="http://blog.com/{{$user->avatar}}">

   

 

 

文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/lxw1844912514/article/details/100028145

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。