source: branches/version-2_5-dev/data/class/helper/SC_Helper_Mail.php @ 20538

Revision 20538, 18.3 KB checked in by Seasoft, 13 years ago (diff)

#627(ソース整形・ソースコメントの改善)
#628(未使用処理・定義などの削除)

  • 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-2010 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     * コンストラクタ.
38     */
39    function SC_Helper_Mail() {
40        $masterData = new SC_DB_MasterData_Ex();
41        $this->arrMAILTPLPATH =  $masterData->getMasterData("mtb_mail_tpl_path");
42        $this->arrPref = $masterData->getMasterData('mtb_pref');
43    }
44
45    /* DBに登録されたテンプレートメールの送信 */
46    function sfSendTemplateMail($to, $to_name, $template_id, &$objPage, $from_address = "", $from_name = "", $reply_to = "") {
47
48        $objQuery = new SC_Query_Ex();
49        // メールテンプレート情報の取得
50        $where = "template_id = ?";
51        $arrRet = $objQuery->select("subject, header, footer", "dtb_mailtemplate", $where, array($template_id));
52        $objPage->tpl_header = $arrRet[0]['header'];
53        $objPage->tpl_footer = $arrRet[0]['footer'];
54        $tmp_subject = $arrRet[0]['subject'];
55
56        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
57
58        $objMailView = new SC_SiteView_Ex();
59        // メール本文の取得
60        $objMailView->assignobj($objPage);
61        $body = $objMailView->fetch($this->arrMAILTPLPATH[$template_id]);
62
63        // メール送信処理
64        $objSendMail = new SC_SendMail_Ex();
65        if ($from_address == "") $from_address = $arrInfo['email03'];
66        if ($from_name == "") $from_name = $arrInfo['shop_name'];
67        if ($reply_to == "") $reply_to = $arrInfo['email03'];
68        $error = $arrInfo['email04'];
69        $tosubject = $this->sfMakeSubject($tmp_subject, $objMailView);
70       
71        $objSendMail->setItem('', $tosubject, $body, $from_address, $from_name, $reply_to, $error, $error);
72        $objSendMail->setTo($to, $to_name);
73        $objSendMail->sendMail();    // メール送信
74    }
75
76    /* 受注完了メール送信 */
77    function sfSendOrderMail($order_id, $template_id, $subject = "", $header = "", $footer = "", $send = true) {
78
79        $arrTplVar = new stdClass();
80        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
81        $arrTplVar->arrInfo = $arrInfo;
82
83        $objQuery = new SC_Query_Ex();
84
85        if($subject == "" && $header == "" && $footer == "") {
86            // メールテンプレート情報の取得
87            $where = "template_id = ?";
88            $arrRet = $objQuery->select("subject, header, footer", "dtb_mailtemplate", $where, array($template_id));
89            $arrTplVar->tpl_header = $arrRet[0]['header'];
90            $arrTplVar->tpl_footer = $arrRet[0]['footer'];
91            $tmp_subject = $arrRet[0]['subject'];
92        } else {
93            $arrTplVar->tpl_header = $header;
94            $arrTplVar->tpl_footer = $footer;
95            $tmp_subject = $subject;
96        }
97
98        // 受注情報の取得
99        $where = "order_id = ?";
100        $arrRet = $objQuery->select("*", "dtb_order", $where, array($order_id));
101        $arrOrder = $arrRet[0];
102        $objQuery->setOrder('order_detail_id');
103        $arrTplVar->arrOrderDetail = $objQuery->select("*", "dtb_order_detail", $where, array($order_id));
104
105        $objProduct = new SC_Product_Ex();
106        $objQuery->setOrder('shipping_id');
107        $arrRet = $objQuery->select("*", "dtb_shipping", "order_id = ?", array($order_id));
108        foreach (array_keys($arrRet) as $key) {
109            $objQuery->setOrder('shipping_id');
110            $arrItems = $objQuery->select("*", "dtb_shipment_item", "order_id = ? AND shipping_id = ?",
111                                          array($order_id, $arrRet[$key]['shipping_id']));
112            foreach ($arrItems as $itemKey => $arrDetail) {
113                foreach ($arrDetail as $detailKey => $detailVal) {
114                    $arrRet[$key]['shipment_item'][$arrDetail['product_class_id']][$detailKey] = $detailVal;
115                }
116
117                $arrRet[$key]['shipment_item'][$arrDetail['product_class_id']]['productsClass'] =& $objProduct->getDetailAndProductsClass($arrDetail['product_class_id']);
118            }
119        }
120        $arrTplVar->arrShipping = $arrRet;
121
122        $arrTplVar->Message_tmp = $arrOrder['message'];
123
124        // 顧客情報の取得
125        $customer_id = $arrOrder['customer_id'];
126        $objQuery->setOrder('customer_id');
127        $arrRet = $objQuery->select('point', "dtb_customer", "customer_id = ?", array($customer_id));
128        $arrCustomer = isset($arrRet[0]) ? $arrRet[0] : "";
129
130        $arrTplVar->arrCustomer = $arrCustomer;
131        $arrTplVar->arrOrder = $arrOrder;
132
133        //その他決済情報
134        if($arrOrder['memo02'] != "") {
135            $arrOther = unserialize($arrOrder['memo02']);
136
137            foreach($arrOther as $other_key => $other_val){
138                if(SC_Utils_Ex::sfTrim($other_val['value']) == ""){
139                    $arrOther[$other_key]['value'] = "";
140                }
141            }
142
143            $arrTplVar->arrOther = $arrOther;
144        }
145
146        // 都道府県変換
147        $arrTplVar->arrPref = $this->arrPref;
148
149        $objCustomer = new SC_Customer_Ex();
150        $arrTplVar->tpl_user_point = $objCustomer->getValue('point');
151
152       if(Net_UserAgent_Mobile::isMobile() === true) {
153            $objMailView = new SC_MobileView_Ex();
154       } else {
155            $objMailView = new SC_SiteView_Ex();
156       }
157        // メール本文の取得
158        $objMailView->assignobj($arrTplVar);
159        $body = $objMailView->fetch($this->arrMAILTPLPATH[$template_id]);
160
161        // メール送信処理
162        $objSendMail = new SC_SendMail_Ex();
163        $bcc = $arrInfo['email01'];
164        $from = $arrInfo['email03'];
165        $error = $arrInfo['email04'];
166        $tosubject = $this->sfMakeSubject($tmp_subject, $objMailView);
167
168        $objSendMail->setItem('', $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
169        $objSendMail->setTo($arrOrder["order_email"], $arrOrder["order_name01"] . " ". $arrOrder["order_name02"] ." 様");
170
171
172        // 送信フラグ:trueの場合は、送信する。
173        if($send) {
174            if ($objSendMail->sendMail()) {
175                $this->sfSaveMailHistory($order_id, $template_id, $tosubject, $body);
176            }
177        }
178
179        return $objSendMail;
180    }
181
182    // テンプレートを使用したメールの送信
183    function sfSendTplMail($to, $tmp_subject, $tplpath, &$objPage) {
184        $objMailView = new SC_SiteView_Ex();
185        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
186        // メール本文の取得
187        $objPage->tpl_shopname=$arrInfo['shop_name'];
188        $objPage->tpl_infoemail = $arrInfo['email02'];
189        $objMailView->assignobj($objPage);
190        $body = $objMailView->fetch($tplpath);
191        // メール送信処理
192        $objSendMail = new SC_SendMail_Ex();
193        $bcc = $arrInfo['email01'];
194        $from = $arrInfo['email03'];
195        $error = $arrInfo['email04'];
196        $tosubject = $this->sfMakeSubject($tmp_subject, $objMailView);
197       
198        $objSendMail->setItem($to, $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
199        $objSendMail->sendMail();
200    }
201
202    // 通常のメール送信
203    function sfSendMail($to, $tmp_subject, $body) {
204        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
205        // メール送信処理
206        $objSendMail = new SC_SendMail_Ex();
207        $bcc = $arrInfo['email01'];
208        $from = $arrInfo['email03'];
209        $error = $arrInfo['email04'];
210        $tosubject = $this->sfMakeSubject($tmp_subject);
211       
212        $objSendMail->setItem($to, $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
213        $objSendMail->sendMail();
214    }
215
216    //件名にテンプレートを用いる
217    function sfMakeSubject($subject, &$objMailView) {
218        if (empty($objMailView)) {
219            $objMailView = new SC_SiteView_Ex();
220        }
221        $objTplAssign = new stdClass;
222       
223        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
224        $objTplAssign->tpl_shopname=$arrInfo['shop_name'];
225        $objTplAssign->tpl_infoemail=$subject; // 従来互換
226        $objTplAssign->tpl_mailtitle=$subject;
227        $objMailView->assignobj($objTplAssign);
228        $subject = $objMailView->fetch('mail_templates/mail_title.tpl');
229        return $subject;
230    }
231
232    // メール配信履歴への登録
233    function sfSaveMailHistory($order_id, $template_id, $subject, $body) {
234        $sqlval['subject'] = $subject;
235        $sqlval['order_id'] = $order_id;
236        $sqlval['template_id'] = $template_id;
237        $sqlval['send_date'] = "Now()";
238        if (!isset($_SESSION['member_id'])) $_SESSION['member_id'] = "";
239        if($_SESSION['member_id'] != "") {
240            $sqlval['creator_id'] = $_SESSION['member_id'];
241        } else {
242            $sqlval['creator_id'] = '0';
243        }
244        $sqlval['mail_body'] = $body;
245
246        $objQuery = new SC_Query_Ex();
247        $sqlval['send_id'] = $objQuery->nextVal("dtb_mail_history_send_id");
248        $objQuery->insert("dtb_mail_history", $sqlval);
249    }
250
251    /* 会員登録があるかどうかのチェック(仮会員を含まない) */
252    function sfCheckCustomerMailMaga($email) {
253        $col = "email, mailmaga_flg, customer_id";
254        $from = "dtb_customer";
255        $where = "(email = ? OR email_mobile = ?) AND status = 2 AND del_flg = 0";
256        $objQuery = new SC_Query_Ex();
257        $arrRet = $objQuery->select($col, $from, $where, array($email));
258        // 会員のメールアドレスが登録されている
259        if(!empty($arrRet[0]['customer_id'])) {
260            return true;
261        }
262        return false;
263    }
264
265    /**
266     * 登録メールを送信する。
267     *
268     * @param string $secret_key 顧客固有キー
269     * @param integer $customer_id 顧客ID
270     * @param boolean $is_mobile false(default):PCアドレスにメールを送る true:携帯アドレスにメールを送る
271     * @return boolean true:成功 false:失敗
272     */
273    function sfSendRegistMail($secret_key, $customer_id = '', $is_mobile = false) {
274        // 顧客データの取得
275        if(SC_Utils_Ex::sfIsInt($customer_id)) {
276            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId($customer_id);
277        }else{
278            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId('', "secret_key = ?", array($secret_key));
279        }
280        if(SC_Utils_Ex::isBlank($arrCustomerData)) {
281            return false;
282        }
283
284        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
285       
286        $objMailText = new SC_SiteView_Ex();
287        $objMailText->assign('CONF', $CONF);
288        $objMailText->assign("name01", $arrCustomerData['name01']);
289        $objMailText->assign("name02", $arrCustomerData['name02']);
290        $objMailText->assign('uniqid', $arrCustomerData['secret_key']);
291        $objMailText->assignobj($arrCustomerData);
292        $objMailText->assignobj($this);
293
294        $objHelperMail  = new SC_Helper_Mail_Ex();
295
296        // 仮会員が有効の場合
297        if(CUSTOMER_CONFIRM_MAIL == true and $arrCustomerData['status'] == 1) {
298            $subject        = $objHelperMail->sfMakeSubject('会員登録のご確認', $objMailText);
299            $toCustomerMail = $objMailText->fetch("mail_templates/customer_mail.tpl");
300        } else {
301            $subject        = $objHelperMail->sfMakeSubject('会員登録のご完了', $objMailText);
302            $toCustomerMail = $objMailText->fetch("mail_templates/customer_regist_mail.tpl");
303        }
304
305        $objMail = new SC_SendMail();
306        $objMail->setItem(
307            ''                    // 宛先
308            , $subject              // サブジェクト
309            , $toCustomerMail       // 本文
310            , $CONF["email03"]      // 配送元アドレス
311            , $CONF["shop_name"]    // 配送元 名前
312            , $CONF["email03"]      // reply_to
313            , $CONF["email04"]      // return_path
314            , $CONF["email04"]      // Errors_to
315            , $CONF["email01"]      // Bcc
316        );
317        // 宛先の設定
318        if($is_mobile) {
319            $to_addr = $arrCustomerData["email_mobile"];
320        }else{
321            $to_addr = $arrCustomerData['email'];
322        }
323        $objMail->setTo($to_addr, $arrCustomerData["name01"] . $arrCustomerData["name02"] ." 様");
324
325        $objMail->sendMail();
326        return true;
327    }
328   
329    /**
330     * 保存されているメルマガテンプレートの取得
331     * @param integer 特定IDのテンプレートを取り出したい時はtemplate_idを指定。未指定時は全件取得
332     * @return array メールテンプレート情報を格納した配列
333     * @todo   表示順も引数で変更できるように
334     */
335    function sfGetMailmagaTemplate($template_id = null){
336        // 初期化
337        $where = '';
338        $objQuery =& SC_Query_Ex::getSingletonInstance();
339       
340        // 条件文
341        $where = 'del_flg = ?';
342        $arrValues[] = 0;
343        //template_id指定時
344        if (SC_Utils_Ex::sfIsInt($template_id) === true) {
345            $where .= 'AND template_id = ?';
346            $arrValues[] = $template_id;
347        }
348       
349        // 表示順
350        $objQuery->setOrder("create_date DESC");
351       
352        $arrResults = $objQuery->select('*', 'dtb_mailmaga_template', $where, $arrValues);
353        return $arrResults;
354    }
355   
356    /**
357     * 保存されているメルマガ送信履歴の取得
358     * @param integer 特定の送信履歴を取り出したい時はsend_idを指定。未指定時は全件取得
359     * @return array 送信履歴情報を格納した配列
360     */
361    function sfGetSendHistory($send_id = null){
362        // 初期化
363        $where = '';
364        $objQuery =& SC_Query_Ex::getSingletonInstance();
365       
366        // 条件文
367        $where = 'del_flg = ?';
368        $arrValues[] = 0;
369       
370        //send_id指定時
371        if (SC_Utils_Ex::sfIsInt($send_id) === true) {
372            $where .= 'AND send_id = ?';
373            $arrValues[] = $send_id;
374        }
375       
376        // 表示順
377        $objQuery->setOrder("create_date DESC");
378       
379        $arrResults = $objQuery->select('*', 'dtb_send_history', $where, $arrValues);
380        return $arrResults;
381    }
382
383    /**
384     * 指定したIDのメルマガ配送を行う
385     *
386     * @param integer $send_id dtb_send_history の情報
387     * @return void
388     */
389    function sfSendMailmagazine($send_id) {
390        $objQuery =& SC_Query_Ex::getSingletonInstance();
391        $objDb = new SC_Helper_DB_Ex();
392        $objSite = $objDb->sfGetBasisData();
393        $objMail = new SC_SendMail_Ex();
394       
395        $where = 'del_flg = 0 AND send_id = ?';
396        $arrMail = $objQuery->getRow('*', 'dtb_send_history', $where, array($send_id));
397
398        // 対象となる$send_idが見つからない
399        if (SC_Utils_Ex::isBlank($arrMail)) return;
400
401        // 送信先リストの取得
402        $arrDestinationList = $objQuery->select(
403            '*',
404            'dtb_send_customer',
405            'send_id = ? AND (send_flag = 2 OR send_flag IS NULL)',
406            array($send_id)
407        );
408       
409        // 現在の配信数
410        $complete_count = $arrMail['complete_count'];
411        if(SC_Utils_Ex::isBlank($arrMail)) $complete_count = 0;
412       
413        foreach ($arrDestinationList as $arrDestination) {
414
415            // 顧客名の変換
416            $customerName = trim($arrDestination['name']);
417            $subjectBody = preg_replace("/{name}/", $customerName, $arrMail['subject']);
418            $mailBody = preg_replace("/{name}/", $customerName, $arrMail['body']);
419
420            $objMail->setItem(
421                $arrDestination['email'],
422                $subjectBody,
423                $mailBody,
424                $objSite["email03"],      // 送信元メールアドレス
425                $objSite["shop_name"],    // 送信元名
426                $objSite["email03"],      // reply_to
427                $objSite["email04"],      // return_path
428                $objSite["email04"]       // errors_to
429            );
430           
431            // テキストメール配信の場合
432            if ($arrMail["mail_method"] == 2) {
433                $sendResut = $objMail->sendMail();
434            // HTMLメール配信の場合
435            } else {
436                $sendResut = $objMail->sendHtmlMail();
437            }
438
439            // 送信完了なら1、失敗なら2をメール送信結果フラグとしてDBに挿入
440            if (!$sendResut) {
441                $sendFlag = '2';
442            } else {
443                // 完了を 1 増やす
444                $sendFlag = '1';
445                $complete_count++;
446            }
447           
448            // 送信結果情報を更新
449            $objQuery->update('dtb_send_customer',
450                              array('send_flag'=>$sendFlag),
451                              'send_id = ? AND customer_id = ?',
452                              array($send_id,$arrDestination["customer_id"]));
453        }
454
455        // メール全件送信完了後の処理
456        $objQuery->update('dtb_send_history',
457                          array('end_date'=>"now()", 'complete_count'=>$complete_count),
458                          'send_id = ?',
459                          array($send_id));
460
461        // 送信完了 報告メール
462        $compSubject = date("Y年m月d日H時i分") . "  下記メールの配信が完了しました。";
463        // 管理者宛に変更
464        $objMail->setTo($objSite["email03"]);
465        $objMail->setSubject($compSubject);
466
467        // テキストメール配信の場合
468        if ($arrMail["mail_method"] == 2 ) {
469            $sendResut = $objMail->sendMail();
470        // HTMLメール配信の場合
471        } else {
472            $sendResut = $objMail->sendHtmlMail();
473        }
474        return;
475    }
476}
477?>
Note: See TracBrowser for help on using the repository browser.