【发布时间】:2014-03-09 23:30:58
【问题描述】:
在我开始重构这段代码之前,它确实有效。在将所有业务逻辑移至模型并向控制器添加 try/catch 块后,一切似乎都正确。当我填写表格并点击发送时,我收到一条成功消息,但没有收到电子邮件。我哪里错了?
控制器
class ContactController extends BaseController {
protected $contact;
public function __construct(Contact $contact)
{
$this->beforeFilter('csrf', array('on' => 'post'));
$this->contact = $contact;
}
public function serve()
{
return View::make('layouts.contact');
}
public function store()
{
try
{
$this->contact->sendMessage(Input::all());
}
catch (ValidationError $e)
{
return Redirect::back()
->withInput()
->withErrors($e->getErrors());
}
return Redirect::to('contact')
->with('message', 'Your message was successfully sent!');
}
}
型号
class Contact extends Eloquent {
public function sendMessage($input)
{
$validation = new Services\Validators\Contact;
if($validation->passes())
{
$fromEmail = Input::get('email');
$fromName = Input::get('name');
$subject = "Email from user";
$data = [ 'msg' => Input::get('message') ];
$toEmail = 'logistics@praestavi.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);
});
}
else
{
$this->errors = $validation->errors;
throw new ValidationError($validation->errors);
}
}
}
重新总结问题:表单有效,但提交并收到成功消息后,我没有收到电子邮件。感谢您花时间查看我的代码。
编辑:添加请求的验证器
Contact.php
<?php namespace Services\Validators;
class Contact extends Validator {
public static $rules = [
'name' => 'required',
'email' => 'required|email',
'message' => 'required'
];
}
Validator.php
<?php namespace Services\Validators;
abstract class Validator {
protected $attributes;
public $errors;
public function __construct($attributes = null)
{
$this->attributes = $attributes ?: \Input::all();
}
public function passes()
{
$validation = \Validator::make($this->attributes, static::$rules);
if ($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
}
【问题讨论】:
-
就像他们说的:生活和学习!
-
嗯,你的“邮件”模块可能有问题?
-
@neoascetic 我不确定它会有什么问题,因为它在我将它从控制器移入模型之前就已经工作了。
-
请出示您的联系人验证器
标签: php laravel laravel-4 eloquent swiftmailer