问题
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