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


相关推荐

  • BN层pytorch实现[通俗易懂]

    BN层pytorch实现[通俗易懂]#CreatedbyXkyat2019/11/29importtimeimporttorchimporttorchvisionimporttorch.nnasnnimportsysimporttorchvision.transformsastransformsfromtorch.utils.data.dataloaderimportDataLoad…

    2022年10月14日
    3
  • ssdp协议 upnp_SSDP,简单服务发现协议

    ssdp协议 upnp_SSDP,简单服务发现协议SSDP简单服务发现协议,是应用层协议,是构成UPnP(通用即插即用)技术的核心协议之一。它为网络客户端(networkclient)提供了一种发现网络服务(networkservices)的机制,采用基于通知和发现路由的多播方式实现。SSDP多播地址:239.255.255.250:1900(IPv4),FF0x::C(IPv6)两种类型的SSDP请求消息会通过SSDP多播地址发送:1….

    2022年10月11日
    7
  • LoadRunner教程(12)-LoadRunner IP欺骗[通俗易懂]

    LoadRunner教程(12)-LoadRunner IP欺骗[通俗易懂]loadrunner不支持自动ip,所以要设置成静态ip打开IPWizard这一步不用管,直接next然后点击完成然后添加IP

    2022年5月23日
    60
  • W3C标准是_关于w3c标准下列说法错误的是

    W3C标准是_关于w3c标准下列说法错误的是W3C简介什么是W3C?万维网联盟(WorldWideWebConsortium)创建于1994年10月工作是对web进行标准化创建并维护WWW标准使用W3C的目的?为了让任何人在任何时间、任何地点、通过任何设备访问web内容。作为用户和浏览器制造商之间的中间层,要想办法满足下游的用户和上游不同浏览器看到的是同样的内容,让页面、程序能支持所有的浏览器,能够满足尽可能多的用户标准内容1、DOC…

    2025年12月13日
    4
  • Typora ——一款Markdown编辑器入门教程

    Typora ——一款Markdown编辑器入门教程Typora——Markdown编辑器入门教程附上目录:文章目录Typora——Markdown编辑器入门教程一.序二.前言那么,什么是富文本格式?什么是Markdown?**Markdown编辑器和常见的富文本编辑器有什么区别?**二.Typora简介什么是Typora?当然,typora的魅力不止于此:三.Typora的安装四.配合扩展体验更佳1.配合Pandoc扩展程序实现导出2…

    2022年5月18日
    48
  • struts 2 漏洞学习总结

    struts 2 漏洞学习总结struts2最近几个漏洞分析&稳定利用payloadhttp://drops.wooyun.org/papers/9020×00背景看到网上关于struts2利用的文章非常多,但是对于漏洞触发跟踪分析的文档比较少,闲来无事跟踪了一下struts最近吵得比较火的两个漏洞,研究了一下能够稳定利用的payload。0x01S2-008

    2022年7月19日
    19

发表回复

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

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