【问题标题】:Why giving undefined variable in sending Email attachment in Laravel?为什么在 Laravel 中发送电子邮件附件时给出未定义的变量?
【发布时间】:2019-12-10 12:49:06
【问题描述】:

我无法将任何变量从 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

【问题讨论】:

  • use($data,$pdf) 你的 $pdf 变量是未定义的,因为你没有使用它。
  • 已使用。但同样的错误。

标签: laravel laravel-mail


【解决方案1】:

尝试在 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');
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-24
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多