source: branches/version-2_12-dev/data/class/helper/SC_Helper_Mail.php @ 21829

Revision 21829, 19.1 KB checked in by Seasoft, 12 years ago (diff)

#1613 (typo修正・ソース整形・ソースコメントの改善)

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