Laravel 4 from contact form to admin email

房东的猫 提交于 2019-12-24 01:57:16

问题


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

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 = 'test@dummyemail.com';
        $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

回答1:


Set your data variable to be an array:

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


来源:https://stackoverflow.com/questions/21565272/laravel-4-from-contact-form-to-admin-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!