使用laravel发送电子邮件,但无法识别变量 [英] Sending email with laravel, but doesn't recognize variable

查看:86
本文介绍了使用laravel发送电子邮件,但无法识别变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

未定义的变量:contactEmail

Undefined variable: contactEmail

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

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

控制器

$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('info@aallouch.com', 'myName')->subject('Mail via aallouch.com');
});

template.mail

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

推荐答案

由于$ data变量定义为:

As your $data variable is defined as:

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

视图中将没有$ data可用,但可以直接使用:

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

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

您的控制器应具有:

    $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('info@aallouch.com', 'myName')->subject('Mail via aallouch.com');
    });

您必须使用将变量传递给闭包

You must pass your variables to the closure using

use ($contactEmail, $contactName)

如上所示.

这篇关于使用laravel发送电子邮件,但无法识别变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆