2 require_once 'Pman.php';
5 * notification script sender - designed to be run by the Notify script - with many children running
8 * called with an id of a core_notify element
10 * uses core_notify - to find an event to object and person.
12 * uses Events table to log failures
15 * calls $object->toEmail($person,$last_send, $notify) to generate an email struct with
26 class Pman_Core_NotifySend extends Pman
28 static $cli_desc = "Send out single notification email (usually called from Core/Notify)";
30 static $cli_opts = array(
32 'desc' => 'Turn on debugging (see DataObjects debugLevel )',
39 'DB_DataObject-debug' => array(
40 'desc' => 'Turn on debugging (see DataObjects debugLevel )',
48 'desc' => 'Force redelivery, even if it has been sent before or not queued...',
55 'desc' => 'Send the message to this address, rather than the one listed.',
65 var $table = 'core_notify';
68 $ff = HTML_FlexyFramework::get();
72 //HTML_FlexyFramework::ensureSingle(__FILE__, $this);
77 function get($id,$opts)
79 if ($opts['DB_DataObject-debug']) {
80 DB_DataObject::debugLevel($opts['DB_DataObject-debug']);
82 //DB_DataObject::debugLevel(1);
83 //date_default_timezone_set('UTC');
85 $force = empty($opts['force']) ? 0 : 1;
87 $w = DB_DataObject::factory($this->table);
92 if (!$force && strtotime($w->act_when) < strtotime($w->sent)) {
95 die("send repeat to early\n");
98 if (!$force && !empty($w->msgid)) {
100 $w->sent = $w->sqlValue("NOW()");
103 die("message has been sent already.\n");
110 // let's work out the last notification sent to this user..
111 $l = DB_DataObject::factory($this->table);
113 'ontable' => $w->ontable,
115 'person_id' => $w->person_id,
117 $l->whereAdd('id != '. $w->id);
118 $l->orderBy('sent DESC');
120 $ar = $l->fetchAll('sent');
121 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
124 $ev = DB_DataObject::factory('Events');
125 $ev->on_id = $w->id; // int(11)
126 $ev->on_table = $this->table;
128 $ev->orderBy('event_when DESC');
129 $ar = $ev->fetchAll('event_when');
130 $last_event = empty($ar) ? 0 : $ar[0];
133 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
135 $next_try = $next_try_min . ' MINUTES';
137 $email = $this->makeEmail($o, $p, $last, $w);
140 if ($email === true) {
142 $ev = $this->addEvent('NOTIFY', $w,
143 "Notification event cleared (not required any more)" );;
145 $w->sent = date('Y-m-d H:i:s');
147 $w->event_id = $ev->id;
149 die(date('Y-m-d h:i:s ') .
150 "Notification event cleared (not required any more)"
156 if ($email === false || isset($email['error'])) {
157 // object returned 'false' - it does not know how to send it..
158 $ev = $this->addEvent('NOTIFY', $w, isset($email['error']) ?
159 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable);
161 $w->sent = date('Y-m-d H:i:s');
163 $w->event_id = $ev->id;
165 die(date('Y-m-d h:i:s ') .
166 (isset($email['error']) ?
167 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable)
175 if (isset($email['later'])) {
177 $w->act_when = $email['later'];
179 die(date('Y-m-d h:i:s ') . " Delivery postponed by email creator");
183 if (!isset($email['headers']['Message-Id'])) {
184 $HOST = gethostname();
185 $email['headers']['Message-Id'] = "<{$this->table}-{$id}@{$HOST}>";
188 //$p->email = 'alan@akbkhome.com'; //for testing..
189 //print_r($email);exit;
190 // should we fetch the watch that caused it.. - which should contain the method to call..
191 // --send-to=test@xxx.com
192 if (!empty($opts['send-to'])) {
193 $p->email = $opts['send-to'];
196 require_once 'Validate.php';
197 if (!Validate::email($p->email, true)) {
198 $ev = $this->addEvent('NOTIFY', $w, "INVALID ADDRESS: " . $p->email);
200 $w->sent = date('Y-m-d H:i:s');
202 $w->event_id = $ev->id;
204 die(date('Y-m-d h:i:s ') . "INVALID ADDRESS: " . $p->email. "\n");
209 $ff = HTML_FlexyFramework::get();
211 $dom = array_pop(explode('@', $p->email));
213 $mxs = $this->mxs($dom);
217 if ($mxs === false) {
218 $ev = $this->addEvent('NOTIFY', $w, "BAD ADDRESS - ". $p->email );
219 $w->sent = date('Y-m-d H:i:s');
221 $w->event_id = $ev->id;
223 die(date('Y-m-d h:i:s') . " - FAILED - BAD EMAIL - {$p->email} \n");
231 require_once 'Mail.php';
233 foreach($mxs as $dom) {
237 if (!isset($ff->Mail['helo'])) {
238 die("config Mail[helo] is not set");
241 $this->debug("Trying SMTP: $dom / HELO {$ff->Mail['helo']}");
242 $mailer = Mail::factory('smtp', array(
244 'localhost' => $ff->Mail['helo'],
248 $res = $mailer->send($p->email, $email['headers'], $email['body']);
256 $ev = $this->addEvent('NOTIFY', $w, 'SUCCESS: ' .$email['headers']['Subject']);
260 $w->sent = date('Y-m-d H:i:s');
261 $w->msgid = $email['headers']['Message-Id'];
262 $w->event_id = $ev->id; // sent ok.. - no need to record it..
265 // enable cc in notify..
266 if (!empty($email['headers']['Cc'])) {
267 $mailer = Mail::factory('smtp', array(
271 $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
272 $mailer->send($email['headers']['Cc'],
273 $email['headers'], $email['body']);
280 die(date('Y-m-d h:i:s') . " - SENT\n");
282 // what type of error..
283 $code = empty($res->userinfo['smtpcode']) ? -1 : $res->userinfo['smtpcode'];
284 if (!empty($res->code) && $res->code == 10001) {
285 // fake greylist if timed out.
290 $this->debug($res->message);
291 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
293 // give up after 2 days..
294 if (in_array($code, array( 421, 450, 451, 452)) && $next_try_min < (2*24*60)) {
296 // check last event for this item..
298 // we try for 3 days..
300 if (strtotime($w->act_start) < strtotime('NOW - 1 HOUR')) {
301 // older that 1 hour.
305 if (strtotime($w->act_start) < strtotime('NOW - 1 DAY')) {
309 if (strtotime($w->act_start) < strtotime('NOW - 2 DAY')) {
314 $this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
315 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
317 die(date('Y-m-d h:i:s') . " - GREYLISTED\n");
322 if ($fail || $next_try_min > (2*24*60)) {
323 // fail.. = log and give up..
324 $ev = $this->addEvent('NOTIFY', $w, "RETRY TIME EXCEEDED - ".
326 ($fail ? ($res->userinfo['smtpcode'] . ' : ' .$res->toString()) : " - UNKNOWN ERROR"));
327 $w->sent = date('Y-m-d H:i:s');
329 $w->event_id = $ev->id;
331 die(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED\n"));
334 // handle no host availalbe forever...
335 if (strtotime($w->act_start) < strtotime('NOW - 3 DAYS')) {
336 $ev = $this->addEvent('NOTIFY', $w, "RETRY TIME EXCEEDED - ". $p->email);
337 $w->sent = date('Y-m-d H:i:s');
339 $w->event_id = $ev->id;
341 die(date('Y-m-d h:i:s') . " - FAILED - RETRY TIME EXCEEDED\n");
347 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED:' . $p->email);
348 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
350 die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE\n");
356 $mx_records = array();
357 $mx_weight = array();
359 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
360 if (!checkdnsrr($fqdn)) {
366 asort($mx_weight,SORT_NUMERIC);
368 foreach($mx_weight as $k => $weight) {
369 $mxs[] = $mx_records[$k];
375 * wrapper to call object->toEmail()
379 headers : {AssocArray},
383 error : {String} // error message in log.
384 send-to: {String} // use to override rcpt
388 function makeEmail($object, $rcpt, $last_sent_date, $notify)
390 return $object->toEmail($rcpt, $last_sent_date, $notify);
395 if (empty($this->cli_args['debug'])) {