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

Revision 22752, 11.4 KB checked in by habu, 11 years ago (diff)

#2203 メールをOutlookで閲覧すると、行間隔が異常に広い

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