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,报错文件路径问题

 

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • java方法中的构造方法与普通方法的区别[通俗易懂]

    “java”方法中的构造方法与普通方法的区别是:一、构造方法的名字必须与定义它的类名完全相同;二、构造方法在初始化对象时自动执行;三、构造方法可以被重载;四、构造方法不能被“static”等关键字修饰。

  • struts2的拦截器AbstractInterceptor「建议收藏」

    struts2的拦截器AbstractInterceptor「建议收藏」publicclassAuthorizationInterceptorextendsAbstractInterceptor{@Override   publicStringintercept(ActionInvocationai)throwsException{      Map&lt;?,?&gt;session=ai.getInvocationConte…

  • Faster-RCNN算法精读[通俗易懂]

    Faster-RCNN算法精读[通俗易懂]读论文:《FasterR-CNN:TowardsReal-TimeObjectDetectionwithRegionProposalNetworks》摘要:算法主要解决两个问题:1、提出区域建议网络RPN,快速生成候选区域;2、通过交替训练,使RPN和Fast-RCNN网络共享参数。一、RPN网络结构RPN网络的作用是输入一张图像,输出一批矩形候选区域

  • Unity零基础到入门 ☀️| 万字教程 对 Unity 中的 Navigation导航系统基础 全面解析+实战演练【收藏不迷路】[通俗易懂]

    Unity零基础到入门 ☀️| 万字教程 对 Unity 中的 Navigation导航系统基础 全面解析+实战演练【收藏不迷路】[通俗易懂]导航系统。导航系统,顾名思义,就是游戏中的一个寻路功能。本文对Unity中的导航Navigation系统做了一个详细的说明,包括案例和效果展示!请品尝!

  • LNK2001:无法解析外部符号_sprintf

    LNK2001:无法解析外部符号_sprintfLNK2001:无法解析外部符号_sprintf与LNK1104:无法打开文件“kernel32.lib”_臻訾胃的博客-CSDN博客从网上下载一个VS的demo,编译之后老是报LNK2001:无法解析外部符号_sprintf的错误,找了好久,发现在项目属性->链接器->输入->附加目录依赖项添加legacy_stdio_definitions.lib即可解决问题。但不知道legacy_stdio_definitions.lib这玩意儿干啥的,有兴趣的自己百度吧..

  • 【AngularJS】 # AngularJS入门

    【AngularJS】 # AngularJS入门1.AngularJS简介AngularJS是一个JavaScript框架,用js编写的库<scriptsrc=”https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js”></script><!–放在<body>元素的底部。提高网页加载速度–>1.1.AngularJS扩展了HTMLAngularJS通过ng-directives扩展了HTMLng-app指令

发表回复

您的电子邮箱地址不会被公开。

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