7. 4 フォームメール メール送信

 自作のフォームメールプラグインの送信を実行するコードです。送信内容の入力処理、送信内容確認処理、送信処理を実行します。

コード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
class Hk_Form_Mail_Send {
    private $err_message = array();      // エラーメッセージ
    private $max_email = 256;            // メールアドレスの入力文字最大数
    private $max_tel   =  20;            // 電話番号の入力文字最大数
    private $max_url   =  60;            // URLの入力文字最大数
    private $max_text1 =  40;            // 1行の入力文字最大数
    private $max_text2 = 800;            // 複数行の入力文字最大数

    // メール送信
    function send_form_mail( $atts ) {
        global $hk_form_mail;
        // form_no 抽出
        extract( shortcode_atts( array( 'form_no' => 0 ), $atts ) );
        if ( $form_no == 0 )        return '';
        // データベースからフォームデータの読み込み
        $form_data = $hk_form_mail->db_read( $form_no );
        if ( $form_data == '' )        return '';
        // 初期化:post data
        $post_data = array();
        $label_exist = $form_data['label_exist'];
        $post_data['label_exist'] = $label_exist;
        for ( $i=1; $i<=$label_exist; $i++ ) {
            $post_data[ $i ]['label'] = $form_data[ $i ]['label'];
            $post_data[ $i ]['need']  = $form_data[ $i ]['need'];
            $post_data[ $i ]['type']  = $form_data[ $i ]['type'];
            $post_data[ $i ]['post']  = '';
        }
        // 初期化:error flag & message
        //   err_message[0]:error flag
        $this->err_message    = array();
        $this->err_message[0] = 0;
        for ( $i=1; $i<=$label_exist; $i++ )
            $this->err_message[$i] = '';
        // 送信ボタン
        if ( ! isset( $_POST["mail_next"] ) )
            $mail_next = 'スタート';
        else
            $mail_next = hk_func_post_read( 'mail_next', 1 );
        // スタート
        if ( $mail_next == 'スタート' ) {
            // メール入力画面表示
            $html = $this->show_mail_input( $post_data );
            return $html;
        }
        // 送信内容確認
        if ( $mail_next == '送信内容確認' ) {
            // POSTされたデータの取り込み
            $post_data = $this->mail_post_data( $post_data );
            // POStされたデータのチェック
            $post_data = $this->mail_post_check( $post_data );
            if ( $this->err_message[ 0 ] != 0 ) {
                $html = $this->show_mail_input( $post_data );
                return $html;
            }
            // 確認画面表示
            $html = $this->show_mail_confirm( $post_data );
            return $html;
        }
        // 送信内容修正
        if ( $mail_next == '送信内容修正' ) {
            // POSTされたデータの取り込み
            $post_data = $this->mail_post_data( $post_data );
            // メール入力画面表示
            $html = $this->show_mail_input( $post_data );
            return $html;
        }
        // 送信
        if ( $mail_next == '送信' ) {
            // POSTされたデータの取り込み
            $post_data = $this->mail_post_data( $post_data );
            // POStされたデータのチェック
            $post_data = $this->mail_post_check( $post_data );
            if ( $this->err_message[ 0 ] != 0 )        return '';
            // 送信
            $html = $this->mail_send( $form_data, $post_data );
            return $html;
        }
        return '';
    }

    // POSTされたデータの取り込み
    //   戻り値:$post_data
    private function mail_post_data( $post_data ) {
        // フォーム項目数
        $label_exist = $post_data['label_exist'];
        // POSTされたデータの取り込み
        for ( $i=1; $i<=$label_exist; $i++ ) {
            $type = $post_data[ $i ]['type'];
            $post_str = 'post_data_'.$i;
            // 入力行数で場合分け(複数行 or1行)
            if ( $type == 'textarea' )
                $post_data[ $i ]['post'] = hk_func_post_read( $post_str, 2 );
            else
                $post_data[ $i ]['post'] = hk_func_post_read( $post_str, 1 );
        }
        return $post_data;
    }

    // POStされたデータのチェック
    //   戻り値:修正後の$post_data
    private function mail_post_check( $post_data ) {
        $label_exist = $post_data['label_exist'];
        for ( $i=1; $i<=$label_exist; $i++ ) {
            $need = $post_data[ $i ]['need'];
            $type = $post_data[ $i ]['type'];
            // 入力有無チェック
            $length = mb_strlen( $post_data[ $i ]['post'] );
            if ( $length == 0 ) {
                if ( $need == 'yes' ) {
                    $this->err_message[ $i ] = ' 必須項目に入力されていません';
                    $this->err_message[ 0 ] = 1;
                    continue;
                }
                continue;
            }
            // メールアドレス
            if ( $type == 'email' ) {
                if ( $length > $this->max_email ) {
                    $this->err_message[ $i ] = ' 入力は'.$this->max_email.'文字までにしてください';
                    // 最大文字数でカット
                    $post_data[ $i ]['post'] = mb_substr( $post_data[ $i ]['post'], 0, $this->max_email );
                    $this->err_message[ 0 ] = 1;
                    continue;
                }
                // メールアドレスチェック
                if ( filter_var( $post_data[ $i ]['post'], FILTER_VALIDATE_EMAIL ) === false ) {
                    $this->err_message[ $i ] = ' メールアドレスの書式が正しくありません';
                    $this->err_message[ 0 ] = 1;
                    continue;
                }
                continue;
            }
            // 電話番号
            if ( $type == 'tel' ) {
                $rtc = $this->tel_check( $post_data[$i]['post'] );
                if ( ! $rtc ) {
                    $this->err_message[ $i ] = ' 電話番号の書式が正しくありません';
                    // 最大文字数でカット
                    $post_data[ $i ]['post'] = mb_substr( $post_data[ $i ]['post'], 0, $this->max_tel );
                    $this->err_message[ 0 ] = 1;
                    continue;
                }
                continue;
            }
            // URL
            if ( $type == 'url' ) {
                if ( $length > $this->max_url ) {
                    $this->err_message[$i] = ' 入力は'.$this->max_url.'文字までにしてください';
                    // 最大文字数でカット
                    $post_data[ $i ]['post'] = mb_substr( $post_data[ $i ]['post'], 0, $this->max_url );
                    $this->err_message[ 0 ] = 1;
                    continue;
                }
                // URLチェック
                if ( filter_var( $post_data[ $i ]['post'], FILTER_VALIDATE_URL ) === false ) {
                    $this->err_message[ $i ] = ' URLの書式が正しくありません';
                    $this->err_message[ 0 ] = 1;
                    continue;
                }
                continue;
            }
            // 複数行テキスト
            if ( $type == 'textarea' ) {
                if ( $length > $this->max_text2 ) {
                    $this->err_message[ $i ] = ' 入力は'.$this->max_text2.'文字までにしてください';
                    // 最大文字数でカット
                    $post_data[ $i ]['post'] = mb_substr( $post_data[ $i ]['post'], 0, $this->max_text2 );
                    $this->err_message[ 0 ] = 1;
                    continue;
                }
            }
            // 一行テキスト
            if ( $type == 'text' ) {
                if ( $length > $this->max_text1 ) {
                    $this->err_message[ $i ] = ' 入力は'.$this->max_text1.'文字までにしてください';
                    // 最大文字数でカット
                    $post_data[ $i ]['post'] = mb_substr( $post_data[ $i ]['post'], 0, $this->max_text1 );
                    $this->err_message[ 0 ] = 1;
                    continue;
                }
            }
            // 複数行テキストと一行テキストのスパムチェック
            if (( $type == 'textarea' ) || ( $type == 'text' )) {
                $rtc = hk_func_spam_check( $post_data[ $i ]['post'] );
                if ( $rtc == 1 ) {
                    $this->err_message[ $i ] = ' URLを書かないでください';
                    $this->err_message[ 0 ] = 1;
                }
                else if ( $rtc == 2 ) {
                    $this->err_message[ $i ] = ' HTMLを書かないでください';
                    $this->err_message[ 0 ] = 1;
                }
            }
        }
        return    $post_data;
    }

    // 電話番号チェック
    //   戻り値:true、false
    private function tel_check( $content ) {
        // 数字、- だけで構成されるか?
        if ( ! preg_match( '/^[0-9-]+$/',$content ) )    return false;
        // - の削除
        $content2 = preg_replace( '/-/', '', $content );
        $length2 = strlen( $content2 );
        // 中継する電話会社を使って電話する時や国際電話
        if ( preg_match( '/^00/', $content2 ) )          return false;
        // 国際電話
        if ( preg_match( '/^010/', $content2 ) )         return false;
        // 携帯電話、PHS 11桁
        if ( preg_match( '/^0[7-9]0-/', $content ) ) {
            if ( $length2 == 11 )    return true;
            return false;
        }
        // 固定電話(他) 10桁
        if ( preg_match( '/^0[0-9]{1,4}-[0-9]{1,4}-[0-9]{4}$/', $content ) ) {
            if ( $length2 == 10 )    return true;
            return false;
        }
        return false;
    }

    // メール入力画面表示
    //   戻り値:$html
    private function show_mail_input( $post_data ) {
        // HTML特殊文字変換
        $post_data = $this->mail_data_to_html( $post_data );
        // 戻り値の初期化
        $html = '<div class="hk_form_mail">'."\n";
        // エラー有無表示
        if ( $this->err_message[0] != 0 ) {
            $html .= '  <h2>入力エラー</h2>'."\n";
            $html .= '  <p>正しく入力されていない項目があります。</p>'."\n";
        }
        // フォーム項目数
        $label_exist = $post_data['label_exist'];
        // 入力欄表示
        $html .= '  <form method="post">'."\n";
        for ( $i=1; $i<=$label_exist; $i++ ) {
            $label = $post_data[ $i ]['label'];
            $need  = $post_data[ $i ]['need'];
            $type  = $post_data[ $i ]['type'];
            if ( $need == 'yes' )  $need_str = ' (必須)';
            else                   $need_str = '';
            $html .= '    <p><label for="box'.$i.'">'.$label.$need_str.'</label>';
            $html .= '<span class="hk_italic">'.$this->err_message[$i].'</span><br />'."\n";
            $id_name = 'id="box'.$i.'" name="post_data_'.$i.'"';
            if ( $type == 'textarea' )
                $html .= '      <textarea cols="80" rows="10" '.$id_name.'>'.$post_data[ $i ]['post'].'</textarea></p>'."\n";
            else if ( $type == 'email' )
                $html .= '      <input type="email" size="40" maxlength="'.$this->max_email.'" '.$id_name.' value="'.$post_data[ $i ]['post'].'" /></p>'."\n";
            else if ( $type == 'tel' ) {
                $html .= '      <input type="tel" size="40" maxlength="'.$this->max_tel.'" '.$id_name.' value="'.$post_data[ $i ]['post'].'" /><br />'."\n";
                $html .= ' [例] 012-345-6789、090-12345678、090-1234-5678</p>'."\n";
            }
            else if ( $type == 'url' )
                $html .= '      <input type="url" size="40" maxlength="'.$this->max_url.'" '.$id_name.' value="'.$post_data[ $i ]['post'].'" /></p>'."\n";
            else
                $html .= '      <input type="'.$type.'" size="40" maxlength="'.$this->max_text1.'" '.$id_name.' value="'.$post_data[ $i ]['post'].'" /></p>'."\n";
        }
        $html .= '    <p><input type="submit" name="mail_next" value="送信内容確認" /></p>'."\n";
        $html .= '  </form>'."\n";
        $html .= '</div>'."\n";
        return $html;
    }

    // 確認画面表示
    //   戻り値:$html
    private function show_mail_confirm( $post_data ) {
        // HTML特殊文字変換
        $post_data = $this->mail_data_to_html( $post_data );
        // 戻り値の初期化
        $html = '<div class="hk_form_mail">'."\n";
        // フォーム項目数
        $label_exist = $post_data['label_exist'];
        // 画面表示
        $html .= '  <h2>入力内容の確認</h2>'."\n";
        $html .= '  <p>以下の内容がメールで送信されます。<br />'."\n";
        $html .= '入力した内容に間違いがないかをご確認の上、「送信」ボタンをクリックして下さい。<br />'."\n";
        $html .= '修正する場合は「送信内容修正」ボタンをクリックして下さい。</p>'."\n";
        $html .= '<table border="0">'."\n";
        $html .= '  <tbody>'."\n";
        $html .= '    <tr><th>項目名</th><th>入力内容</th></tr>'."\n";
        for ( $i=1; $i<=$label_exist; $i++ ) {
            $label = $post_data[ $i ]['label'];
            $type  = $post_data[ $i ]['type'];
            // 入力行数で場合分け(複数行 or1行)
            if ( $type == 'textarea' )
                $post_reg = preg_replace( '/\r\n|[\r\n]/', '<br />', $post_data[ $i ]['post'] );
            else
                $post_reg = $post_data[ $i ]['post'];
            $html .= '    <tr><td class="hk_nowrap">'.$label.'</td><td>'.$post_reg.'</td></tr>'."\n";
        }
        $html .= '  </tbody>'."\n";
        $html .= '</table>'."\n";
        $html .= '  <form method="post">'."\n";
        $html .= '    <input type="submit" name="mail_next" value="送信" />     ';
        $html .= '<input type="submit" name="mail_next" value="送信内容修正" />'."\n";
        for ( $i=1; $i<=$label_exist; $i++ )
            $html .= '    <input type="hidden" name="post_data_'.$i.'" value="'.$post_data[ $i ]['post'].'" />'."\n";
        $html .= '  </form>'."\n";
        $html .= '</div>'."\n";
        return $html;
    }

    // HTML特殊文字変換
    //   戻り値:$post_data
    private function mail_data_to_html( $post_data ) {
        $label_exist = $post_data['label_exist'];
        for ( $i=1; $i<=$label_exist; $i++ ) {
            $post_data[ $i ]['label'] = htmlspecialchars( $post_data[ $i ]['label'] );
            $post_data[ $i ]['post']  = htmlspecialchars( $post_data[ $i ]['post'] );
        }
        return $post_data;
    }

    // 送信
    //   戻り値:$html
    private function mail_send( $form_data, $post_data ) {
        // USER INFORMATION
        $host_addr  = $_SERVER["REMOTE_ADDR"];
        $host_name  = gethostbyaddr( $host_addr );
        $host_agent = $_SERVER["HTTP_USER_AGENT"];
        // GET PRESENT TIME
        $date = hk_func_get_local_time();
        // 送信データ
        $title = $form_data['title'];
        $to    = $form_data['to'];
        $cc    = $form_data['cc'];
        $bcc   = $form_data['bcc'];
        $from  = $form_data['from'];
        $lead  = $form_data['lead'];
        $label_exist = $form_data['label_exist'];
        // アドレス整形
        $to  = preg_replace( '/\r\n|[\r\n]/', ',', $to );
        $cc  = preg_replace( '/\r\n|[\r\n]/', ',', $cc );
        $bcc = preg_replace( '/\r\n|[\r\n]/', ',', $bcc );
        // HEADER
        $header  = '';
        if ( $cc != '' )
            $header .= 'Cc: '.$cc."\n";
        if ( $bcc != '' )
            $header .= 'Bcc: '.$bcc."\n";
        $header .= 'From: '.$from;
        // MESSAGE BODY
        $body  = $lead."\n\n";
        $body .= "────────────────────────────\n";
        $body .= "▼ メッセージの内容 ▼\n";
        $body .= "────────────────────────────\n\n";
        $body .= "【送信日時】\n  ".$date."\n";
        for ( $i=1; $i<=$label_exist; $i++ ) {
            $label = $form_data[ $i ]['label'];
            $type  = $form_data[ $i ]['type'];
            // 入力行数で場合分け(複数行 or1行)
            if ( $type == 'textarea' )
                $body .= '【'.$label."】\n".$post_data[ $i ]['post']."\n";
            else
                $body .= '【'.$label."】\n  ".$post_data[ $i ]['post']."\n";
        }
        $body .= "\n─── 送信者の情報 ─────────────────\n\n";
        $body .= "【送信者のIPアドレス】 ".$host_addr."\n";
        $body .= "【送信者のホスト名】  ".$host_name."\n";
        $body .= "【送信者のUSER AGENT】\n".$host_agent."\n";
        // 送信
        mb_language('ja') ;
        $result = mb_send_mail( $to , $title , $body , $header );
        // 画面表示
        $html = '';
        // メッセージ
        if ( $result ) {
            $page_title = '送信完了';
            $phrase = '<p>メールは正常に送信されました。<br />ご入力ありがとうございました。</p>';
        }
        else {
            $page_title = 'メール送信失敗';
            $phrase = '<p>メールの送信が行われませんでした。<br />ブラウザの[戻る]ボタンをクリックして前のページに戻り、<br />再度「送信」ボタンを押して下さい。</p>';
        }
        $html .= '<h2>'.$page_title.'</h2>'."\n";
        $html .= $phrase."\n";
        return $html;
    }
}

説明

 下記関数に関しては“共通関数”をご覧ください。
  hk_func_post_read
  hk_func_spam_check
  hk_func_get_local_time

 このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。

関連

更新日:2016/01/24
掲載日:2014/08/11