Laravel 4 从联系表到管理员电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21565272/
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-09-14 08:58:47  来源:igfitidea点击:

Laravel 4 from contact form to admin email

phpemaillaravellaravel-4swiftmailer

提问by Mitch

I'm trying to use Laravel's Mail class for the first time and am having some difficulties. I have a simple contact form that should be sent as an email to the admin. Here is what I have tried

我第一次尝试使用 Laravel 的邮件类,但遇到了一些困难。我有一个简单的联系表格,应该作为电子邮件发送给管理员。这是我尝试过的

Controller(Will be refactored into Model once I can get it working)

控制器(一旦我可以让它工作,将被重构为模型)

public function store()
{
    $validation = new Services\Validators\Contact;

    if($validation->passes())
    {
        $fromEmail = Input::get('email');
        $fromName = Input::get('name');
        $subject = "Email from user at website.com";
        $data = Input::get('message');

        $toEmail = '[email protected]';
        $toName = 'Mitch Glenn';

        Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){

            $message->to($toEmail, $toName);

            $message->from($fromEmail, $fromName);

            $message->subject($subject);
        });

        return Redirect::to('/')
            ->with('message', 'Your message was successfully sent!');
    }

    return Redirect::back()
        ->withInput()
        ->withErrors($validation->errors);
}

View

看法

 {{ Form::open(array('action' => 'ContactController@store', 'class' => 'row', 'id' => 'contact')) }}

    {{ Form::label('name', 'Name') }}
    {{ Form::text('name', '', array('class' => 'full')) }}

    {{ Form::label('email', 'Email') }}
    {{ Form::email('email', '', array('class' => 'full')) }}

    {{ Form::label('message', 'Message') }}
    {{ Form::textarea('message', '', array('class' => 'full')) }}

    {{ Form::submit('Send Now', array('class' => 'btn btn-large btn-primary')) }}

 {{ Form::close() }}

@include ('_partials.errors')

When I fill our the contact form and hit send, I get this error:

当我填写我们的联系表格并点击发送时,我收到此错误:

Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, string given

回答by Antonio Carlos Ribeiro

Set your data variable to be an array:

将您的数据变量设置为一个数组:

$data = array( 'message' => Input::get('message') );