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

Revision 23024, 19.5 KB checked in by AMUAMU, 11 years ago (diff)

#2323 (税率対応)
#2235 (越境ECを想定した機能の追加)
#2234 (非会員お客様情報入力テンプレートと機能の共通化)
#2324 (会員登録、注文フォームに「会社名」フィールドを足す)
などの修正

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