source: branches/version-2_12-multilang/data/class/helper/SC_Helper_Mail.php @ 22453

Revision 22453, 19.4 KB checked in by m_uehara, 11 years ago (diff)

#2060 メッセージIDの振り直し

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