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

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!

 

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

 

邮件中的图片问题:

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

 方法二:

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

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/113076.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 你有没有想过,在SpringBoot集成下,Mybatis的mapper代理对象究竟是如何生成的?

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 作者:青石路 cnblogs.com/youzhibing/p/10486307.html 前情回顾 之前我们讲到了…

    2021年6月24日
    68
  • 30、转载樱花飘落代码,无需添加图片,个人觉得很漂亮,收藏一下![通俗易懂]

    30、转载樱花飘落代码,无需添加图片,个人觉得很漂亮,收藏一下![通俗易懂]<!DOCTYPEHTMLPUBLIC”-//W3C//DTDHTML4.0Transitional//EN”><HTML><HEAD><TITLE>NewDocument</TITLE><METANAME=”Generator”CONTENT=”EditPlus”><MET…

    2022年7月19日
    17
  • PHP工厂模式学习「建议收藏」

    PHP工厂模式学习「建议收藏」PHP工厂模式学习目录PHP工厂模式学习介绍引入定义参与者工厂方法模式实例工厂方法模式的优点工厂方法模式缺点适用环境小结介绍工厂方法模式,可以更好的处理客户的需求变化。引入简单工厂模式把实例化对象的工作推迟到了专门的工厂类中。但是当客户需求出现变化的时候,我们不仅得增加新的类和修改工厂类以适应客户的需求,这是设计模式所不允许的。工厂方法模式应运而生。解决思路:那里变化,封装哪里

    2022年7月25日
    10
  • git切换远程分支命令_git查看对应的远程分支

    git切换远程分支命令_git查看对应的远程分支git切换远程分支将远程git仓库里的指定分支拉取到本地(本地不存在的分支)当我想从远程仓库里拉取一条本地不存在的分支时:gitcheckout-b本地分支名origin/远程分支名例如:切换远程分支gitcheckout-breleaseorigin/release-9.4###原文默认,git项目只有一个分支,就是master,我们当然可以在本地创建多个分支,并推送到远程git管理平台上,或者将远程git管理平台上的其他..

    2022年9月21日
    3
  • 分布式计算概述_分布式计算与处理

    分布式计算概述_分布式计算与处理**分布式计算是当前计算机领域常见的名词,那么到底什么是分布式,什么又是分布式计算呢?今天和大家共同研究一下这个话题。**分布式计算的概念一个分布式系统是由若干通过网络互联的计算机组成的软硬件系统,且这些计算机互相配合以完成一个共同目标(往往这个共同目标称为“项目”)分布式计算的优缺点优点:1.超大规模2.虚拟化3.高可靠性4.通用性5.高伸缩性6.按需服务7….

    2025年8月7日
    1
  • sendfile函数–零拷贝

    sendfile函数–零拷贝零拷贝:零拷贝技术可以减少数据拷贝和共享总线操作的次数,消除通信数据在存储器之间不必要的中间拷贝过程,有效地提高通信效率,是设计高速接口通道、实现高速服务器和路由器的关键技术之一。sendfile#includessize_tsendfile(intout_fd,intin_fd,off_t*offset,size_tcount);参数特别注意

    2022年5月24日
    60

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号