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

Revision 21866, 19.2 KB checked in by h_yoshimoto, 12 years ago (diff)

#1831 Copyrightを更新を誤っているので戻します

  • 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 = ? AND del_flg = 0';
126        $arrOrder = $objQuery->getRow('*', 'dtb_order', $where, array($order_id));
127
128        if (empty($arrOrder)) {
129            trigger_error("該当する受注が存在しない。(注文番号: $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 (array_keys($arrRet) as $key) {
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 $itemKey => $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'], $arrOrder['order_name01'] . ' '. $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        return $subject;
264    }
265
266    // メール配信履歴への登録
267    function sfSaveMailHistory($order_id, $template_id, $subject, $body) {
268        $sqlval['subject'] = $subject;
269        $sqlval['order_id'] = $order_id;
270        $sqlval['template_id'] = $template_id;
271        $sqlval['send_date'] = 'CURRENT_TIMESTAMP';
272        if (!isset($_SESSION['member_id'])) $_SESSION['member_id'] = '';
273        if ($_SESSION['member_id'] != '') {
274            $sqlval['creator_id'] = $_SESSION['member_id'];
275        } else {
276            $sqlval['creator_id'] = '0';
277        }
278        $sqlval['mail_body'] = $body;
279
280        $objQuery =& SC_Query_Ex::getSingletonInstance();
281        $sqlval['send_id'] = $objQuery->nextVal('dtb_mail_history_send_id');
282        $objQuery->insert('dtb_mail_history', $sqlval);
283    }
284
285    /* 会員登録があるかどうかのチェック(仮会員を含まない) */
286    function sfCheckCustomerMailMaga($email) {
287        $col = 'email, mailmaga_flg, customer_id';
288        $from = 'dtb_customer';
289        $where = '(email = ? OR email_mobile = ?) AND status = 2 AND del_flg = 0';
290        $objQuery =& SC_Query_Ex::getSingletonInstance();
291        $arrRet = $objQuery->select($col, $from, $where, array($email));
292        // 会員のメールアドレスが登録されている
293        if (!empty($arrRet[0]['customer_id'])) {
294            return true;
295        }
296        return false;
297    }
298
299    /**
300     * 登録メールを送信する。
301     *
302     * @param string $secret_key 会員固有キー
303     * @param integer $customer_id 会員ID
304     * @param boolean $is_mobile false(default):PCアドレスにメールを送る true:携帯アドレスにメールを送る
305     * @return boolean true:成功 false:失敗
306     */
307    function sfSendRegistMail($secret_key, $customer_id = '', $is_mobile = false) {
308        // 会員データの取得
309        if (SC_Utils_Ex::sfIsInt($customer_id)) {
310            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId($customer_id);
311        } else {
312            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId('', 'secret_key = ?', array($secret_key));
313        }
314        if (SC_Utils_Ex::isBlank($arrCustomerData)) {
315            return false;
316        }
317
318        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
319
320        $objMailText = new SC_SiteView_Ex();
321        $objMailText->setPage($this->getPage());
322        $objMailText->assign('CONF', $CONF);
323        $objMailText->assign('name01', $arrCustomerData['name01']);
324        $objMailText->assign('name02', $arrCustomerData['name02']);
325        $objMailText->assign('uniqid', $arrCustomerData['secret_key']);
326        $objMailText->assignobj($arrCustomerData);
327        $objMailText->assignobj($this);
328
329        $objHelperMail  = new SC_Helper_Mail_Ex();
330
331        // 仮会員が有効の場合
332        if (CUSTOMER_CONFIRM_MAIL == true and $arrCustomerData['status'] == 1) {
333            $subject        = $objHelperMail->sfMakeSubject('会員登録のご確認', $objMailText);
334            $toCustomerMail = $objMailText->fetch('mail_templates/customer_mail.tpl');
335        } else {
336            $subject        = $objHelperMail->sfMakeSubject('会員登録のご完了', $objMailText);
337            $toCustomerMail = $objMailText->fetch('mail_templates/customer_regist_mail.tpl');
338        }
339
340        $objMail = new SC_SendMail();
341        $objMail->setItem(
342            ''                    // 宛先
343            , $subject              // サブジェクト
344            , $toCustomerMail       // 本文
345            , $CONF['email03']      // 配送元アドレス
346            , $CONF['shop_name']    // 配送元 名前
347            , $CONF['email03']      // reply_to
348            , $CONF['email04']      // return_path
349            , $CONF['email04']      // Errors_to
350            , $CONF['email01']      // Bcc
351        );
352        // 宛先の設定
353        if ($is_mobile) {
354            $to_addr = $arrCustomerData['email_mobile'];
355        } else {
356            $to_addr = $arrCustomerData['email'];
357        }
358        $objMail->setTo($to_addr, $arrCustomerData['name01'] . $arrCustomerData['name02'] .' 様');
359
360        $objMail->sendMail();
361        return true;
362    }
363
364    /**
365     * 保存されているメルマガテンプレートの取得
366     * @param integer 特定IDのテンプレートを取り出したい時はtemplate_idを指定。未指定時は全件取得
367     * @return array メールテンプレート情報を格納した配列
368     * @todo   表示順も引数で変更できるように
369     */
370    function sfGetMailmagaTemplate($template_id = null) {
371        // 初期化
372        $where = '';
373        $objQuery =& SC_Query_Ex::getSingletonInstance();
374
375        // 条件文
376        $where = 'del_flg = ?';
377        $arrValues[] = 0;
378        //template_id指定時
379        if (SC_Utils_Ex::sfIsInt($template_id) === true) {
380            $where .= ' AND template_id = ?';
381            $arrValues[] = $template_id;
382        }
383
384        // 表示順
385        $objQuery->setOrder('create_date DESC');
386
387        $arrResults = $objQuery->select('*', 'dtb_mailmaga_template', $where, $arrValues);
388        return $arrResults;
389    }
390
391    /**
392     * 保存されているメルマガ送信履歴の取得
393     * @param integer 特定の送信履歴を取り出したい時はsend_idを指定。未指定時は全件取得
394     * @return array 送信履歴情報を格納した配列
395     */
396    function sfGetSendHistory($send_id = null) {
397        // 初期化
398        $where = '';
399        $objQuery =& SC_Query_Ex::getSingletonInstance();
400
401        // 条件文
402        $where = 'del_flg = ?';
403        $arrValues[] = 0;
404
405        //send_id指定時
406        if (SC_Utils_Ex::sfIsInt($send_id) === true) {
407            $where .= ' AND send_id = ?';
408            $arrValues[] = $send_id;
409        }
410
411        // 表示順
412        $objQuery->setOrder('create_date DESC');
413
414        $arrResults = $objQuery->select('*', 'dtb_send_history', $where, $arrValues);
415        return $arrResults;
416    }
417
418    /**
419     * 指定したIDのメルマガ配送を行う
420     *
421     * @param integer $send_id dtb_send_history の情報
422     * @return void
423     */
424    function sfSendMailmagazine($send_id) {
425        $objQuery =& SC_Query_Ex::getSingletonInstance();
426        $objDb = new SC_Helper_DB_Ex();
427        $objSite = $objDb->sfGetBasisData();
428        $objMail = new SC_SendMail_Ex();
429
430        $where = 'del_flg = 0 AND send_id = ?';
431        $arrMail = $objQuery->getRow('*', 'dtb_send_history', $where, array($send_id));
432
433        // 対象となる$send_idが見つからない
434        if (SC_Utils_Ex::isBlank($arrMail)) return;
435
436        // 送信先リストの取得
437        $arrDestinationList = $objQuery->select(
438            '*',
439            'dtb_send_customer',
440            'send_id = ? AND (send_flag = 2 OR send_flag IS NULL)',
441            array($send_id)
442        );
443
444        // 現在の配信数
445        $complete_count = $arrMail['complete_count'];
446        if (SC_Utils_Ex::isBlank($arrMail)) {
447            $complete_count = 0;
448        }
449
450        foreach ($arrDestinationList as $arrDestination) {
451
452            // お名前の変換
453            $customerName = trim($arrDestination['name']);
454            $subjectBody = preg_replace('/{name}/', $customerName, $arrMail['subject']);
455            $mailBody = preg_replace('/{name}/', $customerName, $arrMail['body']);
456
457            $objMail->setItem(
458                $arrDestination['email'],
459                $subjectBody,
460                $mailBody,
461                $objSite['email03'],      // 送信元メールアドレス
462                $objSite['shop_name'],    // 送信元名
463                $objSite['email03'],      // reply_to
464                $objSite['email04'],      // return_path
465                $objSite['email04']       // errors_to
466            );
467
468            // テキストメール配信の場合
469            if ($arrMail['mail_method'] == 2) {
470                $sendResut = $objMail->sendMail();
471            // HTMLメール配信の場合
472            } else {
473                $sendResut = $objMail->sendHtmlMail();
474            }
475
476            // 送信完了なら1、失敗なら2をメール送信結果フラグとしてDBに挿入
477            if (!$sendResut) {
478                $sendFlag = '2';
479            } else {
480                // 完了を 1 増やす
481                $sendFlag = '1';
482                $complete_count++;
483            }
484
485            // 送信結果情報を更新
486            $objQuery->update('dtb_send_customer',
487                              array('send_flag'=>$sendFlag),
488                              'send_id = ? AND customer_id = ?',
489                              array($send_id,$arrDestination['customer_id']));
490        }
491
492        // メール全件送信完了後の処理
493        $objQuery->update('dtb_send_history',
494                          array('end_date'=>'CURRENT_TIMESTAMP', 'complete_count'=>$complete_count),
495                          'send_id = ?',
496                          array($send_id));
497
498        // 送信完了 報告メール
499        $compSubject = date('Y年m月d日H時i分') . '  下記メールの配信が完了しました。';
500        // 管理者宛に変更
501        $objMail->setTo($objSite['email03']);
502        $objMail->setSubject($compSubject);
503
504        // テキストメール配信の場合
505        if ($arrMail['mail_method'] == 2) {
506            $sendResut = $objMail->sendMail();
507        // HTMLメール配信の場合
508        } else {
509            $sendResut = $objMail->sendHtmlMail();
510        }
511        return;
512    }
513}
Note: See TracBrowser for help on using the repository browser.