为什么在 Laravel 中发送电子邮件附件时给出未定义的变量?

Why giving undefined variable in sending Email attachment in Laravel?

我无法将任何变量从 Controller 传递到电子邮件正文。我搜索了很多解决方案,但没有一个与我匹配。

我正在使用 Mailable。但它是空的。我不确定它是否会造成问题。

public function __construct(){}

public function build(){}

我已经在不传递变量的情况下使用虚拟电子邮件进行了测试。电子邮件发送成功。所以,我觉得配置没有问题。

控制器中的函数:

public function mail()
{
    $info = Invoice::find(65)->first();
    // 65 is giving me value. I have checked with dd()
    $data = [
        'invoice' => $info->invoiceNumber
    ];

    Mail::send(['text'=>'invoice.test'], $data, function($message) use($data){
    $pdf = PDF::loadView('invoice.test');
    $message->to('example@gmail.com','John Smith')->subject('Send Mail from Laravel');
    $message->from('from@gmail.com','The Sender');
    $message->attachData($pdf->output(), 'Invoice.pdf');
   });

   return 'Email was sent';
}

test.blade.php

<h1>It is working!! {{ $invoice }}</h1>

数据发送样式遵循: Laravel Mail::send how to pass data to mail View

尝试在 PDF 视图中添加 $data:

$pdf = PDF::loadView('invoice.test', $data);

但是如果你想添加一个与PDF内容不同的邮件正文内容,试试这个:

Mail::send([], $data, function($message) use($data){
   $pdf = PDF::loadView('invoice.test', $data);
   $message->to('example@gmail.com','John Smith')->subject('Send Mail from Laravel');
   $message->from('from@gmail.com','The Sender');
   $message->setBody('sample mail content');
   $message->attachData($pdf->output(), 'Invoice.pdf');
});