如何使用Laravel发送带有PDF的电子邮件

时间:2019-10-01 17:16:11

标签: php laravel laravel-5

我有一个用base 64编码的PDF,这是我数据库中的一个属性,我通过Laravel发送电子邮件,但是我不知道如何以PDF格式发送base64。

public function toMail()
    {
      $pdf_decoded = base64_decode($this->table->raw_label);
      
      $message = (new MailMessage)
                  ->subject(env('APP_NAME').' - HI #'. $this->order->id)
                  ->greeting('¡Hi!');
      
      if(env('APP_URL') == 'http://test.test'){
        $message->bcc(['test@test.com']);
      }

      return $message;
    }

我知道attach属性,但是我不知道实现它。

1 个答案:

答案 0 :(得分:1)

您实际上可以通过MailNotification类来完成此操作,就我个人而言,我会使用Notification,但这取决于您。只需使用->attach($pathToFile)方法并将其文件路径作为参数即可。

以下是使用Notification的示例,希望对您有所帮助!

/**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('Please download the PDF.')
                    ->attach(public_path($this->filename), [
                        'as' => 'filename.pdf',
                        'mime' => 'text/pdf',
                    ]);
    }