1
|
|
|
<?php |
2
|
|
|
/* コンタクトフォーム |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
class Form extends CI_Controller { |
7
|
|
|
|
8
|
|
|
public function __construct() |
9
|
|
|
{ |
10
|
|
|
# 親クラスのコンストラクタを呼び出します。コントローラにコンストラクタを |
11
|
|
|
# 記述する場合は、忘れずに記述してください。 |
12
|
|
|
parent::__construct(); |
13
|
|
|
|
14
|
|
|
# 必要なヘルパーをロードします。 |
15
|
|
|
$this->load->helper(['form', 'url']); |
16
|
|
|
|
17
|
|
|
# セッションクラスをロードすることで、セッションを開始します。 |
18
|
|
|
$this->load->library('session'); |
19
|
|
|
|
20
|
|
|
# 出力クラスのset_header()メソッドでHTTPヘッダのContent-Typeヘッダを指定 |
21
|
|
|
# します。 |
22
|
|
|
$this->output->set_header('Content-Type: text/html; charset=UTF-8'); |
23
|
|
|
|
24
|
|
|
# バリデーション(検証)クラスをロードします。 |
25
|
|
|
$this->load->library('form_validation'); |
26
|
|
|
|
27
|
|
|
//$this->output->enable_profiler(TRUE); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function _set_validation() |
31
|
|
|
{ |
32
|
|
|
# バリデーションの設定をします。 |
33
|
|
|
$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); |
34
|
|
|
$this->form_validation->set_rules('name', '名前', 'trim|required|max_length[20]'); |
35
|
|
|
$this->form_validation->set_rules('email', 'メールアドレス', 'trim|required|valid_email'); |
36
|
|
|
$this->form_validation->set_rules('comment', 'コメント', 'required|max_length[200]'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function index() |
40
|
|
|
{ |
41
|
|
|
# 入力ページ(form)のビューをロードし表示します。 |
42
|
|
|
$this->load->view('form'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function confirm() |
46
|
|
|
{ |
47
|
|
|
# 検証ルールを設定します。 |
48
|
|
|
$this->_set_validation(); |
49
|
|
|
|
50
|
|
|
# バリデーション(検証)クラスのrun()メソッドを呼び出し、送信されたデータの検証 |
|
|
|
|
51
|
|
|
# を行います。検証OKなら、確認ページ(form_confirm)を表示します。 |
52
|
|
|
if ($this->form_validation->run() == TRUE) |
53
|
|
|
{ |
54
|
|
|
$this->load->view('form_confirm'); |
55
|
|
|
} |
56
|
|
|
# 検証でエラーの場合、入力ページ(form)を表示します。 |
57
|
|
|
else |
58
|
|
|
{ |
59
|
|
|
$this->load->view('form'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function send() |
64
|
|
|
{ |
65
|
|
|
# 検証ルールを設定します。 |
66
|
|
|
$this->_set_validation(); |
67
|
|
|
|
68
|
|
|
# 送信されたデータの検証を行い、検証OKなら、メールを送信します。 |
69
|
|
|
if ($this->form_validation->run() == TRUE) |
70
|
|
|
{ |
71
|
|
|
# メールの内容を設定します。 |
72
|
|
|
$mail = []; |
73
|
|
|
$mail['from_name'] = $this->input->post('name'); |
74
|
|
|
$mail['from'] = $this->input->post('email'); |
75
|
|
|
$mail['to'] = '[email protected]'; |
76
|
|
|
$mail['subject'] = 'コンタクトフォーム'; |
77
|
|
|
$mail['body'] = $this->input->post('comment'); |
78
|
|
|
|
79
|
|
|
# _sendmail()メソッドを呼び出しメールの送信処理を行います。 |
80
|
|
|
# メールの送信に成功したら、完了ページ(form_end)を表示します。 |
81
|
|
|
if ($this->_sendmail($mail)) |
82
|
|
|
{ |
83
|
|
|
# 完了ページ(form_end)を表示し、セッションを破棄します。 |
84
|
|
|
$this->load->view('form_end'); |
85
|
|
|
$this->session->sess_destroy(); |
86
|
|
|
} |
87
|
|
|
# メールの送信に失敗した場合、エラーを表示します。 |
88
|
|
|
else |
89
|
|
|
{ |
90
|
|
|
echo 'メール送信エラー'; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
# 検証でエラーの場合、入力ページ(form)を表示します。 |
94
|
|
|
else |
95
|
|
|
{ |
96
|
|
|
$this->load->view('form'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function _sendmail($mail) |
101
|
|
|
{ |
102
|
|
|
# Emailクラスをロードします。 |
103
|
|
|
$this->load->library('email'); |
104
|
|
|
$config = []; |
105
|
|
|
# メールの送信方法を指定します。ここでは、mail()関数を使います。 |
106
|
|
|
$config['protocol'] = 'mail'; |
107
|
|
|
# 日本語ではワードラップ機能は使えませんのでオフにします。 |
108
|
|
|
$config['wordwrap'] = FALSE; |
109
|
|
|
# $configでEmailクラスを初期化します。 |
110
|
|
|
$this->email->initialize($config); |
111
|
|
|
|
112
|
|
|
# メールの内容を変数に代入します。 |
113
|
|
|
$from_name = $mail['from_name']; |
114
|
|
|
$from = $mail['from']; |
115
|
|
|
$to = $mail['to']; |
116
|
|
|
$subject = $mail['subject']; |
117
|
|
|
$body = $mail['body']; |
118
|
|
|
|
119
|
|
|
# 差出人、あて先、件名、本文をEmailクラスに設定します。 |
120
|
|
|
$this->email->from($from, $from_name); |
121
|
|
|
$this->email->to($to); |
122
|
|
|
$this->email->subject($subject); |
123
|
|
|
$this->email->message($body); |
124
|
|
|
|
125
|
|
|
# Emailクラスのsend()メソッドで、実際にメールを送信します。 |
126
|
|
|
# メールの送信が成功した場合はTRUEを、失敗した場合はFALSEを返します。 |
127
|
|
|
if ($this->email->send()) |
128
|
|
|
{ |
129
|
|
|
return TRUE; |
130
|
|
|
} |
131
|
|
|
else |
132
|
|
|
{ |
133
|
|
|
// echo $this->email->print_debugger(); |
|
|
|
|
134
|
|
|
return FALSE; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.