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");
97 if (!empty($opts['debug'])) {
101 $sent = (empty($w->sent) || preg_match('/^0000/', $w->sent)) ? false : true;
103 if (!$force && (!empty($w->msgid) || $sent)) {
106 $w->sent = $w->sqlValue("NOW()");
109 die("message has been sent already.\n");
116 $ev = $this->addEvent('NOTIFY', $w,
117 "Notification event cleared (underlying object does not exist)" );;
119 $w->sent = date('Y-m-d H:i:s');
121 $w->event_id = $ev->id;
123 die(date('Y-m-d h:i:s ') .
124 "Notification event cleared (underlying object does not exist)"
132 if (isset($p->active) && empty($p->active)) {
133 $ev = $this->addEvent('NOTIFY', $w,
134 "Notification event cleared (not user not active any more)" );;
136 $w->sent = date('Y-m-d H:i:s');
138 $w->event_id = $ev->id;
140 die(date('Y-m-d h:i:s ') .
141 "Notification event cleared (not user not active any more)"
143 die("message has been sent already.\n");
147 // let's work out the last notification sent to this user..
148 $l = DB_DataObject::factory($this->table);
150 'ontable' => $w->ontable,
152 'person_id' => $w->person_id,
154 $l->whereAdd('id != '. $w->id);
155 $l->orderBy('sent DESC');
157 $ar = $l->fetchAll('sent');
158 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
161 $ev = DB_DataObject::factory('Events');
162 $ev->on_id = $w->id; // int(11)
163 $ev->on_table = $this->table;
165 $ev->orderBy('event_when DESC');
166 $ar = $ev->fetchAll('event_when');
167 $last_event = empty($ar) ? 0 : $ar[0];
170 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
172 $next_try = $next_try_min . ' MINUTES';
174 // this may modify $p->email. (it will not update it though)
175 $email = $this->makeEmail($o, $p, $last, $w, $force);
178 if ($email === true) {
180 $ev = $this->addEvent('NOTIFY', $w,
181 "Notification event cleared (not required any more)" );;
183 $w->sent = date('Y-m-d H:i:s');
185 $w->event_id = $ev->id;
187 die(date('Y-m-d h:i:s ') .
188 "Notification event cleared (not required any more)"
194 if ($email === false || isset($email['error'])) {
195 // object returned 'false' - it does not know how to send it..
196 $ev = $this->addEvent('NOTIFY', $w, isset($email['error']) ?
197 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable);
199 $w->sent = date('Y-m-d H:i:s');
201 $w->event_id = $ev->id;
203 die(date('Y-m-d h:i:s ') .
204 (isset($email['error']) ?
205 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable)
213 if (isset($email['later'])) {
215 $w->act_when = $email['later'];
217 die(date('Y-m-d h:i:s ') . " Delivery postponed by email creator");
221 if (empty($email['headers']['Message-Id'])) {
222 $HOST = gethostname();
223 $email['headers']['Message-Id'] = "<{$this->table}-{$id}@{$HOST}>";
226 //$p->email = 'alan@akbkhome.com'; //for testing..
227 //print_r($email);exit;
228 // should we fetch the watch that caused it.. - which should contain the method to call..
229 // --send-to=test@xxx.com
231 if (!empty($email['send-to'])) {
232 $p->email = $email['send-to'];
234 if (!empty($opts['send-to'])) {
235 $p->email = $opts['send-to'];
237 // since some of them have spaces?!?!
238 $p->email = trim($p->email);
242 require_once 'Validate.php';
243 if (!Validate::email($p->email, true)) {
244 $ev = $this->addEvent('NOTIFY', $w, "INVALID ADDRESS: " . $p->email);
246 $w->sent = date('Y-m-d H:i:s');
248 $w->event_id = $ev->id;
250 die(date('Y-m-d h:i:s ') . "INVALID ADDRESS: " . $p->email. "\n");
255 $ff = HTML_FlexyFramework::get();
258 $dom = array_pop(explode('@', $p->email));
260 $mxs = $this->mxs($dom);
263 // we might fail doing this...
264 // need to handle temporary failure..
267 // we try for 3 days..
269 if (strtotime($w->act_start) < strtotime('NOW - 1 HOUR')) {
270 // older that 1 hour.
274 if (strtotime($w->act_start) < strtotime('NOW - 1 DAY')) {
278 if (strtotime($w->act_start) < strtotime('NOW - 2 DAY')) {
283 if ($mxs === false) {
284 // only retry for 2 day son the MX issue..
286 $this->addEvent('NOTIFY', $w, 'MX LOOKUP FAILED ' . $dom . ' ' . $res->toString());
287 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
289 die(date('Y-m-d h:i:s') . " - GREYLISTED\n");
292 $ev = $this->addEvent('NOTIFY', $w, "BAD ADDRESS - ". $p->email );
293 $w->sent = date('Y-m-d H:i:s');
295 $w->event_id = $ev->id;
297 die(date('Y-m-d h:i:s') . " - FAILED - BAD EMAIL - {$p->email} \n");
305 if (!$force && strtotime($w->act_start) < strtotime('NOW - 14 DAY')) {
306 $ev = $this->addEvent('NOTIFY', $w, "BAD ADDRESS - ". $p->email );
307 $w->sent = date('Y-m-d H:i:s');
309 $w->event_id = $ev->id;
311 die(date('Y-m-d h:i:s') . " - FAILED - GAVE UP TO OLD - {$p->email} \n");
316 $w->to_email = $p->email;
317 //$this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
318 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
324 require_once 'Mail.php';
326 foreach($mxs as $dom) {
330 if (!isset($ff->Mail['helo'])) {
331 die("config Mail[helo] is not set");
334 $this->debug("Trying SMTP: $dom / HELO {$ff->Mail['helo']}");
335 $mailer = Mail::factory('smtp', array(
337 'localhost' => $ff->Mail['helo'],
339 'socket_options' => isset($ff->Mail['socket_options']) ? $ff->Mail['socket_options'] : null
342 $res = $mailer->send($p->email, $email['headers'], $email['body']);
348 $ev = $this->addEvent('NOTIFYSENT', $w, "{$w->to_email} - {$email['headers']['Subject']}");
352 $w->sent = date('Y-m-d H:i:s');
353 $w->msgid = $email['headers']['Message-Id'];
354 $w->event_id = $ev->id; // sent ok.. - no need to record it..
357 // enable cc in notify..
358 if (!empty($email['headers']['Cc'])) {
359 $mailer = Mail::factory('smtp', array(
363 $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
364 $mailer->send($email['headers']['Cc'],
365 $email['headers'], $email['body']);
369 if (!empty($email['bcc'])) {
370 $mailer = Mail::factory('smtp', array(
374 $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
375 $mailer->send($email['bcc'],
376 $email['headers'], $email['body']);
381 die(date('Y-m-d h:i:s') . " - SENT\n");
383 // what type of error..
384 $code = empty($res->userinfo['smtpcode']) ? -1 : $res->userinfo['smtpcode'];
385 if (!empty($res->code) && $res->code == 10001) {
386 // fake greylist if timed out.
391 $this->debug($res->message);
392 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
394 // give up after 2 days..
395 if (in_array($code, array( 421, 450, 451, 452)) && $next_try_min < (2*24*60)) {
397 // check last event for this item..
400 $this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
401 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
403 die(date('Y-m-d h:i:s') . " - GREYLISTED\n");
408 if ($fail || $next_try_min > (2*24*60)) {
409 // fail.. = log and give up..
410 $ev = $this->addEvent('NOTIFY', $w, ($fail ? "FAILED - " : "RETRY TIME EXCEEDED - ") .
412 ($fail ? ($res->userinfo['smtpcode'] . ' : ' .$res->toString()) : " - UNKNOWN ERROR"));
413 $w->sent = date('Y-m-d H:i:s');
415 $w->event_id = $ev->id;
417 die(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED\n"));
420 // handle no host availalbe forever...
421 if (strtotime($w->act_start) < strtotime('NOW - 3 DAYS')) {
422 $ev = $this->addEvent('NOTIFY', $w, "RETRY TIME EXCEEDED - ". $p->email);
423 $w->sent = date('Y-m-d H:i:s');
425 $w->event_id = $ev->id;
427 die(date('Y-m-d h:i:s') . " - FAILED - RETRY TIME EXCEEDED\n");
433 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED:' . $p->email);
434 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
436 die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE\n");
442 $mx_records = array();
443 $mx_weight = array();
445 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
446 if (!checkdnsrr($fqdn)) {
452 asort($mx_weight,SORT_NUMERIC);
454 foreach($mx_weight as $k => $weight) {
455 $mxs[] = $mx_records[$k];
461 * wrapper to call object->toEmail()
465 headers : {AssocArray},
469 error : {String} // error message in log.
470 send-to: {String} // use to override rcpt
474 function makeEmail($object, $rcpt, $last_sent_date, $notify, $force =false)
476 $m = 'notify'. $notify->evtype;
479 if (!empty($notify->evtype) && method_exists($object,$m)) {
480 return $object->$m($rcpt, $last_sent_date, $notify, $force);
483 if (!method_exists($object, 'toEmail')) {
487 return $object->toEmail($rcpt, $last_sent_date, $notify, $force);
492 if (empty($this->cli_args['debug'])) {