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

Revision 23606, 20.2 KB checked in by kimoto, 10 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/e0f27994-b3c7-4fc6-ad70-55d3cd63769b/patches

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