php 使用 Laravel 发送电子邮件,但无法识别变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19412055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:25:54  来源:igfitidea点击:

Sending email with laravel, but doesn't recognize variable

phpemaillaravellaravel-4undefined

提问by mXX

I'm trying to send an email through Laravel, but I'm getting this error:

我正在尝试通过 Laravel 发送电子邮件,但出现此错误:

Undefined variable: contactEmail

未定义变量:contactEmail

Even though it got defined above it. What is going wrong here?

即使它被定义在它上面。这里出了什么问题?

Controller

控制器

$contactName = Input::get('name');
$contactEmail = Input::get('email');
$contactMessage = Input::get('message');

$data = array('name'=>$contactName, 'email'=>$contactEmail, 'message'=>$contactMessage);
Mail::send('template.mail', $data, function($message)
{   
    $message->from($contactEmail, $contactName);
    $message->to('[email protected]', 'myName')->subject('Mail via aallouch.com');
});

EDIT:

编辑:

template.mail

模板.mail

Name: {{$name}}
Email: {{$email}}
Message:{{$message}}

回答by Antonio Carlos Ribeiro

As your $data variable is defined as:

由于您的 $data 变量定义为:

$data = array(
    'name'=>$contactName, 
    'email'=>$contactEmail, 
    'message'=>$contactMessage
);

You won't have a $data available in your view, but you can use directly:

您的视图中不会有 $data 可用,但您可以直接使用:

{{ $name }}
{{ $email }}
{{ $message }}

EDIT:

编辑:

And your controller should have:

你的控制器应该有:

    $contactName = Input::get('name');
    $contactEmail = Input::get('email');
    $contactMessage = Input::get('message');

    $data = array('name'=>$contactName, 'email'=>$contactEmail, 'message'=>$contactMessage);
    Mail::send('template.mail', $data, function($message) use ($contactEmail, $contactName)
    {   
        $message->from($contactEmail, $contactName);
        $message->to('[email protected]', 'myName')->subject('Mail via aallouch.com');
    });

You must pass your variables to the closure using

你必须使用你的变量传递给闭包

use ($contactEmail, $contactName)

As shown above.

如上图所示。

回答by Vikas Burman

I have got this error and I have solved it. I have replace $messagekeyword with $commentfrom $data.

我遇到了这个错误,我已经解决了。我已经用$data替换了$message关键字$comment

Example below :

下面的例子:

$data = array('name' => 'vikas', 'message' => 'test message');

view

看法

{{ $name }}
{{ $message }}

It's getting error

出错了

$data = array('name' => 'vikas', 'comment' => 'test message');

{{ $name }}
{{ $comment }}

Now it's working fine.

现在它工作正常。

回答by mixel

If you look at source codeyou'll see this line:

如果您查看源代码,您会看到这一行:

 $data['message'] = $message = $this->createMessage();

So your messagefield is overwritten by that line. Use other name for the field like textor comment.

因此,您的message字段被该行覆盖。为字段使用其他名称,如textcomment

回答by plushyObject

I came here to say that if you get this issue in Laravel 5.* or 6.*, make sure that you have your variables set to publicwithin your Mailable class. By default, PHPStorm will make your properties privateand they won't be accessible within the email blade template.

我来这里是想说,如果您在 Laravel 5.* 或 6.* 中遇到此问题,请确保您public在 Mailable 类中设置了变量。默认情况下,PHPStorm 将创建您的属性,private并且无法在电子邮件刀片模板中访问它们。