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) to generate an email struct with
26 class Pman_Core_NotifySend extends Pman
29 var $table = 'core_notify';
32 $ff = HTML_FlexyFramework::get();
36 //HTML_FlexyFramework::ensureSingle(__FILE__, $this);
45 DB_DataObject::debugLevel(1);
46 //date_default_timezone_set('UTC');
49 $w = DB_DataObject::factory($this->table);
51 if (!$w->get($id) || strtotime($w->act_when) < strtotime($w->sent)) {
52 die("invalid id or time");
58 // let's work out the last notification sent to this user..
59 $l = DB_DataObject::factory($this->table);
61 'ontable' => $w->ontable,
63 'person_id' => $w->person_id,
65 $l->whereAdd('id != '. $w->id);
66 $l->orderBy('sent DESC');
68 $ar = $l->fetchAll('sent');
69 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
72 $ev = DB_DataObject::factory('Events');
73 $ev->on_id = $w->id; // int(11)
74 $ev->od_table = $this->table;
76 $ev->orderBy('event_when DESC');
77 $ar = $ev->fetchAll('event_when');
78 $last_event = empty($ar) ? 0 : $ar[0];
81 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
83 $next_try = $next_try_min . ' MINUTES';
86 $email = $o->toEmail($p,$last);
89 // should we fetch the watch that caused it.. - which should contain the method to call..
90 $dom = array_pop(explode('@', $p->email));
92 $mxs = $this->mxs($dom);
98 foreach($mxs as $dom) {
100 $mailer = Mail::factory('smtp', array( 'host' => $dom ));
101 $res = $mailer->send($email['recipients'], $email['headers'], $email['body']);
105 $w->sent = date('Y-m-d H:i:s');
106 $w->msgid = $email['headers']['Message-Id'];
107 $w->event_id = -1; // sent ok.. - no need to record it..
111 // what type of error..
112 list($code, $response) = $mailer->_smtp->getResponse();
114 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
116 // give up after 2 days..
117 if ($code == 451 && $next_try_min < (2*24*60)) {
119 // check last event for this item..
120 $this->addEvent('NOTIFY', $w, 'GREYLISTED');
121 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
128 if ($fail || $next_try_min > (2*24*60)) {
129 // fail.. = log and give up..
130 $id = $this->addEvent('NOTIFY', $w, 'FAILED - '. $fail ? $res->toString() : "RETRY TIME EXCEEDED");
131 $w->sent = date('Y-m-d H:i:s');
138 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED');
139 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
141 die("NO HOST AVAILABLE");
147 $mx_records = array();
148 $mx_weight = array();
150 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
151 return araray($fqdn);
154 asort($mx_weight,SORT_NUMERIC);
156 foreach($mx_weight as $k => $weight) {
157 $mxs[] = $mx_records[$k];