zoukankan      html  css  js  c++  java
  • 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 AppMail;
    
    use IlluminateBusQueueable;
    use IlluminateMailMailable;
    use IlluminateQueueSerializesModels;
    use IlluminateContractsQueueShouldQueue;
    
    use AppShipment;
    use AppShipment_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!

    邮件中的图片问题:

    方法一:
     <img src="{{ $message->embed(public_path().$user->avatar) }}">
    

     方法二:

    <img src="http://blog.com/{{$user->avatar}}">
    

     

  • 相关阅读:
    android videoView 加载等待
    LocalBroadcastManager
    sessionStorage 、localStorage
    javascript 数组、json连接
    properties 文件注意事项
    nutz 使用beetl
    [Git/Github] ubuntu 14.0 下github 配置
    【UNIX环境编程、操作系统】孤儿进程和僵尸进程
    【操作系统】进程间通信
    【操作系统】线程
  • 原文地址:https://www.cnblogs.com/lxwphp/p/9364948.html
Copyright © 2011-2022 走看看