public function action_send() { // CSRF対策 if (!Security::check_token()) { throw new HttpInvalidInputException('ページ遷移が正しくありません'); } $form = $this->forge_form(); $val = $form->validation()->add_callable('MyValidationRules'); if (!$val->run()) { $form->repopulate(); $this->template->title = 'コンタクトフォーム: エラー'; $this->template->content = View::forge('form/index'); $this->template->content->set_safe('html_error', $val->show_errors()); $this->template->content->set_safe('html_form', $form->build('form/confirm')); return; } $post = $val->validated(); $post['ip_address'] = Input::ip(); $post['user_agent'] = Input::user_agent(); unset($post['submit']); // データベースへ保存 $model_form = Model_Form::forge($post); $ret = $model_form->save(); if (!$ret) { Log::error('データベース保存エラー', __METHOD__); $form->repopulate(); $this->template->title = 'コンタクトフォーム: サーバエラー'; $this->template->content = View::forge('form/index'); $html_error = '<p>サーバでエラーが発生しました。</p>'; $this->template->content->set_safe('html_error', $html_error); $this->template->content->set_safe('html_form', $form->build('form/confirm')); return; } // メールの送信 try { $mail = new Model_Mail(); $mail->send($post); $this->template->title = 'コンタクトフォーム: 送信完了'; $this->template->content = View::forge('form/send'); return; } catch (EmailValidationFailedException $e) { Log::error('メール検証エラー: ' . $e->getMessage(), __METHOD__); $html_error = '<p>メールアドレスに誤りがあります。</p>'; } catch (EmailSendingFailedException $e) { Log::error('メール送信エラー: ' . $e->getMessage(), __METHOD__); $html_error = '<p>メールを送信できませんでした。</p>'; } $form->repopulate(); $this->template->title = 'コンタクトフォーム: 送信エラー'; $this->template->content = View::forge('form/index'); $this->template->content->set_safe('html_error', $html_error); $this->template->content->set_safe('html_form', $form->build('form/confirm')); }
public function test_メールを送信するとmail関数が呼ばれる() { // mail()関数からのデータをリセットしておく Config::set('_tests.mail.data', array()); $mail = new Model_Mail(); $mail->send($this->post); // mail()関数からのデータを代入 $mail_data = Config::get('_tests.mail.data'); // var_dump($mail_data); // exit; $this->assertNotEquals(array(), $mail_data); }