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

Revision 23562, 19.9 KB checked in by Seasoft, 10 years ago (diff)

#2584 (管理画面から「注文受付メール(携帯)」を送信すると、「注文受付メール」の本文で送信される)
#2448 (typo修正・ソース整形・ソースコメントの改善 for 2.13.3)

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