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


相关推荐

  • 2021-09-27 网安实验-取证分析-数字取证之foremost

    2021-09-27 网安实验-取证分析-数字取证之foremostForemost的使用关于foremostForemost是基于文件开始格式,文件结束标志和内部数据结构进行恢复文件的程序Foremost参数说明$foremost[-v|-V|-h|-T|-Q|-q|-a|-w-d][-t][-s][-k][-b][-c][-o][-i<file]-V-显示版权信息并退出-t-指定文件类型.(-tjpeg,pdf…)-d-打开间接块检测(针对UNIX文件系统)-i-指定输入文件(默认为标准输

    2022年10月24日
  • java Calendar 中设置时间为0点 0 分 0 秒

    java Calendar 中设置时间为0点 0 分 0 秒publicstaticvoidmain(String[]args){ Calendarcalendar=Calendar.getInstance(); //时 calendar.set(Calendar.HOUR_OF_DAY,0); //分 calendar.set(Calendar.MINUTE,0); //秒 calend…

    2022年5月29日
  • Jenkins(8)构建触发器之定时构建和轮询 SCM

    Jenkins(8)构建触发器之定时构建和轮询 SCM前言跑自动化用例每次用手工点击jenkins出发自动化用例太麻烦了,我们希望能每天固定时间跑,这样就不用管了,坐等收测试报告结果就行。jenkins的定时任务是用的crontab语法定时构建语法

    2022年7月28日
  • 金士顿u盘修复工具怎么用_用diskgenius修复u盘

    金士顿u盘修复工具怎么用_用diskgenius修复u盘重新启动电脑后再插入读卡器试试左栏将列出电脑曾连接的所有网络名称对了,量产工具里有个CDROMSIZE,哪个是干嘛的?怎么填?u盘视频怎么在手机上播放不了怎么办到此就启动成功了,adatau盘量产,其实也不难,只是苹果电脑可能对某些启动盘有些挑剔,大家选择合适的准没问题。云顶攻略在WinPE系统的桌面上,鼠标右击我的电脑”—>管理”—>磁盘管理”,可以看到WinPE系笔记本u盘安装…

    2022年9月9日
  • 网站被恶意刷流量解决方案

    网站被恶意刷流量解决方案很多站长朋友可能会经常遇到被同行竞争对手恶意刷流量的情况,而且流量ip来路是随机的,全国各地乃至全世界的ip都有,根本没办法查出来是谁干的。一般出现这种情况都是对方用流量宝或者流量精灵来刷你网站的,目的很明显,对方要么就是用这些垃圾流量来掩盖自己的ip,从而达到攻击入侵等不可告人的目的,要么就是想用恶意刷流量的方式让你合作的广告联盟帐号被封禁。大部分站长都会对此束手无策,有些甚至被吓得撤下广告,关…

    2022年9月29日
  • 亚马逊云基础架构:一场从未停歇的技术创新革命[通俗易懂]

    亚马逊云基础架构:一场从未停歇的技术创新革命[通俗易懂]2022年4月20日,线上等你!

    2022年7月20日

发表回复

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

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