【问题标题】:Laravel: Trying to send a mail, variable undefinedLaravel:尝试发送邮件,变量未定义
【发布时间】:2014-04-16 18:42:10
【问题描述】:

您好,我正在尝试发送邮件,但是当我尝试使用 Mail::Send 但我创建的变量以获取将要发送到的邮件的信息时不会进入 Mail::Send 部分。我不知道为什么它总是告诉我未定义的变量,当它在顶部定义时。这是代码

$result = DB::table('perfil')->where('usuario', $object['usuario'])->first();

        $info = array('confirmation_code' => $result->codigoVerificacion, 'nombre' => $result->nombre);

        $mail = $result->correoVerificado;

        $nombre = $result->nombre.' '.$result->primerPaterno.' '.$result->segundoApellido;

        Mail::send('emails.registro_nuevo_usuario', $info, function($message)
        {

            $message->from('jfeuchter@gmail.com', 'Jurgen Feuchter');

            $message->to($mail, $nombre)
                ->subject('¡Bienvenido a la mejor red social para pedir y dar rides!');
        });

如果您需要更多代码,请向我索取:P

\app\controllers\registroController.php:103 中的“未定义变量:邮件”

【问题讨论】:

  • 什么未定义的变量?您遗漏了最关键的信息:P Ps 删除您的电子邮件地址。您是否测试了结果的结果? \dd($result); 另外,如果您需要 $result 存在;最好使用 firstOrFail() 而不是 first()
  • 那么$message从何而来?
  • 我的错误是hehehe C\\app\\controllers\\registroController.php:103 中的“未定义变量:邮件”

标签: php laravel mail-sender


【解决方案1】:

$mail 来自closure 之外,因此它在其范围内不可用(与$message 不同,send() 方法作为参数向下传递)。您需要使用use 关键字来传递它(我相信$nombre 也会发生同样的情况)

Mail::send('emails.registro_nuevo_usuario', $info, function($message) use($mail, $nombre)
{
 $message->from('jfeuchter@gmail.com', 'Jurgen Feuchter')
          ->to($mail, $nombre)
          ->subject('¡Bienvenido a la mejor red social para pedir y dar rides!');
});

【讨论】:

  • 谢谢,这就是我要找的东西:D
猜你喜欢
  • 1970-01-01
  • 2015-02-04
  • 2014-12-01
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 2016-10-24
相关资源
最近更新 更多