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

Revision 21751, 9.9 KB checked in by shutta, 12 years ago (diff)

#1613 typo修正・ソース整形・ソースコメントの改善
インデントのズレを修正。

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