TransmitMail 2をカスタマイズ
メールフォームのプログラムは数多くあるのですが、添付ファイルを扱えて無料のものってなかなかないですよね。
私はTransmitMail 2を使用しています。
開発者の方、ありがとうございます。
ただ、このプログラムは、そのまま使うのはちょっとなあという部分がいくつかあるため、カスタマイズを入れています。
まず、自動返信関連です。
メールフォームからのスパムメールというのが割とあります。
添付ファイルを付けたスパムの送信元にされるのはちょっと困った状態になります。
なので、自動返信には添付ファイルを付けないという設定が出来るようにしました。
Configファイル側には
ifの条件を追加しているだけです。
1 2 3 4 5 6 7 8 9 10 11 12 | if ( $this ->config[ 'file' ] && (! $is_auto_reply || $this ->config[ 'auto_reply_files' ] ) ) { foreach ( $this ->files as $file ) { $attach [] = array ( 'PATH' => $this ->config[ 'tmp_dir' ] . $file [ 'tmp_name' ], 'NAME' => $file [ 'name' ] ); } if (isset( $attach )) { $this ->mail->attach( $attach ); } } |
それから、必須項目について。
HTML側で設定できるのは便利といえば便利なのですが、それだと、メールフォームを直接叩けば必須項目の設定を無視できることになります。
スパムメールの踏み台としてとっても便利な仕様ですね。
そこまでしなくても、HTMLをクライアント側でちょちょっと書き換えれば設定変更できてしまう仕様って何だろう…という感じです。
なので、Configファイル内でも必須項目設定が出来るようにしました。
Configファイル側には、
$config[‘required’] = array();
という設定項目を追加します。
で、[lib/TransmitMail.php]の432行目付近を下記のように変更してあります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // 入力必須チェック if (isset( $this ->post[ 'required' ]) ) { $config_required = ( is_array ( $this ->config[ 'required' ]) && count ( $this ->config[ 'required' ]) > 0 ) ? $this ->config[ 'required' ] : array (); foreach ( $this ->post[ 'required' ] as $value ) { $config_required = array_diff ( $config_required , array ( $value )); $config_required = array_values ( $config_required ); $this ->tpl->set( "required.$value" , false); if (!isset( $this ->post[ $value ]) || (isset( $this ->post[ $value ]) && ( is_null ( $this ->post[ $value ]) || ( $this ->post[ $value ] === '' )))) { $this ->tpl->set( "required.$value" , $this ->h( $value . $this ->config[ 'error_required' ])); $this ->global_errors[] = $this ->h( $value . $this ->config[ 'error_required' ]); } } if ( count ( $config_required ) > 0){ foreach ( $config_required as $value ) { $this ->tpl->set( "required.$value" , false); if (!isset( $this ->post[ $value ]) || (isset( $this ->post[ $value ]) && ( is_null ( $this ->post[ $value ]) || ( $this ->post[ $value ] === '' )))) { $this ->tpl->set( "required.$value" , $this ->h( $value . $this ->config[ 'error_required' ])); $this ->global_errors[] = $this ->h( $value . $this ->config[ 'error_required' ]); } } } } |
あとは、外部から値を取り込めるようにしました。
WordPressのカスタムフィールドでラジオボタンやチェックボックスの選択肢を変更できると便利だよね、でもできないね、ということでカスタマイズしました。
このメールフォームで使用されているtinyTemplate.phpって、シンプル過ぎて微妙なんですよね。
ifも真偽値の判定しかできないっぽいし。
ファイル数1つだけで使えてシンプルかつポータブルだから採用されたのかな…うーん。
[lib/TransmitMail.php]の1000行目付近、
1 2 3 4 5 6 | $this_file_path = dirname( __FILE__ ); require_once ( $this_file_path . '/custom_params.php' ); $custom_params = custom_params(); $this ->tpl->set( 'custom_params' , $custom_params ); |
同じ階層に[custom_params.php]というファイルを置いてあげれば、その中で好き放題できます。
クラス化するほどの内容じゃないので、ただのfunctionです。
オブジェクト指向じゃないとやだーって方はテキトーに直してください。
1 2 3 4 5 6 7 8 9 | <?php function custom_params(){ $custom_params = array (); //ここで使いたい値を$custom_paramsに持たせる。 return $custom_params ; } ?> |
もちろん、ここでセキュリティホールを作り込まないように注意してください。
$_POSTの値をそのまま評価/判定に使うとか、そういうのはちょっと恐ろしいです。
いや、意外と多いんです、そういうプログラムとかシステムとか…
システム改修の依頼を受けてコードを読むと、$_POSTの内容を信頼しすぎてるとか、無害化もろくにしないでSQLに放り込んでるとか…しかもプレースホルダも使わないとか。
恐ろしい。
あとは、config側のファイルパス関連をちょっといじったりはしてますが、それはまあここに書くほどじゃないですか…
同じような内容で困っている方の参考になれば幸いです。