source: branches/version-2_13-dev/data/class/SC_SendMail.php @ 23605

Revision 23605, 11.8 KB checked in by kimoto, 12 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

Scrutinizer Auto-Fixes

This patch was automatically generated as part of the following inspection:
 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/d8722894-69a6-4b1b-898d-43618035c60d

Enabled analysis tools:

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