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

Revision 22567, 11.3 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

  • 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
RevLine 
[16503]1<?php
2/*
[16582]3 * This file is part of EC-CUBE
4 *
[22206]5 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
[16503]6 *
7 * http://www.lockon.co.jp/
[16582]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.
[16503]22 */
23
[21989]24// テキスト/HTML メール送信
[22567]25class SC_SendMail
26{
[16503]27
[21989]28    var $to;            // 送信先
29    var $subject;       // 題名
30    var $body;          // 本文
[16503]31    var $cc;            // CC
32    var $bcc;           // BCC
33    var $replay_to;     // replay_to
34    var $return_path;   // return_path
35    var $objMail;
36
[21989]37    /**
38     * コンストラクタ
39     *
40     * @return void
41     */
[22567]42    function __construct()
43    {
[17109]44        $this->arrRecip = array();
[21514]45        $this->to = '';
46        $this->subject = '';
47        $this->body = '';
48        $this->cc = '';
49        $this->bcc = '';
50        $this->replay_to = '';
51        $this->return_path = '';
[16503]52        $this->backend = MAIL_BACKEND;
53        $this->host = SMTP_HOST;
54        $this->port = SMTP_PORT;
55
[21989]56        // PEAR::Mailを使ってメール送信オブジェクト作成
[16503]57        $this->objMail =& Mail::factory($this->backend,
58                                        $this->getBackendParams($this->backend));
[21989]59        if (PEAR::isError($this->objMail)) {
60            // XXX 環境によっては文字エンコードに差異がないか些か心配
61            trigger_error($this->objMail->getMessage(), E_USER_ERROR);
62        }
[16503]63    }
64
[17109]65    // 送信先の設定
[22567]66    function setRecip($key, $recipient)
67    {
[17109]68        $this->arrRecip[$key] = $recipient;
69    }
[20540]70
[16503]71    // 宛先の設定
[22567]72    function setTo($to, $to_name = '')
73    {
[21514]74        if ($to != '') {
[17109]75            $this->to = $this->getNameAddress($to_name, $to);
[20538]76            $this->setRecip('To', $to);
[17109]77        }
[16503]78    }
79
80    // 送信元の設定
[22567]81    function setFrom($from, $from_name = '')
82    {
[16503]83        $this->from = $this->getNameAddress($from_name, $from);
84    }
85
86    // CCの設定
[22567]87    function setCc($cc, $cc_name = '')
88    {
[21514]89        if ($cc != '') {
[16503]90            $this->cc = $this->getNameAddress($cc_name, $cc);
[20538]91            $this->setRecip('Cc', $cc);
[16503]92        }
93    }
94
95    // BCCの設定
[22567]96    function setBCc($bcc)
97    {
[21514]98        if ($bcc != '') {
[16503]99            $this->bcc = $bcc;
[20538]100            $this->setRecip('Bcc', $bcc);
[16503]101        }
102    }
103
104    // Reply-Toの設定
[22567]105    function setReplyTo($reply_to)
106    {
[21514]107        if ($reply_to != '') {
[16503]108            $this->reply_to = $reply_to;
109        }
110    }
111
112    // Return-Pathの設定
[22567]113    function setReturnPath($return_path)
114    {
[16503]115        $this->return_path = $return_path;
116    }
117
118    // 件名の設定
[22567]119    function setSubject($subject)
120    {
[20538]121        $this->subject = mb_encode_mimeheader($subject, 'JIS', 'B', "\n");
[21989]122        $this->subject = str_replace(array("\r\n", "\r"), "\n", $this->subject);
[16503]123    }
124
125    // 本文の設定
[22567]126    function setBody($body)
127    {
[21632]128        // iso-2022-jpだと特殊文字が?で送信されるのでJISを使用する
[20538]129        $this->body = mb_convert_encoding($body, 'JIS', CHAR_CODE);
[16503]130    }
131
[21987]132    /**
133     * 前方互換用
134     *
135     * @deprecated 2.12.2 (#1912)
136     */
[22567]137    function setHost($host)
138    {
[21987]139        trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING);
[16503]140        $this->host = $host;
141        $arrHost = array(
142                'host' => $this->host,
143                'port' => $this->port
144        );
[21989]145        // PEAR::Mailを使ってメール送信オブジェクト作成
[20538]146        $this->objMail =& Mail::factory('smtp', $arrHost);
[16503]147
148    }
149
[21987]150    /**
151     * 前方互換用
152     *
153     * @deprecated 2.12.2 (#1912)
154     */
[22567]155    function setPort($port)
156    {
[21987]157        trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING);
[16503]158        $this->port = $port;
159        $arrHost = array(
160                'host' => $this->host,
161                'port' => $this->port
162        );
[21989]163        // PEAR::Mailを使ってメール送信オブジェクト作成
[20538]164        $this->objMail =& Mail::factory('smtp', $arrHost);
[16503]165    }
166
167    // 名前<メールアドレス>の形式を生成
[22567]168    function getNameAddress($name, $mail_address)
169    {
[21514]170            if ($name != '') {
[16503]171                // 制御文字を変換する。
172                $_name = $name;
[20538]173                $_name = mb_encode_mimeheader($_name, 'JIS', 'B', "\n");
[21753]174                $_name = str_replace('"', '\"', $_name);
175                $name_address = sprintf('"%s" <%s>', $_name, $mail_address);
[16503]176            } else {
177                $name_address = $mail_address;
178            }
179            return $name_address;
180    }
181
[22567]182    function setItem($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='')
183    {
[16503]184        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
185    }
186
[22567]187    function setItemHtml($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='')
188    {
[16503]189        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
190    }
191
192    /*  ヘッダ等を格納
193         $to            -> 送信先メールアドレス
194         $subject       -> メールのタイトル
195         $body          -> メール本文
196         $fromaddress   -> 送信元のメールアドレス
197         $header        -> ヘッダー
198         $from_name     -> 送信元の名前(全角OK)
199         $reply_to      -> reply_to設定
200         $return_path   -> return-pathアドレス設定(エラーメール返送用)
201         $cc            -> カーボンコピー
202         $bcc           -> ブラインドカーボンコピー
203    */
[22567]204    function setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='')
205    {
[16503]206        // 宛先設定
[17109]207        $this->setTo($to);
[16503]208        // 件名設定
209        $this->setSubject($subject);
[21632]210        // 本文設定
[16503]211        $this->setBody($body);
212        // 送信元設定
213        $this->setFrom($fromaddress, $from_name);
214        // 返信先設定
215        $this->setReplyTo($reply_to);
216        // CC設定
217        $this->setCc($cc);
218        // BCC設定
219        $this->setBcc($bcc);
220
221        // Errors-Toは、ほとんどのSMTPで無視され、Return-Pathが優先されるためReturn_Pathに設定する。
[21514]222        if ($errors_to != '') {
[16503]223            $this->return_path = $errors_to;
[21514]224        } else if ($return_path != '') {
[16503]225            $this->return_path = $return_path;
226        } else {
227            $this->return_path = $fromaddress;
228        }
229    }
230
231    // ヘッダーを返す
[22567]232    function getBaseHeader()
233    {
[21989]234        // 送信するメールの内容と送信先
[21926]235        $arrHeader = array();
[16503]236        $arrHeader['MIME-Version'] = '1.0';
237        $arrHeader['To'] = $this->to;
238        $arrHeader['Subject'] = $this->subject;
239        $arrHeader['From'] = $this->from;
240        $arrHeader['Return-Path'] = $this->return_path;
[21514]241        if ($this->reply_to != '') {
[16503]242            $arrHeader['Reply-To'] = $this->reply_to;
243        }
[21514]244        if ($this->cc != '') {
[16503]245            $arrHeader['Cc'] = $this->cc;
246        }
[21514]247        if ($this->bcc != '') {
[16503]248            $arrHeader['Bcc'] = $this->bcc;
249        }
[21514]250        $arrHeader['Date'] = date('D, j M Y H:i:s O');
[21632]251        $arrHeader['Content-Transfer-Encoding'] = '7bit';
[16503]252        return $arrHeader;
253    }
254
255    // ヘッダーを返す
[22567]256    function getTEXTHeader()
257    {
[16503]258        $arrHeader = $this->getBaseHeader();
[21634]259        $arrHeader['Content-Type'] = 'text/plain; charset="ISO-2022-JP"';
[16503]260        return $arrHeader;
261    }
262
263    // ヘッダーを返す
[22567]264    function getHTMLHeader()
265    {
[16503]266        $arrHeader = $this->getBaseHeader();
[21634]267        $arrHeader['Content-Type'] = 'text/html; charset="ISO-2022-JP"';
[16503]268        return $arrHeader;
269    }
[20540]270
[18866]271    /**
272     * メーラーバックエンドに応じた送信先を返す
273     *
274     * @return array|string メーラーバックエンドに応じた送信先
275     */
[22567]276    function getRecip()
277    {
[17109]278        switch ($this->backend) {
[18866]279            // PEAR::Mail_mail#send は、(他のメーラーバックエンドと異なり) 第1引数を To: として扱う。Cc: や Bcc: は、ヘッダー情報から処理する。
[21526]280            case 'mail':
281                return $this->to;
282
283            case 'sendmail':
284            case 'smtp':
285            default:
286                return $this->arrRecip;
[17109]287        }
288    }
[16503]289
[18667]290    /**
291     * TXTメール送信を実行する.
292     *
293     * 設定された情報を利用して, メールを送信する.
294     *
295     * @return void
296     */
[22567]297    function sendMail($isHtml = false)
298    {
[18866]299        $header = $isHtml ? $this->getHTMLHeader() : $this->getTEXTHeader();
[18667]300        $recip = $this->getRecip();
[16503]301        // メール送信
[18866]302        $result = $this->objMail->send($recip, $header, $this->body);
[21751]303        if (PEAR::isError($result)) {
[21983]304            // XXX Windows 環境では SJIS でメッセージを受け取るようなので変換する。
305            $msg = mb_convert_encoding($result->getMessage(), CHAR_CODE, 'auto');
306            $msg = 'メール送信に失敗しました。[' . $msg . ']';
307            trigger_error($msg, E_USER_WARNING);
[21751]308            GC_Utils_Ex::gfDebugLog($header);
309            return false;
310        }
[18866]311        return true;
[16503]312    }
313
[18866]314    /**
315     * HTMLメール送信を実行する.
316     *
317     * @return void
318     */
[22567]319    function sendHtmlMail()
320    {
[20539]321        return $this->sendMail(true);
[16503]322    }
323
324    /**
[20970]325     * メーラーバックエンドに応じたパラメーターを返す.
[16503]326     *
327     * @param string $backend Pear::Mail のバックエンド
[20970]328     * @return array メーラーバックエンドに応じたパラメーターの配列
[16503]329     */
[22567]330    function getBackendParams($backend)
331    {
[16503]332        switch ($backend) {
[21526]333            case 'mail':
334                $arrParams = array();
335                break;
[21989]336
[21526]337            case 'sendmail':
[21527]338                $arrParams = array(
339                    'sendmail_path' => '/usr/bin/sendmail',
340                    'sendmail_args' => '-i',
341                );
[21526]342                break;
[21989]343
[21526]344            case 'smtp':
345                $arrParams = array(
[21527]346                    'host' => $this->host,
347                    'port' => $this->port,
348                );
[21827]349                if (defined('SMTP_USER')
350                    && defined('SMTP_PASSWORD')
351                    && !SC_Utils_Ex::isBlank(SMTP_USER)
352                    && !SC_Utils_Ex::isBlank(SMTP_PASSWORD)) {
353                    $arrParams['auth'] = true;
354                    $arrParams['username'] = SMTP_USER;
355                    $arrParams['password'] = SMTP_PASSWORD;
356                }
[21526]357                break;
[21989]358
359            default:
360                trigger_error('不明なバックエンド。[$backend = ' . var_export($backend, true) . ']', E_USER_ERROR);
361                exit;
[16503]362        }
[16669]363        return $arrParams;
[16503]364    }
365}
Note: See TracBrowser for help on using the repository browser.