source: branches/version-2_12-multilang/data/class/SC_SendMail.php @ 22268

Revision 22268, 11.1 KB checked in by m_uehara, 11 years ago (diff)

#2030 メールの文字コード・コンテンツタイプを定数に差し替えました。

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