前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题

laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题

作者头像
全栈程序员站长
发布2021-10-25 10:12:31
7240
发布2021-10-25 10:12:31
举报
文章被收录于专栏:全栈程序员必看

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:

代码语言:javascript
复制
"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:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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’.

代码语言:javascript
复制
代码语言:javascript
复制
$this->attachmentFile = public_path() . '/' . $storagePath; 亲测可行

Maybe you need to change this variable in public/index.php. I have right below the require bootstrap:

代码语言:javascript
复制
$app->bind('path.public', function() {
    return __DIR__;
});

Make some tests.

代码语言:javascript
复制
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,报错文件路径问题
laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题
laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题

邮件中的图片问题:

代码语言:javascript
复制
方法一:
代码语言:javascript
复制
 <img src="{
    { $message->embed(public_path().$user->avatar) }}">

方法二:

代码语言:javascript
复制
<img src="http://blog.com/{
    {$user->avatar}}">
laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题
laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/113076.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • “Unable to open file for reading” (Swift_IoException) in Laravel Mailable
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档