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


相关推荐

  • java -jar启动时设定springboot日志输出级别

    java -jar启动时设定springboot日志输出级别

    2021年5月13日
    193
  • React教程(详细版)

    React教程(详细版)一、简介(背景)1.1、概念它是一个将数据渲染为HTML视图的js库1.2、原生js痛点用dom的API去操作dom,繁琐且效率低用js直接操作dom,浏览器会进行大量的回流和重绘原生jjs没有组件化的编程方案,代码复用性低,哪怕有模块话的概念,但模块化也只能拆解一个个js,对样式和结构也没办法拆解,组件化就相当于3剑客整体拆解,成为一个个的小功能1.3、React特点采用组件化模式,声明式编码,提高开发效率和组件复用性在ReactNative中可以用react预发进行安卓、io

    2022年7月16日
    15
  • MQTT学习笔记(6)搭建本地MQTT服务器

    MQTT学习笔记(6)搭建本地MQTT服务器搭建EMQTT下载地址下载emqttd-windows10-v2.3.11.zip解压后cd到bin目录,执行emqttdconsole执行成功会弹出下面窗口,不成功就关掉cmd重新试下打开浏览器输入http://127.0.0.1:18083默认用户名admin默认密码public进入如下界面框起来的点进去看看就知道了。注意如果你用w…

    2022年5月28日
    31
  • win7 64位官方旗舰版上搭建ruby on rails的步骤

    win7 64位官方旗舰版上搭建ruby on rails的步骤今天在win7上安装ruby成功,步骤记录下来了,分享给朋友们。亲测,可行。———-第一步:安装ruby————1.安装rubyinstaller-2.2.4-x64.exe,记得勾选addpath…选项,安装完之后ruby-v查看版本号,比如ruby2.2.4p230(2015-12-16revision53155)[x64-mi

    2022年6月5日
    28
  • linux系统查看网卡命令_linux如何配置网卡

    linux系统查看网卡命令_linux如何配置网卡rhel内核版本号信息:[root@hvrhub~]#uname-aLinuxhvrhub2.6.18-308.el5#1SMPFriJan2717:17:51EST2012x86_64x86_64x86_64GNU/Linux查看网卡的驱动。制造商等信息:[root@hvrhub~]#kudzu–probe–class=network-class:…

    2022年10月19日
    0
  • IDEA全局搜索汉字[通俗易懂]

    IDEA全局搜索汉字[通俗易懂]IDEA全局搜索指定汉字的方法,我还没找到,但有个间接的方法是:快捷键ctrl+shift+f,通过正则表达式找到所有汉字,之后再查找要找的汉字:正则表达式:[\u4e00-\u9fa5]

    2022年6月22日
    202

发表回复

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

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