Laravel邮件附件订单

时间:2020-06-10 10:54:45

标签: php laravel email laravel-6 laravel-mail

我正在向电子邮件发送多个附件。它们来自存储磁盘以及远程源。我想先显示磁盘文件,然后再显示远程文件。

这是我的build()方法:

public function build()
{

   // Attaching Disk File First
   $this->attachFromStorageDisk('public', '/path/to/myfiel.pdf');

   // Attaching Remote Files 
   foreach ($this->files as $file) {
      $this->attach($file);
   }

   return $this->view('email.orders.shipped');
}

我优先使用buildAttachments方法来生成存储磁盘文件:

此处覆盖了buildAttachments方法:

protected function buildAttachments( $message )
{
    $this->buildDiskAttachments( $message ); // <-- Brought this one top to build disk attachment first

    foreach ( $this->attachments as $attachment ) {
        $message->attach( $attachment['file'], $attachment['options'] );
    }

    foreach ( $this->rawAttachments as $attachment ) {
        $message->attachData(
            $attachment['data'], $attachment['name'], $attachment['options']
        );
    }

    return $this;
}

我的磁盘文件是PDF。远程文件可以混合使用pdf/png/jpg/jpeg。当所有远程文件均为pdf时,附件顺序工作正常。 但是,如果有image file(jpg/jpeg/png),则总是优先。

这是我完整的Mailable课程:

class OrderShipped extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    protected $files;

    public function __construct($files)
    {
        $this->files    = $files;
    }

    public function build()
    {

        // Attaching Disk File First
        $this->attachFromStorageDisk( 'public', '/path/to/myfiel.pdf' );

        // Attaching Remote Files 
        foreach ( $this->files as $file ) {
            $this->attach( $file );
        }

        return $this->view( 'email.orders.shipped' );
    }

    /**
     * Add all of the attachments to the message.
     *
     * @param \Illuminate\Mail\Message $message
     *
     * @return $this
     */
    protected function buildAttachments( $message )
    {
        $this->buildDiskAttachments( $message ); // <-- Brought this one top to build disk attachment first

        foreach ( $this->attachments as $attachment ) {
            $message->attach( $attachment['file'], $attachment['options'] );
        }

        foreach ( $this->rawAttachments as $attachment ) {
            $message->attachData(
                $attachment['data'], $attachment['name'], $attachment['options']
            );
        }

        return $this;
    }
}

现在,无论我附加的远程文件类型如何,我总是希望PDF File中的Storage Disk显示为FIRST。你能帮忙吗?

Laravel版本:6.18.16

0 个答案:

没有答案