Question

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
Was it helpful?

Solution

Set your data variable to be an array:

$data = array( 'message' => Input::get('message') );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top