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:

"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.

解决方法:

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!

邮件中的图片问题:

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

方法二:

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

最新文章

  1. 使用NSJSONSerialization将数组或字典转为字符串
  2. [Java]Hessian客户端和服务端代码例子
  3. ELF Format 笔记(八)—— 符号的类型和属性(st_info)
  4. IBM AppScan 安全扫描:加密会话(SSL)Cookie 中缺少 Secure 属性 处理办法 分类: 数据安全 2014-06-28 11:35 2805人阅读 评论(0) 收藏
  5. Oracle连接的若干错误
  6. Django 学习笔记之七 实现分页
  7. 关于Eclipse插件开发(一)
  8. 无法将类型为excel.applicationclass的com 强制转换为接口类型的解决方法[转]
  9. jQuery防京东浮动网站楼层导航代码
  10. 【题解】放球游戏A
  11. 状态压缩动态规划 状压DP
  12. HTML语法介绍
  13. linux中结构体对齐【转】
  14. 如何用prometheus监控k8s集群中业务pod的metrics
  15. linux C宏定义 转
  16. 感谢Thunder
  17. selenium之数据驱动框架应用WPS个人中心自动签到
  18. Two ways to assign values to member variables
  19. springboot项目属性配置及注意事项
  20. 什么是spark(一) 分区以及和MR的区别

热门文章

  1. python邮件发送:普通文本、html、添加附件
  2. uva 11300 分金币(利用绝对值加和进行求出最小值)
  3. Leetcode55. Jump Game跳跃游戏
  4. HttpClient实现远程调用
  5. MySQL学习-- UNION与UNION ALL
  6. Idea下面无法识别web-inf下lib目录的子目录的jar包,只能直接放lib下面才能识别?
  7. 基于docker的php调用基于docker的mysql数据库的方法
  8. Linux常用命令6 压缩解压命令
  9. PHP核心编程--站内搜索
  10. 【JZOJ3875】【NOIP2014八校联考第4场第2试10.20】星球联盟(alliance)