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

Revision 20361, 18.3 KB checked in by fukuda, 13 years ago (diff)

#963 [管理画面]メルマガ管理リファクタリング 1.eregをpregに 2.細かいバグ修正

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