source: branches/comu-ver2/data/class/SC_SendMail.php @ 17568

Revision 17568, 10.4 KB checked in by Seasoft, 16 years ago (diff)

merge 17212,17213,17214,17216,17218,17219,17220

  • 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, "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    function getRecip() {
251        switch ($this->backend) {
252        case "mail":
253            return $this->to;
254            break;
255        case "sendmail":
256        case "smtp":
257        default:
258            return $this->arrRecip;
259            break;
260        }
261    }
262
263    //  TXTメール送信を実行する
264    function sendMail() {
265        $header = $this->getTEXTHeader();
266        // メール送信
267        $result = $this->objMail->send($this->getRecip(), $header, $this->body);
268        if (PEAR::isError($result)) {
269            GC_Utils_Ex::gfPrintLog($result->getMessage());
270            GC_Utils_Ex::gfDebugLog($header);
271            return false;
272        }
273        return true;
274    }
275
276    // HTMLメール送信を実行する
277    function sendHtmlMail() {
278        $header = $this->getHTMLHeader();
279        // メール送信
280        $result = $this->objMail->send($this->getRecip(), $header, $this->body);
281        if (PEAR::isError($result)) {
282            GC_Utils_Ex::gfPrintLog($result->getMessage());
283            GC_Utils_Ex::gfDebugLog($header);
284            return false;
285        }
286        return true;
287    }
288
289    /**
290     * メーラーバックエンドに応じたパラメータを返す.
291     *
292     * @param string $backend Pear::Mail のバックエンド
293     * @return array メーラーバックエンドに応じたパラメータの配列
294     */
295    function getBackendParams($backend) {
296        switch ($backend) {
297        case "mail":
298            $arrParams = array();
299            break;
300        case "sendmail":
301            $arrParams = array('sendmail_path' => '/usr/bin/sendmail',
302                               'sendmail_args' => '-i'
303                               );
304            break;
305        case "smtp":
306        default:
307            $arrParams = array(
308                               'host' => $this->host,
309                               'port' => $this->port
310                               );
311            break;
312        }
313        return $arrParams;
314    }
315}
316?>
Note: See TracBrowser for help on using the repository browser.