source: branches/version-2_13-dev/data/class/helper/SC_Helper_Mail.php @ 23008

Revision 23008, 19.4 KB checked in by shutta, 11 years ago (diff)

#2312 (SC_Helper_Mail::sfMakeSubject()の引数が足りなくてWarningが出る)
第2引数を省略できるように修正。

  • 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/**
25 * メール関連 のヘルパークラス.
26 *
27 * @package Helper
28 * @author LOCKON CO.,LTD.
29 * @version $Id$
30 */
31class SC_Helper_Mail
32{
33    /** メールテンプレートのパス */
34    var $arrMAILTPLPATH;
35
36    /**
37     * LC_Pageオブジェクト.
38     *
39     * @var LC_Page
40     */
41    protected $objPage;
42
43    /**
44     * コンストラクタ.
45     */
46    function __construct()
47    {
48        $masterData = new SC_DB_MasterData_Ex();
49        $this->arrMAILTPLPATH =  $masterData->getMasterData('mtb_mail_tpl_path');
50        $this->arrPref = $masterData->getMasterData('mtb_pref');
51    }
52
53    /**
54     * LC_Pageオブジェクトをセットします.
55     *
56     * @param LC_Page $objPage
57     */
58    function setPage(LC_Page $objPage)
59    {
60        $this->objPage = $objPage;
61    }
62
63    /**
64     * LC_Pageオブジェクトを返します.
65     *
66     * @return LC_Page
67     */
68    function getPage()
69    {
70        return $this->objPage;
71    }
72
73    /* DBに登録されたテンプレートメールの送信 */
74    function sfSendTemplateMail($to, $to_name, $template_id, &$objPage, $from_address = '', $from_name = '', $reply_to = '', $bcc = '')
75    {
76        // メールテンプレート情報の取得
77        $objMailtemplate = new SC_Helper_Mailtemplate_Ex();
78        $mailtemplate = $objMailtemplate->get($template_id);
79        $objPage->tpl_header = $mailtemplate['header'];
80        $objPage->tpl_footer = $mailtemplate['footer'];
81        $tmp_subject = $mailtemplate['subject'];
82
83        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
84
85        $objMailView = new SC_SiteView_Ex();
86        $objMailView->setPage($this->getPage());
87        // メール本文の取得
88        $objMailView->assignobj($objPage);
89        $body = $objMailView->fetch($this->arrMAILTPLPATH[$template_id]);
90
91        // メール送信処理
92        $objSendMail = new SC_SendMail_Ex();
93        if ($from_address == '') $from_address = $arrInfo['email03'];
94        if ($from_name == '') $from_name = $arrInfo['shop_name'];
95        if ($reply_to == '') $reply_to = $arrInfo['email03'];
96        $error = $arrInfo['email04'];
97        $tosubject = $this->sfMakeSubject($tmp_subject, $objMailView);
98
99        $objSendMail->setItem('', $tosubject, $body, $from_address, $from_name, $reply_to, $error, $error, $bcc);
100        $objSendMail->setTo($to, $to_name);
101        $objSendMail->sendMail();    // メール送信
102    }
103
104    /* 受注完了メール送信 */
105    function sfSendOrderMail($order_id, $template_id, $subject = '', $header = '', $footer = '', $send = true)
106    {
107        $arrTplVar = new stdClass();
108        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
109        $arrTplVar->arrInfo = $arrInfo;
110
111        $objQuery =& SC_Query_Ex::getSingletonInstance();
112
113        if ($subject == '' && $header == '' && $footer == '') {
114            // メールテンプレート情報の取得
115            $objMailtemplate = new SC_Helper_Mailtemplate_Ex();
116            $mailtemplate = $objMailtemplate->get($template_id);
117            $arrTplVar->tpl_header = $mailtemplate['header'];
118            $arrTplVar->tpl_footer = $mailtemplate['footer'];
119            $tmp_subject = $mailtemplate['subject'];
120        } else {
121            $arrTplVar->tpl_header = $header;
122            $arrTplVar->tpl_footer = $footer;
123            $tmp_subject = $subject;
124        }
125
126        // 受注情報の取得
127        $where = 'order_id = ? AND del_flg = 0';
128        $arrOrder = $objQuery->getRow('*', 'dtb_order', $where, array($order_id));
129
130        if (empty($arrOrder)) {
131            trigger_error("該当する受注が存在しない。(注文番号: $order_id)", E_USER_ERROR);
132        }
133
134        $where = 'order_id = ?';
135        $objQuery->setOrder('order_detail_id');
136        $arrTplVar->arrOrderDetail = $objQuery->select('*', 'dtb_order_detail', $where, array($order_id));
137
138        $objProduct = new SC_Product_Ex();
139        $objQuery->setOrder('shipping_id');
140        $arrRet = $objQuery->select('*', 'dtb_shipping', 'order_id = ?', array($order_id));
141        foreach ($arrRet as $key => $value) {
142            $objQuery->setOrder('shipping_id');
143            $arrItems = $objQuery->select('*', 'dtb_shipment_item', 'order_id = ? AND shipping_id = ?',
144                                          array($order_id, $arrRet[$key]['shipping_id']));
145            foreach ($arrItems as $arrDetail) {
146                foreach ($arrDetail as $detailKey => $detailVal) {
147                    $arrRet[$key]['shipment_item'][$arrDetail['product_class_id']][$detailKey] = $detailVal;
148                }
149
150                $arrRet[$key]['shipment_item'][$arrDetail['product_class_id']]['productsClass'] =& $objProduct->getDetailAndProductsClass($arrDetail['product_class_id']);
151            }
152        }
153        $arrTplVar->arrShipping = $arrRet;
154
155        $arrTplVar->Message_tmp = $arrOrder['message'];
156
157        // 会員情報の取得
158        $customer_id = $arrOrder['customer_id'];
159        $objQuery->setOrder('customer_id');
160        $arrRet = $objQuery->select('point', 'dtb_customer', 'customer_id = ?', array($customer_id));
161        $arrCustomer = isset($arrRet[0]) ? $arrRet[0] : '';
162
163        $arrTplVar->arrCustomer = $arrCustomer;
164        $arrTplVar->arrOrder = $arrOrder;
165
166        //その他決済情報
167        if ($arrOrder['memo02'] != '') {
168            $arrOther = unserialize($arrOrder['memo02']);
169
170            foreach ($arrOther as $other_key => $other_val) {
171                if (SC_Utils_Ex::sfTrim($other_val['value']) == '') {
172                    $arrOther[$other_key]['value'] = '';
173                }
174            }
175
176            $arrTplVar->arrOther = $arrOther;
177        }
178
179        // 都道府県変換
180        $arrTplVar->arrPref = $this->arrPref;
181
182        $objCustomer = new SC_Customer_Ex();
183        $arrTplVar->tpl_user_point = $objCustomer->getValue('point');
184
185        $objMailView = null;
186        if (SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE) {
187            $objMailView = new SC_MobileView_Ex();
188        } else {
189            $objMailView = new SC_SiteView_Ex();
190        }
191        // メール本文の取得
192        $objMailView->setPage($this->getPage());
193        $objMailView->assignobj($arrTplVar);
194        $body = $objMailView->fetch($this->arrMAILTPLPATH[$template_id]);
195
196        // メール送信処理
197        $objSendMail = new SC_SendMail_Ex();
198        $bcc = $arrInfo['email01'];
199        $from = $arrInfo['email03'];
200        $error = $arrInfo['email04'];
201        $tosubject = $this->sfMakeSubject($tmp_subject, $objMailView);
202
203        $objSendMail->setItem('', $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
204        $objSendMail->setTo($arrOrder['order_email'], $arrOrder['order_name01'] . ' '. $arrOrder['order_name02'] .' 様');
205
206        // 送信フラグ:trueの場合は、送信する。
207        if ($send) {
208            if ($objSendMail->sendMail()) {
209                $this->sfSaveMailHistory($order_id, $template_id, $tosubject, $body);
210            }
211        }
212
213        return $objSendMail;
214    }
215
216    // テンプレートを使用したメールの送信
217    function sfSendTplMail($to, $tmp_subject, $tplpath, &$objPage)
218    {
219        $objMailView = new SC_SiteView_Ex();
220        $objMailView->setPage($this->getPage());
221        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
222        // メール本文の取得
223        $objPage->tpl_shopname=$arrInfo['shop_name'];
224        $objPage->tpl_infoemail = $arrInfo['email02'];
225        $objMailView->assignobj($objPage);
226        $body = $objMailView->fetch($tplpath);
227        // メール送信処理
228        $objSendMail = new SC_SendMail_Ex();
229        $bcc = $arrInfo['email01'];
230        $from = $arrInfo['email03'];
231        $error = $arrInfo['email04'];
232        $tosubject = $this->sfMakeSubject($tmp_subject, $objMailView);
233
234        $objSendMail->setItem($to, $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
235        $objSendMail->sendMail();
236    }
237
238    // 通常のメール送信
239    function sfSendMail($to, $tmp_subject, $body)
240    {
241        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
242        // メール送信処理
243        $objSendMail = new SC_SendMail_Ex();
244        $bcc = $arrInfo['email01'];
245        $from = $arrInfo['email03'];
246        $error = $arrInfo['email04'];
247        $tosubject = $this->sfMakeSubject($tmp_subject);
248
249        $objSendMail->setItem($to, $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
250        $objSendMail->sendMail();
251    }
252
253    //件名にテンプレートを用いる
254    function sfMakeSubject($subject, &$objMailView = NULL)
255    {
256        if (empty($objMailView)) {
257            $objMailView = new SC_SiteView_Ex();
258            $objMailView->setPage($this->getPage());
259        }
260        $objTplAssign = new stdClass;
261
262        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
263        $objTplAssign->tpl_shopname=$arrInfo['shop_name'];
264        $objTplAssign->tpl_infoemail=$subject; // 従来互換
265        $objTplAssign->tpl_mailtitle=$subject;
266        $objMailView->assignobj($objTplAssign);
267        $subject = $objMailView->fetch('mail_templates/mail_title.tpl');
268        // #1940 (SC_Helper_Mail#sfMakeSubject 先頭に改行を含む値を返す) 対応
269        $subject = trim($subject);
270
271        return $subject;
272    }
273
274    // メール配信履歴への登録
275    function sfSaveMailHistory($order_id, $template_id, $subject, $body)
276    {
277        $sqlval = array();
278        $sqlval['subject'] = $subject;
279        $sqlval['order_id'] = $order_id;
280        $sqlval['template_id'] = $template_id;
281        $sqlval['send_date'] = 'CURRENT_TIMESTAMP';
282        if (!isset($_SESSION['member_id'])) $_SESSION['member_id'] = '';
283        if ($_SESSION['member_id'] != '') {
284            $sqlval['creator_id'] = $_SESSION['member_id'];
285        } else {
286            $sqlval['creator_id'] = '0';
287        }
288        $sqlval['mail_body'] = $body;
289
290        $objQuery =& SC_Query_Ex::getSingletonInstance();
291        $sqlval['send_id'] = $objQuery->nextVal('dtb_mail_history_send_id');
292        $objQuery->insert('dtb_mail_history', $sqlval);
293    }
294
295    /* 会員登録があるかどうかのチェック(仮会員を含まない) */
296    function sfCheckCustomerMailMaga($email)
297    {
298        $col = 'email, mailmaga_flg, customer_id';
299        $from = 'dtb_customer';
300        $where = '(email = ? OR email_mobile = ?) AND status = 2 AND del_flg = 0';
301        $objQuery =& SC_Query_Ex::getSingletonInstance();
302        $arrRet = $objQuery->select($col, $from, $where, array($email));
303        // 会員のメールアドレスが登録されている
304        if (!empty($arrRet[0]['customer_id'])) {
305            return true;
306        }
307
308        return false;
309    }
310
311    /**
312     * 登録メールを送信する。
313     *
314     * @param string $secret_key 会員固有キー
315     * @param integer $customer_id 会員ID
316     * @param boolean $is_mobile false(default):PCアドレスにメールを送る true:携帯アドレスにメールを送る
317     * @return boolean true:成功 false:失敗
318     */
319    function sfSendRegistMail($secret_key, $customer_id = '', $is_mobile = false)
320    {
321        // 会員データの取得
322        if (SC_Utils_Ex::sfIsInt($customer_id)) {
323            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId($customer_id);
324        } else {
325            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId('', 'secret_key = ?', array($secret_key));
326        }
327        if (SC_Utils_Ex::isBlank($arrCustomerData)) {
328            return false;
329        }
330
331        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
332
333        $objMailText = new SC_SiteView_Ex();
334        $objMailText->setPage($this->getPage());
335        $objMailText->assign('CONF', $CONF);
336        $objMailText->assign('name01', $arrCustomerData['name01']);
337        $objMailText->assign('name02', $arrCustomerData['name02']);
338        $objMailText->assign('uniqid', $arrCustomerData['secret_key']);
339        $objMailText->assignobj($arrCustomerData);
340        $objMailText->assignobj($this);
341
342        $objHelperMail  = new SC_Helper_Mail_Ex();
343
344        // 仮会員が有効の場合
345        if (CUSTOMER_CONFIRM_MAIL == true and $arrCustomerData['status'] == 1) {
346            $subject        = $objHelperMail->sfMakeSubject('会員登録のご確認', $objMailText);
347            $toCustomerMail = $objMailText->fetch('mail_templates/customer_mail.tpl');
348        } else {
349            $subject        = $objHelperMail->sfMakeSubject('会員登録のご完了', $objMailText);
350            $toCustomerMail = $objMailText->fetch('mail_templates/customer_regist_mail.tpl');
351        }
352
353        $objMail = new SC_SendMail_Ex();
354        $objMail->setItem(
355            ''                    // 宛先
356            , $subject              // サブジェクト
357            , $toCustomerMail       // 本文
358            , $CONF['email03']      // 配送元アドレス
359            , $CONF['shop_name']    // 配送元 名前
360            , $CONF['email03']      // reply_to
361            , $CONF['email04']      // return_path
362            , $CONF['email04']      // Errors_to
363            , $CONF['email01']      // Bcc
364        );
365        // 宛先の設定
366        if ($is_mobile) {
367            $to_addr = $arrCustomerData['email_mobile'];
368        } else {
369            $to_addr = $arrCustomerData['email'];
370        }
371        $objMail->setTo($to_addr, $arrCustomerData['name01'] . $arrCustomerData['name02'] .' 様');
372
373        $objMail->sendMail();
374
375        return true;
376    }
377
378    /**
379     * 保存されているメルマガテンプレートの取得
380     * @param integer 特定IDのテンプレートを取り出したい時はtemplate_idを指定。未指定時は全件取得
381     * @return array メールテンプレート情報を格納した配列
382     * @todo   表示順も引数で変更できるように
383     */
384    function sfGetMailmagaTemplate($template_id = null)
385    {
386        // 初期化
387        $where = '';
388        $objQuery =& SC_Query_Ex::getSingletonInstance();
389
390        // 条件文
391        $where = 'del_flg = ?';
392        $arrValues[] = 0;
393        //template_id指定時
394        if (SC_Utils_Ex::sfIsInt($template_id) === true) {
395            $where .= ' AND template_id = ?';
396            $arrValues[] = $template_id;
397        }
398
399        // 表示順
400        $objQuery->setOrder('create_date DESC');
401
402        $arrResults = $objQuery->select('*', 'dtb_mailmaga_template', $where, $arrValues);
403
404        return $arrResults;
405    }
406
407    /**
408     * 保存されているメルマガ送信履歴の取得
409     * @param integer 特定の送信履歴を取り出したい時はsend_idを指定。未指定時は全件取得
410     * @return array 送信履歴情報を格納した配列
411     */
412    function sfGetSendHistory($send_id = null)
413    {
414        // 初期化
415        $where = '';
416        $objQuery =& SC_Query_Ex::getSingletonInstance();
417
418        // 条件文
419        $where = 'del_flg = ?';
420        $arrValues[] = 0;
421
422        //send_id指定時
423        if (SC_Utils_Ex::sfIsInt($send_id) === true) {
424            $where .= ' AND send_id = ?';
425            $arrValues[] = $send_id;
426        }
427
428        // 表示順
429        $objQuery->setOrder('create_date DESC');
430
431        $arrResults = $objQuery->select('*', 'dtb_send_history', $where, $arrValues);
432
433        return $arrResults;
434    }
435
436    /**
437     * 指定したIDのメルマガ配送を行う
438     *
439     * @param integer $send_id dtb_send_history の情報
440     * @return void
441     */
442    function sfSendMailmagazine($send_id)
443    {
444        $objQuery =& SC_Query_Ex::getSingletonInstance();
445        $objDb = new SC_Helper_DB_Ex();
446        $objSite = $objDb->sfGetBasisData();
447        $objMail = new SC_SendMail_Ex();
448
449        $where = 'del_flg = 0 AND send_id = ?';
450        $arrMail = $objQuery->getRow('*', 'dtb_send_history', $where, array($send_id));
451
452        // 対象となる$send_idが見つからない
453        if (SC_Utils_Ex::isBlank($arrMail)) return;
454
455        // 送信先リストの取得
456        $arrDestinationList = $objQuery->select(
457            '*',
458            'dtb_send_customer',
459            'send_id = ? AND (send_flag = 2 OR send_flag IS NULL)',
460            array($send_id)
461        );
462
463        // 現在の配信数
464        $complete_count = $arrMail['complete_count'];
465        if (SC_Utils_Ex::isBlank($arrMail)) {
466            $complete_count = 0;
467        }
468
469        foreach ($arrDestinationList as $arrDestination) {
470            // お名前の変換
471            $customerName = trim($arrDestination['name']);
472            $subjectBody = preg_replace('/{name}/', $customerName, $arrMail['subject']);
473            $mailBody = preg_replace('/{name}/', $customerName, $arrMail['body']);
474
475            $objMail->setItem(
476                $arrDestination['email'],
477                $subjectBody,
478                $mailBody,
479                $objSite['email03'],      // 送信元メールアドレス
480                $objSite['shop_name'],    // 送信元名
481                $objSite['email03'],      // reply_to
482                $objSite['email04'],      // return_path
483                $objSite['email04']       // errors_to
484            );
485
486            // テキストメール配信の場合
487            if ($arrMail['mail_method'] == 2) {
488                $sendResut = $objMail->sendMail();
489            // HTMLメール配信の場合
490            } else {
491                $sendResut = $objMail->sendHtmlMail();
492            }
493
494            // 送信完了なら1、失敗なら2をメール送信結果フラグとしてDBに挿入
495            if (!$sendResut) {
496                $sendFlag = '2';
497            } else {
498                // 完了を 1 増やす
499                $sendFlag = '1';
500                $complete_count++;
501            }
502
503            // 送信結果情報を更新
504            $objQuery->update('dtb_send_customer',
505                              array('send_flag'=>$sendFlag),
506                              'send_id = ? AND customer_id = ?',
507                              array($send_id,$arrDestination['customer_id']));
508        }
509
510        // メール全件送信完了後の処理
511        $objQuery->update('dtb_send_history',
512                          array('end_date'=>'CURRENT_TIMESTAMP', 'complete_count'=>$complete_count),
513                          'send_id = ?',
514                          array($send_id));
515
516        // 送信完了 報告メール
517        $compSubject = date('Y年m月d日H時i分') . '  下記メールの配信が完了しました。';
518        // 管理者宛に変更
519        $objMail->setTo($objSite['email03']);
520        $objMail->setSubject($compSubject);
521
522        // テキストメール配信の場合
523        if ($arrMail['mail_method'] == 2) {
524            $sendResut = $objMail->sendMail();
525        // HTMLメール配信の場合
526        } else {
527            $sendResut = $objMail->sendHtmlMail();
528        }
529
530        return;
531    }
532}
Note: See TracBrowser for help on using the repository browser.