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)
blank

相关推荐

  • 数据仓库搭建ODS层[通俗易懂]

    数据仓库搭建ODS层[通俗易懂]其他内容请关注我的博客!在<项目>专栏里!!!目录一、用户行为数据1.1创建日志表1.2ODS层加载数据脚本二、业务数据2.1hive建表2.2ODS层加载数据脚本一、用户行为数据1.1创建日志表1)创建支持lzo压缩的分区表droptableifexistsods_log;CREATEEXTERNALTABLEods_log(`line`string)PARTITIONEDBY(`dt`string)–

  • 2017过去了,2018来了「建议收藏」

    2017那些过往的岁月,留给自己!

  • BZOJ2803[Poi2012]Prefixuffix——hash

    BZOJ2803[Poi2012]Prefixuffix——hash

  • mysql show status详解

    mysql show status详解

  • 10种用于渗透测试的漏洞扫描工具有哪些_渗透测试和漏洞扫描区别

    10种用于渗透测试的漏洞扫描工具有哪些_渗透测试和漏洞扫描区别漏洞扫描工具是IT部门中必不可少的工具之一,因为漏洞每天都会出现,给企业带来安全隐患。漏洞扫描工具有助于检测安全漏洞、应用程序、操作系统、硬件和网络系统。黑客在不停的寻找漏洞,并且利用它们谋取利益。网络中的漏洞需要及时识别和修复,以防止攻击者的利用。漏洞扫描程序可连续和自动扫描,可以扫描网络中是否存在潜在漏洞。帮助It部门识别互联网或任何设备上的漏洞,并手动或自动修复它。在本文中,我们将介绍市场上可用的十大最佳漏洞扫描工具。1.OpenVAS漏洞扫描工具OpenVAS漏洞扫描器是

  • Java反射机制及其使用

    Java反射机制及其使用文章目录1Java反射机制概述2反射及其使用2.1关于类java.lang.Class2.2类的加载2.2.1类的加载过程2.2.2类加载器2.3反射的使用2.3.1创建运行时类的对象2.3.2获取运行时类的属性结构2.3.3获取运行时类的方法结构2.3.4获取运行时类的构造器结构2.3.5获取运行时类的父类信息2.3.6获取运行时类实现的接口2.3.7获取运行时类声明的注解2.3.7获取运行时类所在的包2.3.8调用类的构造函数、操作类的属性、调用类中方法1Java

发表回复

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

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