source: branches/version-2_12-dev/data/class/SC_SendMail.php @ 21703

Revision 21703, 10.0 KB checked in by Seasoft, 14 years ago (diff)

#1715 (SC_SendMail#getNameAddress 名前の扱いに誤り)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
RevLine 
[16503]1<?php
2/*
[16582]3 * This file is part of EC-CUBE
4 *
[20764]5 * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved.
[16503]6 *
7 * http://www.lockon.co.jp/
[16582]8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
[16503]22 */
23
24//--- テキスト/HTML メール送信
25class SC_SendMail {
26
27    var $to;            //  送信先
28    var $subject;       //  題名
29    var $body;          //  本文
30    var $cc;            // CC
31    var $bcc;           // BCC
32    var $replay_to;     // replay_to
33    var $return_path;   // return_path
34    var $objMail;
35
36    // コンストラクタ
37    function SC_SendMail() {
[17109]38        $this->arrRecip = array();
[21514]39        $this->to = '';
40        $this->subject = '';
41        $this->body = '';
42        $this->cc = '';
43        $this->bcc = '';
44        $this->replay_to = '';
45        $this->return_path = '';
[16503]46        $this->backend = MAIL_BACKEND;
47        $this->host = SMTP_HOST;
48        $this->port = SMTP_PORT;
[21446]49        mb_language('Japanese');
[16503]50
51        //-- PEAR::Mailを使ってメール送信オブジェクト作成
52        $this->objMail =& Mail::factory($this->backend,
53                                        $this->getBackendParams($this->backend));
54    }
55
[17109]56    // 送信先の設定
57    function setRecip($key, $recipient) {
58        $this->arrRecip[$key] = $recipient;
59    }
[20540]60
[16503]61    // 宛先の設定
[21514]62    function setTo($to, $to_name = '') {
63        if ($to != '') {
[17109]64            $this->to = $this->getNameAddress($to_name, $to);
[20538]65            $this->setRecip('To', $to);
[17109]66        }
[16503]67    }
68
69    // 送信元の設定
[21514]70    function setFrom($from, $from_name = '') {
[16503]71        $this->from = $this->getNameAddress($from_name, $from);
72    }
73
74    // CCの設定
[21514]75    function setCc($cc, $cc_name = '') {
76        if ($cc != '') {
[16503]77            $this->cc = $this->getNameAddress($cc_name, $cc);
[20538]78            $this->setRecip('Cc', $cc);
[16503]79        }
80    }
81
82    // BCCの設定
83    function setBCc($bcc) {
[21514]84        if ($bcc != '') {
[16503]85            $this->bcc = $bcc;
[20538]86            $this->setRecip('Bcc', $bcc);
[16503]87        }
88    }
89
90    // Reply-Toの設定
91    function setReplyTo($reply_to) {
[21514]92        if ($reply_to != '') {
[16503]93            $this->reply_to = $reply_to;
94        }
95    }
96
97    // Return-Pathの設定
98    function setReturnPath($return_path) {
99        $this->return_path = $return_path;
100    }
101
102    // 件名の設定
103    function setSubject($subject) {
[20538]104        $this->subject = mb_encode_mimeheader($subject, 'JIS', 'B', "\n");
[17568]105        $this->subject = str_replace("\x0D\x0A", "\n", $this->subject);
106        $this->subject = str_replace("\x0D", "\n", $this->subject);
107        $this->subject = str_replace("\x0A", "\n", $this->subject);
[16503]108    }
109
110    // 本文の設定
111    function setBody($body) {
[21632]112        // iso-2022-jpだと特殊文字が?で送信されるのでJISを使用する
[20538]113        $this->body = mb_convert_encoding($body, 'JIS', CHAR_CODE);
[16503]114    }
115
[20970]116    // SMTPサーバーの設定
[16503]117    function setHost($host) {
118        $this->host = $host;
119        $arrHost = array(
120                'host' => $this->host,
121                'port' => $this->port
122        );
123        //-- PEAR::Mailを使ってメール送信オブジェクト作成
[20538]124        $this->objMail =& Mail::factory('smtp', $arrHost);
[16503]125
126    }
127
128    // SMTPポートの設定
129    function setPort($port) {
130        $this->port = $port;
131        $arrHost = array(
132                'host' => $this->host,
133                'port' => $this->port
134        );
135        //-- PEAR::Mailを使ってメール送信オブジェクト作成
[20538]136        $this->objMail =& Mail::factory('smtp', $arrHost);
[16503]137    }
138
139    // 名前<メールアドレス>の形式を生成
140    function getNameAddress($name, $mail_address) {
[21514]141            if ($name != '') {
[16503]142                // 制御文字を変換する。
143                $_name = $name;
[20538]144                $_name = mb_encode_mimeheader($_name, 'JIS', 'B', "\n");
[21703]145                $name_address = $_name . ' <' . $mail_address . '>';
[16503]146            } else {
147                $name_address = $mail_address;
148            }
149            return $name_address;
150    }
151
[21515]152    function setItem($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') {
[16503]153        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
154    }
155
[21515]156    function setItemHtml($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') {
[16503]157        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
158    }
159
160    /*  ヘッダ等を格納
161         $to            -> 送信先メールアドレス
162         $subject       -> メールのタイトル
163         $body          -> メール本文
164         $fromaddress   -> 送信元のメールアドレス
165         $header        -> ヘッダー
166         $from_name     -> 送信元の名前(全角OK)
167         $reply_to      -> reply_to設定
168         $return_path   -> return-pathアドレス設定(エラーメール返送用)
169         $cc            -> カーボンコピー
170         $bcc           -> ブラインドカーボンコピー
171    */
[21515]172    function setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') {
[16503]173        // 宛先設定
[17109]174        $this->setTo($to);
[16503]175        // 件名設定
176        $this->setSubject($subject);
[21632]177        // 本文設定
[16503]178        $this->setBody($body);
179        // 送信元設定
180        $this->setFrom($fromaddress, $from_name);
181        // 返信先設定
182        $this->setReplyTo($reply_to);
183        // CC設定
184        $this->setCc($cc);
185        // BCC設定
186        $this->setBcc($bcc);
187
188        // Errors-Toは、ほとんどのSMTPで無視され、Return-Pathが優先されるためReturn_Pathに設定する。
[21514]189        if ($errors_to != '') {
[16503]190            $this->return_path = $errors_to;
[21514]191        } else if ($return_path != '') {
[16503]192            $this->return_path = $return_path;
193        } else {
194            $this->return_path = $fromaddress;
195        }
196    }
197
198    // ヘッダーを返す
199    function getBaseHeader() {
200        //-- 送信するメールの内容と送信先
201        $arrHeader['MIME-Version'] = '1.0';
202        $arrHeader['To'] = $this->to;
203        $arrHeader['Subject'] = $this->subject;
204        $arrHeader['From'] = $this->from;
205        $arrHeader['Return-Path'] = $this->return_path;
[21514]206        if ($this->reply_to != '') {
[16503]207            $arrHeader['Reply-To'] = $this->reply_to;
208        }
[21514]209        if ($this->cc != '') {
[16503]210            $arrHeader['Cc'] = $this->cc;
211        }
[21514]212        if ($this->bcc != '') {
[16503]213            $arrHeader['Bcc'] = $this->bcc;
214        }
[21514]215        $arrHeader['Date'] = date('D, j M Y H:i:s O');
[21632]216        $arrHeader['Content-Transfer-Encoding'] = '7bit';
[16503]217        return $arrHeader;
218    }
219
220    // ヘッダーを返す
221    function getTEXTHeader() {
222        $arrHeader = $this->getBaseHeader();
[21634]223        $arrHeader['Content-Type'] = 'text/plain; charset="ISO-2022-JP"';
[16503]224        return $arrHeader;
225    }
226
227    // ヘッダーを返す
228    function getHTMLHeader() {
229        $arrHeader = $this->getBaseHeader();
[21634]230        $arrHeader['Content-Type'] = 'text/html; charset="ISO-2022-JP"';
[16503]231        return $arrHeader;
232    }
[20540]233
[18866]234    /**
235     * メーラーバックエンドに応じた送信先を返す
236     *
237     * @return array|string メーラーバックエンドに応じた送信先
238     */
[17109]239    function getRecip() {
240        switch ($this->backend) {
[18866]241            // PEAR::Mail_mail#send は、(他のメーラーバックエンドと異なり) 第1引数を To: として扱う。Cc: や Bcc: は、ヘッダー情報から処理する。
[21526]242            case 'mail':
243                return $this->to;
244
245            case 'sendmail':
246            case 'smtp':
247            default:
248                return $this->arrRecip;
[17109]249        }
250    }
[16503]251
[18667]252    /**
253     * TXTメール送信を実行する.
254     *
255     * 設定された情報を利用して, メールを送信する.
256     *
257     * @return void
258     */
[18866]259    function sendMail($isHtml = false) {
260        $header = $isHtml ? $this->getHTMLHeader() : $this->getTEXTHeader();
[18667]261        $recip = $this->getRecip();
[16503]262        // メール送信
[18866]263        $result = $this->objMail->send($recip, $header, $this->body);
[18667]264            if (PEAR::isError($result)) {
265                GC_Utils_Ex::gfPrintLog($result->getMessage());
266                GC_Utils_Ex::gfDebugLog($header);
[18866]267                return false;
[18667]268            }
[18866]269        return true;
[16503]270    }
271
[18866]272    /**
273     * HTMLメール送信を実行する.
274     *
275     * @return void
276     */
[16503]277    function sendHtmlMail() {
[20539]278        return $this->sendMail(true);
[16503]279    }
280
281    /**
[20970]282     * メーラーバックエンドに応じたパラメーターを返す.
[16503]283     *
284     * @param string $backend Pear::Mail のバックエンド
[20970]285     * @return array メーラーバックエンドに応じたパラメーターの配列
[16503]286     */
287    function getBackendParams($backend) {
288        switch ($backend) {
[21526]289            case 'mail':
290                $arrParams = array();
291                break;
292            case 'sendmail':
[21527]293                $arrParams = array(
294                    'sendmail_path' => '/usr/bin/sendmail',
295                    'sendmail_args' => '-i',
296                );
[21526]297                break;
298            case 'smtp':
299            default:
300                $arrParams = array(
[21527]301                    'host' => $this->host,
302                    'port' => $this->port,
303                );
[21526]304                break;
[16503]305        }
[16669]306        return $arrParams;
[16503]307    }
308}
Note: See TracBrowser for help on using the repository browser.