我第一次尝试使用Laravel的Mail类,但遇到了一些困难。我有一个简单的联系表单,应该作为电子邮件发送给管理员。这是我试过的
控制器(一旦我能让它工作,将会重构为模型)
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);
}
查看
{{ 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')
当我填写联系表单并点击发送时,我收到此错误:
Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, string given
答案 0 :(得分:2)
将数据变量设置为数组:
$data = array( 'message' => Input::get('message') );