【问题标题】:Laravel/PHP error: Undefined variable: email2 [duplicate]Laravel/PHP 错误:未定义变量:email2 [重复]
【发布时间】:2019-07-27 14:57:42
【问题描述】:

我试图发送一封电子邮件,当我想写入目的地的电子邮件地址(电子邮件是函数的参数)时,我遇到了这个错误。谁能帮帮我。

这是我的功能:

public function email($name, $email2) {
  $data = array('name'=> $name);
  Mail::send('mail',$data,function($message) {
    $message->to($email2, $name)->subject('Deposit Confirmation');
    $message->attach(public_path('document.pdf'));
    $message->from('xxx@xxx.pt', 'XXX');
  });
}

这是我的错误:“未定义的变量 email2”

谢谢

【问题讨论】:

    标签: php laravel email variables


    【解决方案1】:

    当你使用匿名函数传递变量时,你应该使用use 构造

    public function email($name, $email2) {
          $data = array('name'=> $name);
          Mail::send('mail',$data,function($message) use ($name, $email2) {
            $message->to($email2, $name)->subject('Deposit Confirmation');
            $message->attach(public_path('document.pdf'));
            $message->from('xxx@xxx.pt', 'XXX');
          });
        }
    

    这里你可以找到更多关于匿名函数的例子https://www.php.net/manual/en/functions.anonymous.php

    这里是来自官方文档的简单示例

    $message = "hello";
    
    // Inherit $message
    $example = function () use ($message) {
        var_dump($message);
    };
    
    $example();
    
    // Output: string(5) "hello"
    

    【讨论】:

    • 发生在哪一行代码中?
    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 2022-12-31
    • 2014-10-09
    • 2020-11-30
    相关资源
    最近更新 更多