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中利用DatagramPacket与DatagramSocket进行通讯的示例

    Java中利用DatagramPacket与DatagramSocket进行通讯的示例对以下demo进行了扩展,增了消息循环和等待。 Java中的DatagramPacket与DatagramSocket的初步扩展的代码如下:1.接收端工程代码:由于接收端的控制台log会被发送端的log冲掉,所以把log写到文件中。packagecom.ameyume.receiver;importjava.io.File;importjava.io.FileNotFoundExcep

    2022年5月4日
    41
  • JVM之内存结构详解

    JVM之内存结构详解对于开发人员来说,如果不了解Java的JVM,那真的是很难写得一手好代码,很难查得一手好bug。同时,JVM也是面试环节的中重灾区。今天开始,《JVM详解》系列开启,带大家深入了解JVM相关知识。我们不能为了面试而面试,但是学习会这些核心知识你必定会成为面试与工作中“最亮的一颗星”。本系列首发于微信公众号“程序新视界”。下面,开启我们的第一篇文章《JVM之内存结构详解》。学习也是要讲究方式方法…

    2022年6月11日
    24
  • 非常详细的rsyslogd & logrotate配置文件解析[通俗易懂]

    非常详细的rsyslogd & logrotate配置文件解析[通俗易懂]rsyslog服务和logrotate服务======================================================================rsyslog是一个syslogd的多线程增强版。现在Fedora和Ubuntu,rhel6默认的日志系统都是rsyslog了rsyslog服务和logrotate服务=====

    2022年8月15日
    3
  • flow control

    flow controlPCIE每个VirtualChannel都维护一个独立的FlowControlCreditPool。发送端要发送TLP,首先得获得Credit。FlowControl对3种TLP有效:1.PostedRequest(P)-Messages和MemoryWrites;2.Non-PostedRequest(NP)-所有的Reads,I/Owrites,Con…

    2022年5月3日
    61
  • 漫画网站服务器,建立家庭漫画服务器,从iPad上看漫画

    漫画网站服务器,建立家庭漫画服务器,从iPad上看漫画好,故事开始。以前就在想,在retina屏幕的iPad上看漫画该多爽。可是现在捧着iPad却发现看漫画很困难,自己电脑上下载了一堆漫画都是jpg图片,导入iBook也很麻烦。现在通过家里的iMac建立一个家庭漫画服务器,直接在线浏览速度快、体验好,而且还很方便。以后下载了漫画往目录里一扔,就可以拿起iPad看了。需要工具:MAMP是Mac下出名的Apache-MySQL-PHP服务器套件。…

    2022年6月17日
    24
  • Java中的锁

    Java中的锁在学习或者使用Java的过程中进程会遇到各种各样的锁的概念:公平锁、非公平锁、自旋锁、可重入锁、偏向锁、轻量级锁、重量级锁、读写锁、互斥锁等待。这里整理了Java中的各种锁,若有不足之处希望大家在下方留言探讨。WARNING:本文适合有一定JAVA基础的同学阅读。公平锁和非公平锁公平锁是指多个线程在等待同一个锁时,必须按照申请锁的先后顺序来一次获得锁。公平锁的好处是等待锁的线程…

    2022年7月18日
    13

发表回复

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

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