-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbnewsletter.php
213 lines (169 loc) · 5.17 KB
/
dbnewsletter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* A newsletter batch script, written in PHP5 running from command prompt *
* Description: It looks in a sql table for newsletter jobs and execute pending jobs.
* by: Dravion - 08/24/2014
*/
#!/usr/bin/php
<?php
namespace corenews;
use PDO;
use PDOException;
/* This is the global connection point to the sql db */
class PDOConnectionFactory{
public $con = null;
public $dbType = "mysql";
public $host = "localhost";
public $user = "root";
public $pass = "wizzard";
public $db = "newsdb";
public $persistent = true;
public function PDOConnectionFactory( $persistent=false ) {
if( $persistent != false){ $this->persistent = true; }
}
/* Get a new connection from the pool */
public function getConnection() {
try {
$this->con = new PDO($this->dbType.":host=".$this->host.";dbname=".$this->db,
$this->user, $this->pass, array( PDO::ATTR_PERSISTENT => $this->persistent ));
$this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $this->con;
} catch (
PDOException $ex ) {
echo "Error: ".$ex->getMessage();
return false;
}
}
public function Close() {
if( $this->con != null )
$this->con = null;
}
}
/* in class news are the needed functions for newsjobhandling */
class news {
private $nid = null;
private $headline = null;
private $storydate = null;
private $story;
/* call if news was successfully send */
public function jobisdone($nid) {
$db = new PDOConnectionFactory(TRUE);
$pdo = $db->getConnection();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$stmt = $pdo->prepare
("
UPDATE
news
SET
sended = 1
WHERE
nid = :nid;
");
$stmt->bindParam(':nid' , $nid, PDO::PARAM_INT);
$stmt->execute();
$pdo = null;
} catch(PDOException $e) {
echo $e->getMessage();
}
return true;
}
/* process pending newsjobs */
public function sendNews() {
try {
$db = new PDOConnectionFactory(TRUE);
$pdo = $db->getConnection();
$stmt = $pdo->prepare("
SELECT
firstname,
email
FROM
customers
WHERE
newsletter = 1;
");
$stmt->execute();
while ($rs = $stmt->fetch(PDO::FETCH_OBJ)) {
$nid = $this->nid;
$subject = $this->headline;
$message = $this->story;
$headers = 'From: '."Infomailer" . "\r\n" .
'Reply-To: '."Infomailer" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
/* params ok, send email now */
mail($rs->email, $this->headline, $message, $headers);
$this->jobisdone($this->nid);
}
} catch(PDOException $e) {
echo "Error : ".$e->getMessage();
return null;
}
}
/* load news where the sendflag is not set to 1 */
public function loadNews() {
$nState = false;
try {
$db = new PDOConnectionFactory(TRUE);
$pdo = $db->getConnection();
$stmt = $pdo->prepare("
SELECT
nid,
headline,
storydate,
story
FROM
news
WHERE
sended = 0;
");
$stmt->execute();
while ($rs = $stmt->fetch(PDO::FETCH_OBJ)) {
$this->nid = $rs->nid;
$this->headline = $rs->headline;
$this->storydate = $rs->storydate;
$this->story = $rs->story;
$nState = true;
}
} catch(PDOException $e) {
echo "Error: ".$e->getMessage();
return null;
}
return $nState;
}
public function getJobCount() {
$nState = 0;
try {
$db = new PDOConnectionFactory(TRUE);
$pdo = $db->getConnection();
$stmt = $pdo->prepare("
SELECT
count(nid) AS x
FROM
news
WHERE
sended = 0
");
$stmt->execute();
while ($rs = $stmt->fetch(PDO::FETCH_OBJ)) {
$nState = $rs->x;
}
} catch(PDOException $e){
echo "Error: ".$e->getMessage();
return null;
}
return $nState;
}
}
/* Main program */
print "\nNewsscript 1.0\n\n";
$cinfo = new news();
print "New Jobs(".$cinfo->getJobCount().")\r";
while (true) {
if ($cinfo->loadNews() == true) {
print "New Jobs(".$cinfo->getJobCount().") found, working... Exit with CTRL+C\r";
$cinfo->sendNews();
print "\r";
} else {
$date = date('Y/m/d H:i:s');
print "No newsjobs pending, waiting for jobs... ".$date ." Exit with CTRL+C\r";
}
sleep(3); /* sleep 3 secs until we check the mysql news table again for new newsjobs */
}