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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab微分方程组_matlab求微分方程特解

    matlab微分方程组_matlab求微分方程特解主要内容:matlab参数识别应用,主要适用于微分方程、微分方程组参数识别、simulink模型参数识别,领域不限。1使用matlab识别微分方程参数以及微分方程组(多个微分方程)参数2使用matlab调用simulink并识别simulink模型的参数(m函数与simulink交互)内容为本人在学习过程中总结的知识,拿出来与大家分享,希望大家多多讨论。下边贴出一部分源码,其它完整内容在附件的…

    2022年9月6日
    3
  • 如果我说熟悉SpringBoot 面试官会怎么问?

    如果我说熟悉SpringBoot 面试官会怎么问?SpringBoot因简化了Spring框架使用难度,极大地提高了Java企业级应用开发的效率,成为企业考核人才的重要标准之一。但随着现今互联网行业快速发展、企业业务不断深入,相应地对SpringBoot技术要求也愈来愈高。春节时期有一位打算金三银四面试的读者私信问我:如果我说熟悉SpringBoot面试官会怎么问?​可能不少朋友跟他一样,不清楚当下企业真实生产环境下对SpringBoot有哪些具体要求,需要掌握到什么程度。为此,结合这些年的面试经历及各大厂的职位要求,给

    2022年6月5日
    27
  • docker 导入导出镜像_docker拉取镜像到本地

    docker 导入导出镜像_docker拉取镜像到本地docker导入导出镜像文件:把某个docker镜像保存到本地文件,命令如下dockersave-o镜像名.tar原始镜像名(REPOSITORY项)导出$dockersave-o/root/images/jenkins_image.tarjenkins/jenkins:latest导入$dockerload</root/images/jenkins_image.tar导出镜像如果要存出镜像到本地文件,可以使用dockersave命令。例如,存出本地的ubu

    2022年9月5日
    2
  • error lnk 2019_lnk2005错误

    error lnk 2019_lnk2005错误导致LNK2019的常见问题:(1)未链接的对象文件或包含符号定义的库(2)符号声明的拼写不与符号的定义相同(3)使用了函数,但类型或参数数目不匹配函数定义(4)声明但未定义的函数或变量(5) 调用约定是函数声明和函数定义之间的差异(6)符号定义在c文件中,但未使用externC在c++文件中声明(7)符号定义为静态,并随后被外部文件引用(8)未定义类的静态成员(9)生成依赖项仅定义为解决方…

    2022年10月5日
    0
  • 京东商城2020年销售额_天猫精灵怎么激活互联网

    京东商城2020年销售额_天猫精灵怎么激活互联网今日看点✦2020天猫双11全球狂欢季总成交额4982亿,超450个品牌成交额过亿✦京东11.11全球热爱季累计下单金额超2715亿,再创纪录✦百度整合好看视频和全民小视频,升级为…

    2025年7月29日
    1
  • excel 导出json_导出的数据格式不对

    excel 导出json_导出的数据格式不对json格式数据转Excel导出的两种方法第一种table格式数据直接转Excel:但是用这种方式会出现一种问题,就是当你的table有分页的情况下,只能抓取当前分页的数据。拿到表格的id就可以

    2022年8月4日
    20

发表回复

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

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