source: branches/version-2_13_0/data/class/helper/SC_Helper_Mail.php @ 23202

Revision 23202, 19.8 KB checked in by michael_nelson, 11 years ago (diff)

#2116 仮登録メール再送で会員登録完了メールが送信されます mishap with character

  • 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        $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    public 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    public 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    public 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    public 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    public 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     * @param $resend_flg true  仮登録メール再送
321     * @return boolean true:成功 false:失敗
322     * 
323     */
324    public function sfSendRegistMail($secret_key, $customer_id = '', $is_mobile = false, $resend_flg = false)
325    {
326        // 会員データの取得
327        if (SC_Utils_Ex::sfIsInt($customer_id)) {
328            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId($customer_id);
329        } else {
330            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId('', 'secret_key = ?', array($secret_key));
331        }
332        if (SC_Utils_Ex::isBlank($arrCustomerData)) {
333            return false;
334        }
335
336        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
337
338        $objMailText = new SC_SiteView_Ex();
339        $objMailText->setPage($this->getPage());
340        $objMailText->assign('CONF', $CONF);
341        $objMailText->assign('name01', $arrCustomerData['name01']);
342        $objMailText->assign('name02', $arrCustomerData['name02']);
343        $objMailText->assign('uniqid', $arrCustomerData['secret_key']);
344        $objMailText->assignobj($arrCustomerData);
345        $objMailText->assignobj($this);
346
347        $objHelperMail  = new SC_Helper_Mail_Ex();
348        // 仮会員が有効の場合   
349        if (CUSTOMER_CONFIRM_MAIL == true and $arrCustomerData['status'] == 1 or $arrCustomerData['status'] == 1 and $resend_flg == true) {
350            $subject        = $objHelperMail->sfMakeSubject('会員登録のご確認', $objMailText);
351            $toCustomerMail = $objMailText->fetch('mail_templates/customer_mail.tpl');
352        } else {
353            $subject        = $objHelperMail->sfMakeSubject('会員登録のご完了', $objMailText);
354            $toCustomerMail = $objMailText->fetch('mail_templates/customer_regist_mail.tpl');
355           
356        }
357
358        $objMail = new SC_SendMail_Ex();
359        $objMail->setItem(
360            ''                    // 宛先
361            , $subject              // サブジェクト
362            , $toCustomerMail       // 本文
363            , $CONF['email03']      // 配送元アドレス
364            , $CONF['shop_name']    // 配送元 名前
365            , $CONF['email03']      // reply_to
366            , $CONF['email04']      // return_path
367            , $CONF['email04']      // Errors_to
368            , $CONF['email01']      // Bcc
369        );
370        // 宛先の設定
371        if ($is_mobile) {
372            $to_addr = $arrCustomerData['email_mobile'];
373        } else {
374            $to_addr = $arrCustomerData['email'];
375        }
376        $objMail->setTo($to_addr, $arrCustomerData['name01'] . $arrCustomerData['name02'] .' 様');
377
378        $objMail->sendMail();
379
380        return true;
381    }
382
383    /**
384     * 保存されているメルマガテンプレートの取得
385     * @param integer 特定IDのテンプレートを取り出したい時はtemplate_idを指定。未指定時は全件取得
386     * @return array メールテンプレート情報を格納した配列
387     * @todo   表示順も引数で変更できるように
388     */
389    public function sfGetMailmagaTemplate($template_id = null)
390    {
391        // 初期化
392        $where = '';
393        $objQuery =& SC_Query_Ex::getSingletonInstance();
394
395        // 条件文
396        $where = 'del_flg = ?';
397        $arrValues[] = 0;
398        //template_id指定時
399        if (SC_Utils_Ex::sfIsInt($template_id) === true) {
400            $where .= ' AND template_id = ?';
401            $arrValues[] = $template_id;
402        }
403
404        // 表示順
405        $objQuery->setOrder('create_date DESC');
406
407        $arrResults = $objQuery->select('*', 'dtb_mailmaga_template', $where, $arrValues);
408
409        return $arrResults;
410    }
411
412    /**
413     * 保存されているメルマガ送信履歴の取得
414     * @param integer 特定の送信履歴を取り出したい時はsend_idを指定。未指定時は全件取得
415     * @return array 送信履歴情報を格納した配列
416     */
417    public function sfGetSendHistory($send_id = null)
418    {
419        // 初期化
420        $where = '';
421        $objQuery =& SC_Query_Ex::getSingletonInstance();
422
423        // 条件文
424        $where = 'del_flg = ?';
425        $arrValues[] = 0;
426
427        //send_id指定時
428        if (SC_Utils_Ex::sfIsInt($send_id) === true) {
429            $where .= ' AND send_id = ?';
430            $arrValues[] = $send_id;
431        }
432
433        // 表示順
434        $objQuery->setOrder('create_date DESC');
435
436        $arrResults = $objQuery->select('*', 'dtb_send_history', $where, $arrValues);
437
438        return $arrResults;
439    }
440
441    /**
442     * 指定したIDのメルマガ配送を行う
443     *
444     * @param integer $send_id dtb_send_history の情報
445     * @return void
446     */
447    public function sfSendMailmagazine($send_id)
448    {
449        $objQuery =& SC_Query_Ex::getSingletonInstance();
450        $objDb = new SC_Helper_DB_Ex();
451        $objSite = $objDb->sfGetBasisData();
452        $objMail = new SC_SendMail_Ex();
453
454        $where = 'del_flg = 0 AND send_id = ?';
455        $arrMail = $objQuery->getRow('*', 'dtb_send_history', $where, array($send_id));
456
457        // 対象となる$send_idが見つからない
458        if (SC_Utils_Ex::isBlank($arrMail)) return;
459
460        // 送信先リストの取得
461        $arrDestinationList = $objQuery->select(
462            '*',
463            'dtb_send_customer',
464            'send_id = ? AND (send_flag = 2 OR send_flag IS NULL)',
465            array($send_id)
466        );
467
468        // 現在の配信数
469        $complete_count = $arrMail['complete_count'];
470        if (SC_Utils_Ex::isBlank($arrMail)) {
471            $complete_count = 0;
472        }
473
474        foreach ($arrDestinationList as $arrDestination) {
475            // お名前の変換
476            $customerName = trim($arrDestination['name']);
477            $subjectBody = preg_replace('/{name}/', $customerName, $arrMail['subject']);
478            $mailBody = preg_replace('/{name}/', $customerName, $arrMail['body']);
479
480            $objMail->setItem(
481                $arrDestination['email'],
482                $subjectBody,
483                $mailBody,
484                $objSite['email03'],      // 送信元メールアドレス
485                $objSite['shop_name'],    // 送信元名
486                $objSite['email03'],      // reply_to
487                $objSite['email04'],      // return_path
488                $objSite['email04']       // errors_to
489            );
490
491            // テキストメール配信の場合
492            if ($arrMail['mail_method'] == 2) {
493                $sendResut = $objMail->sendMail();
494            // HTMLメール配信の場合
495            } else {
496                $sendResut = $objMail->sendHtmlMail();
497            }
498
499            // 送信完了なら1、失敗なら2をメール送信結果フラグとしてDBに挿入
500            if (!$sendResut) {
501                $sendFlag = '2';
502            } else {
503                // 完了を 1 増やす
504                $sendFlag = '1';
505                $complete_count++;
506            }
507
508            // 送信結果情報を更新
509            $objQuery->update('dtb_send_customer',
510                              array('send_flag'=>$sendFlag),
511                              'send_id = ? AND customer_id = ?',
512                              array($send_id,$arrDestination['customer_id']));
513        }
514
515        // メール全件送信完了後の処理
516        $objQuery->update('dtb_send_history',
517                          array('end_date'=>'CURRENT_TIMESTAMP', 'complete_count'=>$complete_count),
518                          'send_id = ?',
519                          array($send_id));
520
521        // 送信完了 報告メール
522        $compSubject = date('Y年m月d日H時i分') . '  下記メールの配信が完了しました。';
523        // 管理者宛に変更
524        $objMail->setTo($objSite['email03']);
525        $objMail->setSubject($compSubject);
526
527        // テキストメール配信の場合
528        if ($arrMail['mail_method'] == 2) {
529            $sendResut = $objMail->sendMail();
530        // HTMLメール配信の場合
531        } else {
532            $sendResut = $objMail->sendHtmlMail();
533        }
534
535        return;
536    }
537}
Note: See TracBrowser for help on using the repository browser.