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


相关推荐

  • FileSystemWatcher触发多次Change事件的解决办法

    FileSystemWatcher触发多次Change事件的解决办法FileSystemWatcher监视文件夹中数据变化时,会多次出发Change事件。EnableRaisingEvents=true,启动文件监控,网上查到的做法是通过一个定时器来避免多次触发。其实可以通过,在第一次触发后EnableRaisingEvents=false先关闭在打开EnableRaisingEvents=true,简单解决这个问题。

    2022年6月18日
    27
  • ie11兼容性视图设置怎么能自动兼容_ie11兼容模式ie8

    ie11兼容性视图设置怎么能自动兼容_ie11兼容模式ie8ie11浏览器不兼容的解决办法Edge浏览器已然成为最新win10系统的默认浏览器,但是用户量却远远不及IE11,IE11虽然性能得到了大的改进,但在浏览网页的时候还是会出现一些兼容性的问题,下面小编就讲为大家分享IE11浏览器网页不兼容的四个有效解决方法。方法一、添加受信任的站点1、打开IE11浏览器,点击浏览器右上角的“工具”选项,再选择“Internet选项”;2、点击界面的上方的“安全…

    2025年10月2日
    6
  • 在工厂做IT的职业前途[通俗易懂]

    在工厂做IT的职业前途[通俗易懂]从毕业到现在大部分时间是在制造工厂渡过.大学读的是信息管理,什么都学,什么都不懂.所以刚毕业时候工作很难找.几经周折,终于进了厦门的一家制造工厂的MIS部门做开发ERP的Coder.工厂规模虽不是很大但IT部门的学习氛围还可以,…

    2025年10月31日
    5
  • NoSuchMethodException解决「建议收藏」

    NoSuchMethodException解决「建议收藏」java.lang.NoSuchMethodException:com.test.action.test.findfinish()解决办法:1.检查action层是否存在该方法2.检查该方法是否为public3.检查此方法是否传了参数,[java]viewplaincopypublicStringtest(longid){return

    2022年6月16日
    40
  • Axure导航二级菜单[通俗易懂]

    Axure导航二级菜单[通俗易懂]    效果:当鼠标移入或者单击“权限管理”时,“权限管理”填充色由蓝变为白,字体由白变成黑。同时,弹出两个子选项“账号管理”和“设备管理”,当鼠标移入子选项时,该子选项背景变为灰白色。当鼠标移出母选项和子选项时,弹框隐藏,同时母选项背景和字体颜色恢复原状。原型设计:(1)设置元件的选中状态(2)设置鼠标移入或移出该组件的事件为了更好设计逻辑,我们采用触发的方式,新…

    2022年5月18日
    42
  • TLSF算法1:二级索引的计算

    TLSF算法1:二级索引的计算TLSF算法(一)分配中的位图计算一、什么是TLSF算法二,f的确定三、s的确定四、实验结果一、什么是TLSF算法在嵌入式系统中,内存需要在分配和释放时有一个确定的相应时间,才能进一步分析其实时任务的可调度性。因此TLSF算法是一个十分适用嵌入式领域的动态内存分配算法。在关于TLSf算法的经典文章中《TLSF:aNewDynamicMemoryAllocatorforReal-TimeSystems》详细介绍了TLSF算法相关知识。TLSF算法使用隔离匹配机制来实现良好匹配策略。基本的

    2022年6月17日
    52

发表回复

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

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