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

Revision 21983, 10.7 KB checked in by Seasoft, 12 years ago (diff)

#1906 (SC_SendMail#sendMail のエラー出力が不適切)

  • 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 *
[21867]5 * Copyright(c) 2000-2012 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    // コンストラクタ
[21767]37    function __construct() {
[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");
[21753]145                $_name = str_replace('"', '\"', $_name);
146                $name_address = sprintf('"%s" <%s>', $_name, $mail_address);
[16503]147            } else {
148                $name_address = $mail_address;
149            }
150            return $name_address;
151    }
152
[21515]153    function setItem($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') {
[16503]154        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
155    }
156
[21515]157    function setItemHtml($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') {
[16503]158        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
159    }
160
161    /*  ヘッダ等を格納
162         $to            -> 送信先メールアドレス
163         $subject       -> メールのタイトル
164         $body          -> メール本文
165         $fromaddress   -> 送信元のメールアドレス
166         $header        -> ヘッダー
167         $from_name     -> 送信元の名前(全角OK)
168         $reply_to      -> reply_to設定
169         $return_path   -> return-pathアドレス設定(エラーメール返送用)
170         $cc            -> カーボンコピー
171         $bcc           -> ブラインドカーボンコピー
172    */
[21515]173    function setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') {
[16503]174        // 宛先設定
[17109]175        $this->setTo($to);
[16503]176        // 件名設定
177        $this->setSubject($subject);
[21632]178        // 本文設定
[16503]179        $this->setBody($body);
180        // 送信元設定
181        $this->setFrom($fromaddress, $from_name);
182        // 返信先設定
183        $this->setReplyTo($reply_to);
184        // CC設定
185        $this->setCc($cc);
186        // BCC設定
187        $this->setBcc($bcc);
188
189        // Errors-Toは、ほとんどのSMTPで無視され、Return-Pathが優先されるためReturn_Pathに設定する。
[21514]190        if ($errors_to != '') {
[16503]191            $this->return_path = $errors_to;
[21514]192        } else if ($return_path != '') {
[16503]193            $this->return_path = $return_path;
194        } else {
195            $this->return_path = $fromaddress;
196        }
197    }
198
199    // ヘッダーを返す
200    function getBaseHeader() {
201        //-- 送信するメールの内容と送信先
[21926]202        $arrHeader = array();
[16503]203        $arrHeader['MIME-Version'] = '1.0';
204        $arrHeader['To'] = $this->to;
205        $arrHeader['Subject'] = $this->subject;
206        $arrHeader['From'] = $this->from;
207        $arrHeader['Return-Path'] = $this->return_path;
[21514]208        if ($this->reply_to != '') {
[16503]209            $arrHeader['Reply-To'] = $this->reply_to;
210        }
[21514]211        if ($this->cc != '') {
[16503]212            $arrHeader['Cc'] = $this->cc;
213        }
[21514]214        if ($this->bcc != '') {
[16503]215            $arrHeader['Bcc'] = $this->bcc;
216        }
[21514]217        $arrHeader['Date'] = date('D, j M Y H:i:s O');
[21632]218        $arrHeader['Content-Transfer-Encoding'] = '7bit';
[16503]219        return $arrHeader;
220    }
221
222    // ヘッダーを返す
223    function getTEXTHeader() {
224        $arrHeader = $this->getBaseHeader();
[21634]225        $arrHeader['Content-Type'] = 'text/plain; charset="ISO-2022-JP"';
[16503]226        return $arrHeader;
227    }
228
229    // ヘッダーを返す
230    function getHTMLHeader() {
231        $arrHeader = $this->getBaseHeader();
[21634]232        $arrHeader['Content-Type'] = 'text/html; charset="ISO-2022-JP"';
[16503]233        return $arrHeader;
234    }
[20540]235
[18866]236    /**
237     * メーラーバックエンドに応じた送信先を返す
238     *
239     * @return array|string メーラーバックエンドに応じた送信先
240     */
[17109]241    function getRecip() {
242        switch ($this->backend) {
[18866]243            // PEAR::Mail_mail#send は、(他のメーラーバックエンドと異なり) 第1引数を To: として扱う。Cc: や Bcc: は、ヘッダー情報から処理する。
[21526]244            case 'mail':
245                return $this->to;
246
247            case 'sendmail':
248            case 'smtp':
249            default:
250                return $this->arrRecip;
[17109]251        }
252    }
[16503]253
[18667]254    /**
255     * TXTメール送信を実行する.
256     *
257     * 設定された情報を利用して, メールを送信する.
258     *
259     * @return void
260     */
[18866]261    function sendMail($isHtml = false) {
262        $header = $isHtml ? $this->getHTMLHeader() : $this->getTEXTHeader();
[18667]263        $recip = $this->getRecip();
[16503]264        // メール送信
[18866]265        $result = $this->objMail->send($recip, $header, $this->body);
[21751]266        if (PEAR::isError($result)) {
[21983]267            // XXX Windows 環境では SJIS でメッセージを受け取るようなので変換する。
268            $msg = mb_convert_encoding($result->getMessage(), CHAR_CODE, 'auto');
269            $msg = 'メール送信に失敗しました。[' . $msg . ']';
270            trigger_error($msg, E_USER_WARNING);
[21751]271            GC_Utils_Ex::gfDebugLog($header);
272            return false;
273        }
[18866]274        return true;
[16503]275    }
276
[18866]277    /**
278     * HTMLメール送信を実行する.
279     *
280     * @return void
281     */
[16503]282    function sendHtmlMail() {
[20539]283        return $this->sendMail(true);
[16503]284    }
285
286    /**
[20970]287     * メーラーバックエンドに応じたパラメーターを返す.
[16503]288     *
289     * @param string $backend Pear::Mail のバックエンド
[20970]290     * @return array メーラーバックエンドに応じたパラメーターの配列
[16503]291     */
292    function getBackendParams($backend) {
293        switch ($backend) {
[21526]294            case 'mail':
295                $arrParams = array();
296                break;
297            case 'sendmail':
[21527]298                $arrParams = array(
299                    'sendmail_path' => '/usr/bin/sendmail',
300                    'sendmail_args' => '-i',
301                );
[21526]302                break;
303            case 'smtp':
304            default:
305                $arrParams = array(
[21527]306                    'host' => $this->host,
307                    'port' => $this->port,
308                );
[21827]309                if (defined('SMTP_USER')
310                    && defined('SMTP_PASSWORD')
311                    && !SC_Utils_Ex::isBlank(SMTP_USER)
312                    && !SC_Utils_Ex::isBlank(SMTP_PASSWORD)) {
313                    $arrParams['auth'] = true;
314                    $arrParams['username'] = SMTP_USER;
315                    $arrParams['password'] = SMTP_PASSWORD;
316                }
[21526]317                break;
[16503]318        }
[16669]319        return $arrParams;
[16503]320    }
321}
Note: See TracBrowser for help on using the repository browser.