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

Revision 19670, 10.4 KB checked in by nanasess, 13 years ago (diff)

スマートフォン対応(#787)

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