1 | <?php |
---|
2 | /* |
---|
3 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
---|
4 | * |
---|
5 | * http://www.lockon.co.jp/ |
---|
6 | */ |
---|
7 | $include_dir = realpath(dirname( __FILE__)); |
---|
8 | require_once($include_dir."/../require.php"); |
---|
9 | $conn = new SC_DbConn(); |
---|
10 | $objSite = new SC_SiteInfo($conn); |
---|
11 | |
---|
12 | if(MELMAGA_SEND != true) { |
---|
13 | exit; |
---|
14 | } |
---|
15 | |
---|
16 | if($_GET['mode'] == 'now') { |
---|
17 | //---- 未送信データを取得する |
---|
18 | $time_data = $conn->getAll( "SELECT send_id FROM dtb_send_history WHERE complete_count = 0 AND del_flg = 0 ORDER BY send_id ASC, start_date ASC" ); |
---|
19 | } else { |
---|
20 | |
---|
21 | // postgresql と mysql とでSQLをわける |
---|
22 | if (DB_TYPE == "pgsql") { |
---|
23 | $sql = "SELECT send_id FROM dtb_send_history "; |
---|
24 | $sql.= "WHERE start_date BETWEEN current_timestamp + '- 5 minutes' AND current_timestamp + '5 minutes' AND del_flg = 0 ORDER BY send_id ASC, start_date ASC"; |
---|
25 | }else if (DB_TYPE == "mysql") { |
---|
26 | $sql = "SELECT send_id FROM dtb_send_history "; |
---|
27 | $sql.= "WHERE start_date BETWEEN date_add(now(),INTERVAL -5 minute) AND date_add(now(),INTERVAL 5 minute) AND del_flg = 0 ORDER BY send_id ASC, start_date ASC"; |
---|
28 | } |
---|
29 | //---- 30分毎にCronが送信時間データ確認 |
---|
30 | $time_data = $conn->getAll($sql); |
---|
31 | } |
---|
32 | |
---|
33 | $count = count($time_data); |
---|
34 | |
---|
35 | if( $count > 0 ){ |
---|
36 | print("start sending <br />\n"); |
---|
37 | } else { |
---|
38 | print("not found <br />\n"); |
---|
39 | exit; |
---|
40 | } |
---|
41 | |
---|
42 | //---- メール送信 |
---|
43 | for( $i = 0; $i < count( $time_data ); $i++ ) { |
---|
44 | |
---|
45 | $sql = "SELECT * FROM dtb_send_customer WHERE send_id = ? AND (send_flag = 2 OR send_flag IS NULL)"; |
---|
46 | $list_data[] = $conn->getAll( $sql, array( $time_data[$i]["send_id"] ) ); |
---|
47 | |
---|
48 | $sql = "SELECT * FROM dtb_send_history WHERE send_id = ?"; |
---|
49 | $mail_data[] = $conn->getAll( $sql, array( $time_data[$i]["send_id"] ) ); |
---|
50 | |
---|
51 | } |
---|
52 | |
---|
53 | //---- 送信結果フラグ用SQL |
---|
54 | $sql_flag ="UPDATE dtb_send_customer SET send_flag = ? WHERE send_id = ? AND customer_id = ?"; |
---|
55 | $objMail = new GC_SendMail(); |
---|
56 | |
---|
57 | //---- メール生成と送信 |
---|
58 | for( $i = 0; $i < count( $time_data ); $i++ ) { |
---|
59 | |
---|
60 | for( $j = 0; $j < count( $list_data[$i] ); $j ++ ) { |
---|
61 | |
---|
62 | $customerName = ""; |
---|
63 | $mailBody = ""; |
---|
64 | $sendFlag = ""; |
---|
65 | |
---|
66 | //-- 顧客名の変換 |
---|
67 | $name = trim($list_data[$i][$j]["name"]); |
---|
68 | |
---|
69 | if ($name == "") { |
---|
70 | $name = "お客"; |
---|
71 | } |
---|
72 | |
---|
73 | $customerName = htmlspecialchars($name); |
---|
74 | $subjectBody = ereg_replace( "{name}", $customerName , $mail_data[$i][0]["subject"] ); |
---|
75 | $mailBody = ereg_replace( "{name}", $customerName , $mail_data[$i][0]["body"] ); |
---|
76 | |
---|
77 | //-- テキストメール配信の場合 |
---|
78 | if( $mail_data[$i][0]["mail_method"] == 2 ) { |
---|
79 | |
---|
80 | $sendResut = MAIL_SENDING( |
---|
81 | $list_data[$i][$j]["email"] // 顧客宛先 |
---|
82 | ,$subjectBody // Subject |
---|
83 | ,$mailBody // メール本文 |
---|
84 | ,$objSite->data["email03"] // 送信元メールアドレス |
---|
85 | ,$objSite->data["company_name"] // 送信元名 |
---|
86 | ,$objSite->data["email03"] // reply_to |
---|
87 | ,$objSite->data["email04"] // return_path |
---|
88 | ,$objSite->data["email04"] // errors_to |
---|
89 | ); |
---|
90 | |
---|
91 | //-- HTMLメール配信の場合 |
---|
92 | } elseif( $mail_data[$i][0]["mail_method"] == 1 || $mail_data[$i][0]["mail_method"] == 3) { |
---|
93 | |
---|
94 | $sendResut = HTML_MAIL_SENDING( |
---|
95 | $list_data[$i][$j]["email"] |
---|
96 | ,$subjectBody |
---|
97 | ,$mailBody |
---|
98 | ,$objSite->data["email03"] // 送信元メールアドレス |
---|
99 | ,$objSite->data["company_name"] // 送信元名 |
---|
100 | ,$objSite->data["email03"] // reply_to |
---|
101 | ,$objSite->data["email04"] // return_path |
---|
102 | ,$objSite->data["email04"] // errors_to |
---|
103 | ); |
---|
104 | } |
---|
105 | |
---|
106 | //-- 送信完了なら1、失敗なら0をメール送信結果フラグとしてDBに挿入 |
---|
107 | if( ! $sendResut ){ |
---|
108 | $sendFlag = "-1"; |
---|
109 | } else { |
---|
110 | $sendFlag = "1"; |
---|
111 | |
---|
112 | // 完了を1こ増やす |
---|
113 | $sql = "UPDATE dtb_send_history SET complete_count = complete_count + 1 WHERE send_id = ?"; |
---|
114 | $conn->query( $sql, array($mail_data[$i][0]["send_id"]) ); |
---|
115 | } |
---|
116 | |
---|
117 | $conn->query( $sql_flag, array( $sendFlag, $mail_data[$i][0]["send_id"], $list_data[$i][$j]["customer_id"] ) ); |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | } |
---|
122 | |
---|
123 | //--- メール全件送信完了後の処理 |
---|
124 | $completeSql = "UPDATE dtb_send_history SET end_date = now() WHERE send_id = ?"; |
---|
125 | $conn->query( $completeSql, array( $time_data[$i]["send_id"] ) ); |
---|
126 | |
---|
127 | //--- 送信完了 報告メール |
---|
128 | $compData = date("Y年m月d日H時i分" . " 下記メールの配信が完了しました。" ); |
---|
129 | |
---|
130 | HTML_MAIL_SENDING( |
---|
131 | $objSite->data["email03"] |
---|
132 | ,$compData |
---|
133 | ,$mail_data[$i][0]["body"] |
---|
134 | ,$objSite->data["email03"] // 送信元メールアドレス |
---|
135 | ,$objSite->data["company_name"] // 送信元名 |
---|
136 | ,$objSite->data["email03"] // reply_to |
---|
137 | ,$objSite->data["email04"] // return_path |
---|
138 | ,$objSite->data["email04"] // errors_to |
---|
139 | ); |
---|
140 | |
---|
141 | if ($_GET['mode'] = "now") { |
---|
142 | header("Location: " . URL_DIR . "admin/mail/history.php"); |
---|
143 | } |
---|
144 | echo "complete\n"; |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | //--- テキストメール配信 |
---|
150 | function MAIL_SENDING( $to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to="", $bcc="", $cc ="" ) { |
---|
151 | |
---|
152 | |
---|
153 | $mail_obj = new GC_SendMail(); |
---|
154 | $mail_obj->setItem( $to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc ); |
---|
155 | |
---|
156 | if( $mail_obj->sendMail() ) { |
---|
157 | return true; |
---|
158 | } |
---|
159 | |
---|
160 | } |
---|
161 | |
---|
162 | //--- HTMLメール配信 |
---|
163 | function HTML_MAIL_SENDING( $to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to="", $bcc="", $cc ="" ) { |
---|
164 | |
---|
165 | |
---|
166 | $html_mail_obj = new GC_SendMail(); |
---|
167 | $html_mail_obj->setItemHtml( $to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc ); |
---|
168 | |
---|
169 | |
---|
170 | if( $html_mail_obj->sendHtmlMail() ) { |
---|
171 | return true; |
---|
172 | } |
---|
173 | |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | ?> |
---|