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

Revision 23409, 19.8 KB checked in by pineray, 10 years ago (diff)

#2544 公開側のViewクラスをひとつにまとめる

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