laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题
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}}">
laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题的更多相关文章
- PHP 文件上传注意一个地方,移动文件时要保证目标目录存在,否则报错
move_uploaded_file ( $_FILES ["file"] ["tmp_name"], "upload/" . $fileN ...
- 错误笔记 对象为null时调用改对象的方法会报错
对象为null时调用改对象的方法会报错
- 启动多个eclipse 时,因为一个另一个启动报错,
启动多个eclipse 时,因为一个另一个启动报错, 原因: 可能是 有一个 eclipse 中 的 tomcat 配置出错:preference中 tomcat 配置 context dec ...
- 在使用python语言的open函数时,提示错误OSError: [Errno 22] Invalid argument: ‘文件路径’
如题,在使用python语言的open函数时,提示错误OSError: [Errno 22] Invalid argument: '文件路径',在查阅了大量资料后也得到了一些解决方案,但是这些解决方案 ...
- Linux安装软件时90%的人会遇到这个报错,如何解决?
提示 Could not get lock /var/lib/dpkg/lock 报错? 有些小伙伴在使用 apt 包管理器更新或安装软件时,可能会遇到过诸如以下的错误提示: E: Could not ...
- react className 有多个值时的处理 / react 样式使用 百分比(%) 报错
1.react className 有多个值时的处理 <fieldset className={`${styles.formFieldset} ${styles.formItem}`}> ...
- docker-compose exec时 出现"fork/exec /proc/self/exe: no such file or directory" 报错
问题:跟往常一样执行docker-compos exec redis sh时出现如下错误,而容器是运行状态中. # docker-compose exec redis sh rpc error: co ...
- 使用Maven构建Java Web项目时,关于jsp中引入js、css文件路径问题。
今天有点闲,自己动手搭建一个Java Web项目,遇到jsp中引入js.css文件时路径不正确的问题,于是在网上查阅了很多资料,最终都无法解决问题,于是,上stackoverflow找到了解决方法,这 ...
- spring boot集成shiro-redis时,分布式根据seesionId获取session报错排查总结
昨天在集成shiro-redis的时候,使用sessionId在其他微服务获取用户的session时,发生错误:There is no session with id [xxx]. 查遍了所有资料,基 ...
随机推荐
- 2018-8-10-如何移动-nuget-缓存文件夹
title author date CreateTime categories 如何移动 nuget 缓存文件夹 lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...
- 2016计蒜之道复赛A 百度地图的实时路况
百度地图的实时路况功能相当强大,能方便出行的人们避开拥堵路段.一个地区的交通便捷程度就决定了该地区的拥堵情况.假设一个地区有 nnn 个观测点,编号从 111 到 nnn.定义 d(u,v,w)d(u ...
- ant详细介绍
Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序时,经常要用这个命令.Make命令 ...
- ubuntu上源码编译安装mysql5.7.27
一.查看操作系统环境和目录结构,并创建mysql用户和组,以及规划安装mysql所需要的目录. #cat /etc/issue 查看发行版本信息: #cat /proc/version 查看正在运行 ...
- 精密MRAM芯片制造系统
MRAM是一种非常复杂的薄膜多层堆叠,由10多种不同材料和超过30层以上的薄膜与堆叠组成,部分薄膜层的厚度仅达数埃,比人类的发丝还要薄500000倍,相近于一颗原子的大小,如何控制这些薄膜层的厚度.沉 ...
- Spring Boot 数据库连接池参数
挑战A.I.,赢百万奖金......了解更多详情>>> Tomcat JDBC 连接池 Spring Boot 默认选择 Tomcat JDBC Pool 作为数据库连接池.Tomc ...
- Bigdecimal 相加结果为0的解决
之前很少使用这样的一个对象BigDecimal,今天在改需求的时候遇到了,结果坑爹的怎么相加最后都为零. 代码如下: BigDecimal totalAmount = new BigDecimal(0 ...
- 前端与编译原理——用JS写一个JS解释器
说起编译原理,印象往往只停留在本科时那些枯燥的课程和晦涩的概念.作为前端开发者,编译原理似乎离我们很远,对它的理解很可能仅仅局限于"抽象语法树(AST)".但这仅仅是个开头而已.编 ...
- SQL注入绕过的技巧总结
sql注入在很早很早以前是很常见的一个漏洞.后来随着安全水平的提高,sql注入已经很少能够看到了.但是就在今天,还有很多网站带着sql注入漏洞在运行.稍微有点安全意识的朋友就应该懂得要做一下sql注入 ...
- 利用Eclipse进行远程Debug
这项功能真的十分赞,当我不想写junit test,又想调试在实际环境中才能起作用的Java程序,远程debug真的是太好用了. 参数:java -jar -Xdebug -Xrunjdwp:tran ...