在我开始重新分解此代码之前,它有效。将所有业务逻辑移动到模型并将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;
}
}
答案 0 :(得分:0)
假设你刚刚在这里重构了代码,我会开始在Mail::send()
调用周围添加一些调试。也许把它包装在dd()
函数中。你应该看到一个整数;邮件成功发送到的收件人数。
此外,您的app/config/mail.php
内容是什么样的(请删除所有敏感密码)。