1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2007 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 | // {{{ requires |
---|
25 | require_once(CLASS_PATH . "pages/LC_Page.php"); |
---|
26 | |
---|
27 | /** |
---|
28 | * メール配信履歴 のページクラス. |
---|
29 | * |
---|
30 | * @package Page |
---|
31 | * @author LOCKON CO.,LTD. |
---|
32 | * @version $Id$ |
---|
33 | */ |
---|
34 | class LC_Page_Admin_Mail_Sendmail extends LC_Page { |
---|
35 | |
---|
36 | var $objMail; |
---|
37 | // }}} |
---|
38 | // {{{ functions |
---|
39 | |
---|
40 | /** |
---|
41 | * Page を初期化する. |
---|
42 | * |
---|
43 | * @return void |
---|
44 | */ |
---|
45 | function init() { |
---|
46 | // SC_SendMailの拡張 |
---|
47 | if(file_exists(MODULE_PATH . "mdl_speedmail/SC_SpeedMail.php")) { |
---|
48 | require_once(MODULE_PATH . "mdl_speedmail/SC_SpeedMail.php"); |
---|
49 | // SpeedMail対応 |
---|
50 | $this->objMail = new SC_SpeedMail(); |
---|
51 | } else { |
---|
52 | $this->objMail = new SC_SendMail_Ex(); |
---|
53 | } |
---|
54 | |
---|
55 | parent::init(); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Page のプロセス. |
---|
60 | * |
---|
61 | * @return void |
---|
62 | */ |
---|
63 | function process() { |
---|
64 | $conn = new SC_DbConn(); |
---|
65 | $objSite = new SC_SiteInfo($conn); |
---|
66 | |
---|
67 | if(MELMAGA_SEND != true) { |
---|
68 | exit; |
---|
69 | } |
---|
70 | |
---|
71 | //リアルタイム配信モードがオンのとき |
---|
72 | if($_GET['mode'] == 'now') { |
---|
73 | //---- 未送信データを取得する |
---|
74 | $time_data = $conn->getAll( "SELECT send_id FROM dtb_send_history WHERE complete_count = 0 AND del_flg = 0 AND end_date IS NULL ORDER BY send_id ASC, start_date ASC" ); |
---|
75 | } else { |
---|
76 | // postgresql と mysql とでSQLをわける |
---|
77 | if (DB_TYPE == "pgsql") { |
---|
78 | $sql = "SELECT send_id FROM dtb_send_history "; |
---|
79 | $sql.= "WHERE start_date BETWEEN current_timestamp + '- 5 minutes' AND current_timestamp + '5 minutes' AND del_flg = 0 AND end_date IS NULL ORDER BY send_id ASC, start_date ASC"; |
---|
80 | }else if (DB_TYPE == "mysql") { |
---|
81 | $sql = "SELECT send_id FROM dtb_send_history "; |
---|
82 | $sql.= "WHERE start_date BETWEEN date_add(now(),INTERVAL -5 minute) AND date_add(now(),INTERVAL 5 minute) AND del_flg = 0 AND end_date IS NULL ORDER BY send_id ASC, start_date ASC"; |
---|
83 | } |
---|
84 | //---- 30分毎にCronが送信時間データ確認 |
---|
85 | $time_data = $conn->getAll($sql); |
---|
86 | } |
---|
87 | |
---|
88 | //未送信メルマガの数 |
---|
89 | $count = count($time_data); |
---|
90 | |
---|
91 | //未送信メルマガがあれば送信処理を続ける。なければ中断する。 |
---|
92 | if( $count > 0 ){ |
---|
93 | print("start sending <br />\n"); |
---|
94 | } else { |
---|
95 | print("not found <br />\n"); |
---|
96 | exit; |
---|
97 | } |
---|
98 | |
---|
99 | //---- メール送信準備 |
---|
100 | for( $i = 0; $i < $count; $i++ ) { |
---|
101 | // 送信先リストの取得 |
---|
102 | $sql = "SELECT * FROM dtb_send_customer WHERE send_id = ? AND (send_flag = 2 OR send_flag IS NULL)"; |
---|
103 | $list_data[] = $conn->getAll( $sql, array( $time_data[$i]["send_id"] ) ); |
---|
104 | // 送信先データの取得 |
---|
105 | $sql = "SELECT * FROM dtb_send_history WHERE send_id = ?"; |
---|
106 | $mail_data[] = $conn->getAll( $sql, array( $time_data[$i]["send_id"] ) ); |
---|
107 | } |
---|
108 | |
---|
109 | //---- 送信結果フラグ用SQL |
---|
110 | $sql_flag ="UPDATE dtb_send_customer SET send_flag = ? WHERE send_id = ? AND customer_id = ?"; |
---|
111 | |
---|
112 | //---- メール生成と送信 |
---|
113 | for( $i = 0; $i < $count; $i++ ) { |
---|
114 | for( $j = 0; $j < count( $list_data[$i] ); $j ++ ) { |
---|
115 | $customerName = ""; |
---|
116 | $mailBody = ""; |
---|
117 | $sendFlag = ""; |
---|
118 | |
---|
119 | //-- 顧客名の変換 |
---|
120 | $name = trim($list_data[$i][$j]["name"]); |
---|
121 | |
---|
122 | if ($name == "") { |
---|
123 | $name = "お客"; |
---|
124 | } |
---|
125 | |
---|
126 | $customerName = htmlspecialchars($name); |
---|
127 | $subjectBody = ereg_replace( "{name}", $customerName , $mail_data[$i][0]["subject"] ); |
---|
128 | $mailBody = ereg_replace( "{name}", $customerName , $mail_data[$i][0]["body"] ); |
---|
129 | |
---|
130 | $this->objMail->setItem( |
---|
131 | $list_data[$i][$j]["email"] |
---|
132 | ,$subjectBody |
---|
133 | ,$mailBody |
---|
134 | ,$objSite->data["email03"] // 送信元メールアドレス |
---|
135 | ,$objSite->data["shop_name"] // 送信元名 |
---|
136 | ,$objSite->data["email03"] // reply_to |
---|
137 | ,$objSite->data["email04"] // return_path |
---|
138 | ,$objSite->data["email04"] // errors_to |
---|
139 | ); |
---|
140 | |
---|
141 | //-- テキストメール配信の場合 |
---|
142 | if( $mail_data[$i][0]["mail_method"] == 2 ) { |
---|
143 | $sendResut = $this->objMail->sendMail(); |
---|
144 | //-- HTMLメール配信の場合 |
---|
145 | } else { |
---|
146 | $sendResut = $this->objMail->sendHtmlMail(); |
---|
147 | } |
---|
148 | |
---|
149 | //-- 送信完了なら1、失敗なら-1をメール送信結果フラグとしてDBに挿入 |
---|
150 | if( ! $sendResut ){ |
---|
151 | $sendFlag = "-1"; |
---|
152 | } else { |
---|
153 | $sendFlag = "1"; |
---|
154 | |
---|
155 | // 完了を 1 増やす |
---|
156 | $sql = "UPDATE dtb_send_history SET complete_count = complete_count + 1 WHERE send_id = ?"; |
---|
157 | $conn->query( $sql, array($mail_data[$i][0]["send_id"])); |
---|
158 | } |
---|
159 | $conn->query( $sql_flag, array( $sendFlag, $mail_data[$i][0]["send_id"], $list_data[$i][$j]["customer_id"] ) ); |
---|
160 | } |
---|
161 | |
---|
162 | //--- メール全件送信完了後の処理 |
---|
163 | $completeSql = "UPDATE dtb_send_history SET end_date = now() WHERE send_id = ?"; |
---|
164 | $conn->query( $completeSql, array( $time_data[$i]["send_id"] ) ); |
---|
165 | |
---|
166 | //--- 送信完了 報告メール |
---|
167 | $compSubject = date("Y年m月d日H時i分" . " 下記メールの配信が完了しました。" ); |
---|
168 | // 管理者宛に変更 |
---|
169 | $this->objMail->setTo($objSite->data["email03"]); |
---|
170 | $this->objMail->setSubject($compSubject); |
---|
171 | |
---|
172 | //-- テキストメール配信の場合 |
---|
173 | if( $mail_data[$i][0]["mail_method"] == 2 ) { |
---|
174 | $sendResut = $this->objMail->sendMail(); |
---|
175 | //-- HTMLメール配信の場合 |
---|
176 | } else { |
---|
177 | $sendResut = $this->objMail->sendHtmlMail(); |
---|
178 | } |
---|
179 | } |
---|
180 | if ($_GET['mode'] = "now") { |
---|
181 | header("Location: " . URL_DIR . "admin/mail/history.php"); |
---|
182 | } |
---|
183 | echo "complete\n"; |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * デストラクタ. |
---|
188 | * |
---|
189 | * @return void |
---|
190 | */ |
---|
191 | function destroy() { |
---|
192 | parent::destroy(); |
---|
193 | } |
---|
194 | } |
---|
195 | ?> |
---|