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

Revision 21987, 11.0 KB checked in by Seasoft, 12 years ago (diff)

#1912 (SC_SendMail#setHost SC_SendMail#setPort が保守されていない)

  • 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
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2012 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
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.
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 __construct() {
38        $this->arrRecip = array();
39        $this->to = '';
40        $this->subject = '';
41        $this->body = '';
42        $this->cc = '';
43        $this->bcc = '';
44        $this->replay_to = '';
45        $this->return_path = '';
46        $this->backend = MAIL_BACKEND;
47        $this->host = SMTP_HOST;
48        $this->port = SMTP_PORT;
49        mb_language('Japanese');
50
51        //-- PEAR::Mailを使ってメール送信オブジェクト作成
52        $this->objMail =& Mail::factory($this->backend,
53                                        $this->getBackendParams($this->backend));
54    }
55
56    // 送信先の設定
57    function setRecip($key, $recipient) {
58        $this->arrRecip[$key] = $recipient;
59    }
60
61    // 宛先の設定
62    function setTo($to, $to_name = '') {
63        if ($to != '') {
64            $this->to = $this->getNameAddress($to_name, $to);
65            $this->setRecip('To', $to);
66        }
67    }
68
69    // 送信元の設定
70    function setFrom($from, $from_name = '') {
71        $this->from = $this->getNameAddress($from_name, $from);
72    }
73
74    // CCの設定
75    function setCc($cc, $cc_name = '') {
76        if ($cc != '') {
77            $this->cc = $this->getNameAddress($cc_name, $cc);
78            $this->setRecip('Cc', $cc);
79        }
80    }
81
82    // BCCの設定
83    function setBCc($bcc) {
84        if ($bcc != '') {
85            $this->bcc = $bcc;
86            $this->setRecip('Bcc', $bcc);
87        }
88    }
89
90    // Reply-Toの設定
91    function setReplyTo($reply_to) {
92        if ($reply_to != '') {
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) {
104        $this->subject = mb_encode_mimeheader($subject, 'JIS', 'B', "\n");
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);
108    }
109
110    // 本文の設定
111    function setBody($body) {
112        // iso-2022-jpだと特殊文字が?で送信されるのでJISを使用する
113        $this->body = mb_convert_encoding($body, 'JIS', CHAR_CODE);
114    }
115
116    /**
117     * 前方互換用
118     *
119     * @deprecated 2.12.2 (#1912)
120     */
121    function setHost($host) {
122        trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING);
123        $this->host = $host;
124        $arrHost = array(
125                'host' => $this->host,
126                'port' => $this->port
127        );
128        //-- PEAR::Mailを使ってメール送信オブジェクト作成
129        $this->objMail =& Mail::factory('smtp', $arrHost);
130
131    }
132
133    /**
134     * 前方互換用
135     *
136     * @deprecated 2.12.2 (#1912)
137     */
138    function setPort($port) {
139        trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING);
140        $this->port = $port;
141        $arrHost = array(
142                'host' => $this->host,
143                'port' => $this->port
144        );
145        //-- PEAR::Mailを使ってメール送信オブジェクト作成
146        $this->objMail =& Mail::factory('smtp', $arrHost);
147    }
148
149    // 名前<メールアドレス>の形式を生成
150    function getNameAddress($name, $mail_address) {
151            if ($name != '') {
152                // 制御文字を変換する。
153                $_name = $name;
154                $_name = mb_encode_mimeheader($_name, 'JIS', 'B', "\n");
155                $_name = str_replace('"', '\"', $_name);
156                $name_address = sprintf('"%s" <%s>', $_name, $mail_address);
157            } else {
158                $name_address = $mail_address;
159            }
160            return $name_address;
161    }
162
163    function setItem($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') {
164        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
165    }
166
167    function setItemHtml($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') {
168        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
169    }
170
171    /*  ヘッダ等を格納
172         $to            -> 送信先メールアドレス
173         $subject       -> メールのタイトル
174         $body          -> メール本文
175         $fromaddress   -> 送信元のメールアドレス
176         $header        -> ヘッダー
177         $from_name     -> 送信元の名前(全角OK)
178         $reply_to      -> reply_to設定
179         $return_path   -> return-pathアドレス設定(エラーメール返送用)
180         $cc            -> カーボンコピー
181         $bcc           -> ブラインドカーボンコピー
182    */
183    function setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') {
184        // 宛先設定
185        $this->setTo($to);
186        // 件名設定
187        $this->setSubject($subject);
188        // 本文設定
189        $this->setBody($body);
190        // 送信元設定
191        $this->setFrom($fromaddress, $from_name);
192        // 返信先設定
193        $this->setReplyTo($reply_to);
194        // CC設定
195        $this->setCc($cc);
196        // BCC設定
197        $this->setBcc($bcc);
198
199        // Errors-Toは、ほとんどのSMTPで無視され、Return-Pathが優先されるためReturn_Pathに設定する。
200        if ($errors_to != '') {
201            $this->return_path = $errors_to;
202        } else if ($return_path != '') {
203            $this->return_path = $return_path;
204        } else {
205            $this->return_path = $fromaddress;
206        }
207    }
208
209    // ヘッダーを返す
210    function getBaseHeader() {
211        //-- 送信するメールの内容と送信先
212        $arrHeader = array();
213        $arrHeader['MIME-Version'] = '1.0';
214        $arrHeader['To'] = $this->to;
215        $arrHeader['Subject'] = $this->subject;
216        $arrHeader['From'] = $this->from;
217        $arrHeader['Return-Path'] = $this->return_path;
218        if ($this->reply_to != '') {
219            $arrHeader['Reply-To'] = $this->reply_to;
220        }
221        if ($this->cc != '') {
222            $arrHeader['Cc'] = $this->cc;
223        }
224        if ($this->bcc != '') {
225            $arrHeader['Bcc'] = $this->bcc;
226        }
227        $arrHeader['Date'] = date('D, j M Y H:i:s O');
228        $arrHeader['Content-Transfer-Encoding'] = '7bit';
229        return $arrHeader;
230    }
231
232    // ヘッダーを返す
233    function getTEXTHeader() {
234        $arrHeader = $this->getBaseHeader();
235        $arrHeader['Content-Type'] = 'text/plain; charset="ISO-2022-JP"';
236        return $arrHeader;
237    }
238
239    // ヘッダーを返す
240    function getHTMLHeader() {
241        $arrHeader = $this->getBaseHeader();
242        $arrHeader['Content-Type'] = 'text/html; charset="ISO-2022-JP"';
243        return $arrHeader;
244    }
245
246    /**
247     * メーラーバックエンドに応じた送信先を返す
248     *
249     * @return array|string メーラーバックエンドに応じた送信先
250     */
251    function getRecip() {
252        switch ($this->backend) {
253            // PEAR::Mail_mail#send は、(他のメーラーバックエンドと異なり) 第1引数を To: として扱う。Cc: や Bcc: は、ヘッダー情報から処理する。
254            case 'mail':
255                return $this->to;
256
257            case 'sendmail':
258            case 'smtp':
259            default:
260                return $this->arrRecip;
261        }
262    }
263
264    /**
265     * TXTメール送信を実行する.
266     *
267     * 設定された情報を利用して, メールを送信する.
268     *
269     * @return void
270     */
271    function sendMail($isHtml = false) {
272        $header = $isHtml ? $this->getHTMLHeader() : $this->getTEXTHeader();
273        $recip = $this->getRecip();
274        // メール送信
275        $result = $this->objMail->send($recip, $header, $this->body);
276        if (PEAR::isError($result)) {
277            // XXX Windows 環境では SJIS でメッセージを受け取るようなので変換する。
278            $msg = mb_convert_encoding($result->getMessage(), CHAR_CODE, 'auto');
279            $msg = 'メール送信に失敗しました。[' . $msg . ']';
280            trigger_error($msg, E_USER_WARNING);
281            GC_Utils_Ex::gfDebugLog($header);
282            return false;
283        }
284        return true;
285    }
286
287    /**
288     * HTMLメール送信を実行する.
289     *
290     * @return void
291     */
292    function sendHtmlMail() {
293        return $this->sendMail(true);
294    }
295
296    /**
297     * メーラーバックエンドに応じたパラメーターを返す.
298     *
299     * @param string $backend Pear::Mail のバックエンド
300     * @return array メーラーバックエンドに応じたパラメーターの配列
301     */
302    function getBackendParams($backend) {
303        switch ($backend) {
304            case 'mail':
305                $arrParams = array();
306                break;
307            case 'sendmail':
308                $arrParams = array(
309                    'sendmail_path' => '/usr/bin/sendmail',
310                    'sendmail_args' => '-i',
311                );
312                break;
313            case 'smtp':
314            default:
315                $arrParams = array(
316                    'host' => $this->host,
317                    'port' => $this->port,
318                );
319                if (defined('SMTP_USER')
320                    && defined('SMTP_PASSWORD')
321                    && !SC_Utils_Ex::isBlank(SMTP_USER)
322                    && !SC_Utils_Ex::isBlank(SMTP_PASSWORD)) {
323                    $arrParams['auth'] = true;
324                    $arrParams['username'] = SMTP_USER;
325                    $arrParams['password'] = SMTP_PASSWORD;
326                }
327                break;
328        }
329        return $arrParams;
330    }
331}
Note: See TracBrowser for help on using the repository browser.