source: branches/version-2/data/class/SC_SendMail.php @ 17092

Revision 17092, 10.1 KB checked in by satou, 16 years ago (diff)

#234 メールバックエンドによるメール配信の不具合を修正

  • 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-2007 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
24require_once(dirname(__FILE__) . '/../module/Mail.php');
25require_once(dirname(__FILE__) . '/../module/Mail/mime.php');
26
27//--- テキスト/HTML メール送信
28class SC_SendMail {
29
30    var $to;            //  送信先
31    var $subject;       //  題名
32    var $body;          //  本文
33    var $cc;            // CC
34    var $bcc;           // BCC
35    var $replay_to;     // replay_to
36    var $return_path;   // return_path
37    var $arrEncode;
38    var $objMailMime;
39    var $arrTEXTEncode;
40    var $arrHTMLEncode;
41    var $objMail;
42
43    // コンストラクタ
44    function SC_SendMail() {
45        $this->arrRecip = array();
46        $this->to = "";
47        $this->subject = "";
48        $this->body = "";
49        $this->cc = "";
50        $this->bcc = "";
51        $this->replay_to = "";
52        $this->return_path = "";
53        $this->arrEncode = array();
54        $this->backend = MAIL_BACKEND;
55        $this->host = SMTP_HOST;
56        $this->port = SMTP_PORT;
57        $this->objMailMime = new Mail_mime();
58        mb_language( "Japanese" );
59
60        //-- PEAR::Mailを使ってメール送信オブジェクト作成
61        $this->objMail =& Mail::factory($this->backend,
62                                        $this->getBackendParams($this->backend));
63    }
64
65    // 送信先の設定
66    function setRecip($key, $recipient) {
67        $this->arrRecip[$key] = $recipient;
68    }
69   
70    // 宛先の設定
71    function setTo($to, $to_name = "") {
72        if($to != "") {
73            $this->to = $this->getNameAddress($to_name, $to);
74            $this->setRecip("To", $to);
75        }
76    }
77
78    // 送信元の設定
79    function setFrom($from, $from_name = "") {
80        $this->from = $this->getNameAddress($from_name, $from);
81    }
82
83    // CCの設定
84    function setCc($cc, $cc_name = "") {
85        if($cc != "") {
86            $this->cc = $this->getNameAddress($cc_name, $cc);
87            $this->setRecip("Cc", $cc);
88        }
89    }
90
91    // BCCの設定
92    function setBCc($bcc) {
93        if($bcc != "") {
94            $this->bcc = $bcc;
95            $this->setRecip("Bcc", $bcc);
96        }
97    }
98
99    // Reply-Toの設定
100    function setReplyTo($reply_to) {
101        if($reply_to != "") {
102            $this->reply_to = $reply_to;
103        }
104    }
105
106    // Return-Pathの設定
107    function setReturnPath($return_path) {
108        $this->return_path = $return_path;
109    }
110
111    // 件名の設定
112    function setSubject($subject) {
113        $this->subject = mb_encode_mimeheader($subject);
114    }
115
116    // 本文の設定
117    function setBody($body) {
118        $this->body = mb_convert_encoding($body, "JIS", CHAR_CODE);
119    }
120
121    // SMTPサーバの設定
122    function setHost($host) {
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    // SMTPポートの設定
134    function setPort($port) {
135        $this->port = $port;
136        $arrHost = array(
137                'host' => $this->host,
138                'port' => $this->port
139        );
140        //-- PEAR::Mailを使ってメール送信オブジェクト作成
141        $this->objMail =& Mail::factory("smtp", $arrHost);
142    }
143
144    // 名前<メールアドレス>の形式を生成
145    function getNameAddress($name, $mail_address) {
146            if($name != "") {
147                // 制御文字を変換する。
148                $_name = $name;
149                $_name = ereg_replace("<","<", $_name);
150                $_name = ereg_replace(">",">", $_name);
151                if(OS_TYPE != 'WIN') {
152                    // windowsでは文字化けするので使用しない。
153                    $_name = mb_convert_encoding($_name,"JIS",CHAR_CODE);
154                }
155                $_name = mb_encode_mimeheader($_name);
156                $name_address = "\"". $_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        // 本文設定(iso-2022-jpだと特殊文字が?で送信されるのでJISを使用する)
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['MIME-Version'] = '1.0';
213        $arrHeader['To'] = $this->to;
214        $arrHeader['Subject'] = $this->subject;
215        $arrHeader['From'] = $this->from;
216        $arrHeader['Return-Path'] = $this->return_path;
217        if($this->reply_to != "") {
218            $arrHeader['Reply-To'] = $this->reply_to;
219        }
220        if($this->cc != "") {
221            $arrHeader['Cc'] = $this->cc;
222        }
223        if($this->bcc != "") {
224            $arrHeader['Bcc'] = $this->bcc;
225        }
226        $arrHeader['Date'] = date("D, j M Y H:i:s O");
227        return $arrHeader;
228    }
229
230    // ヘッダーを返す
231    function getTEXTHeader() {
232        $arrHeader = $this->getBaseHeader();
233        $arrHeader['Content-Type'] = "text/plain; charset=\"ISO-2022-JP\"";
234        $arrHeader['Content-Transfer-Encoding'] = "7bit";
235        return $arrHeader;
236    }
237
238    // ヘッダーを返す
239    function getHTMLHeader() {
240        $arrHeader = $this->getBaseHeader();
241        $arrHeader['Content-Type'] = "text/html; charset=\"ISO-2022-JP\"";
242        $arrHeader['Content-Transfer-Encoding'] = "ISO-2022-JP";
243        return $arrHeader;
244    }
245   
246    // 送信先を返す
247    function getRecip() {
248        switch ($this->backend) {
249        case "mail":
250            return $this->to;
251            break;
252        case "sendmail":
253        case "smtp":
254        default:
255            return $this->arrRecip;
256            break;
257        }
258    }
259
260    //  TXTメール送信を実行する
261    function sendMail() {
262        $header = $this->getTEXTHeader();
263        // メール送信
264        $result = $this->objMail->send($this->getRecip(), $header, $this->body);
265        if (PEAR::isError($result)) {
266            GC_Utils_Ex::gfPrintLog($result->getMessage());
267            GC_Utils_Ex::gfDebugLog($header);
268            return false;
269        }
270        return true;
271    }
272
273    // HTMLメール送信を実行する
274    function sendHtmlMail() {
275        $header = $this->getHTMLHeader();
276        // メール送信
277        $result = $this->objMail->send($this->getRecip(), $header, $this->body);
278        if (PEAR::isError($result)) {
279            GC_Utils_Ex::gfPrintLog($result->getMessage());
280            GC_Utils_Ex::gfDebugLog($header);
281            return false;
282        }
283        return true;
284    }
285
286    /**
287     * メーラーバックエンドに応じたパラメータを返す.
288     *
289     * @param string $backend Pear::Mail のバックエンド
290     * @return array メーラーバックエンドに応じたパラメータの配列
291     */
292    function getBackendParams($backend) {
293        switch ($backend) {
294        case "mail":
295            $arrParams = array();
296            break;
297        case "sendmail":
298            $arrParams = array('sendmail_path' => '/usr/bin/sendmail',
299                               'sendmail_args' => '-i'
300                               );
301            break;
302        case "smtp":
303        default:
304            $arrParams = array(
305                               'host' => $this->host,
306                               'port' => $this->port
307                               );
308            break;
309        }
310        return $arrParams;
311    }
312}
313?>
Note: See TracBrowser for help on using the repository browser.