source: branches/version-2_5-dev/data/class/SC_SendMail.php @ 18866

Revision 18866, 10.6 KB checked in by Seasoft, 16 years ago (diff)

#837(SC_SendMail#sendMail と SC_SendMail#sendHtmlMail で複数宛先の扱い方が異なる)改修

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