source: branches/version-2_13-dev/data/class/SC_SendMail.php @ 22857

Revision 22857, 11.4 KB checked in by Seasoft, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.13.0)

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