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

Revision 22206, 11.2 KB checked in by kim, 11 years ago (diff)

#2003 copyrightを2013に更新

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