2 require_once 'Pman.php';
5 * notification script sender
7 * called with an id of a core_notify element
9 * uses core_notify - to find an event to object and person.
11 * uses Events table to log failures
14 * calls $object->toEmail($person,$last_send) to generate an email struct with
25 class Pman_Core_NotifySend extends Pman
28 var $table = 'core_notify';
31 $ff = HTML_FlexyFramework::get();
35 //HTML_FlexyFramework::ensureSingle(__FILE__, $this);
44 DB_DataObject::debugLevel(1);
45 //date_default_timezone_set('UTC');
48 $w = DB_DataObject::factory($this->table);
50 if (!$w->get($id) || strtotime($w->act_when) < strtotime($w->sent)) {
51 die("invalid id or time");
57 // let's work out the last notification sent to this user..
58 $l = DB_DataObject::factory($this->table);
60 'ontable' => $w->ontable,
62 'person_id' => $w->person_id,
64 $l->whereAdd('id != '. $w->id);
65 $l->orderBy('sent DESC');
67 $ar = $l->fetchAll('sent');
68 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
71 $ev = DB_DataObject::factory('Events');
72 $ev->on_id = $w->id; // int(11)
73 $ev->od_table = $this->table;
75 $ev->orderBy('event_when DESC');
76 $ar = $ev->fetchAll('event_when');
77 $last_event = empty($ar) ? 0 : $ar[0];
80 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
82 $next_try = $next_try_min . ' MINUTES';
85 $email = $o->toEmail($p,$last);
86 // should we fetch the watch that caused it.. - which should contain the method to call..
87 $dom = array_pop(explode('@', $p->email));
89 $mxs = $this->mxs($dom);
95 foreach($mxs as $dom) {
97 $mailer = Mail::factory('smtp', array( 'host' => $dom ));
98 $res = $mailer->send($email['recipients'], $email['headers'], $email['body']);
102 $w->sent = date('Y-m-d H:i:s');
103 $w->msgid = $email['headers']['Message-Id'];
104 $w->event_id = -1; // sent ok.. - no need to record it..
108 // what type of error..
109 list($code, $response) = $mailer->_smtp->getResponse();
111 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
113 // give up after 2 days..
114 if ($code == 451 && $next_try_min < (2*24*60)) {
116 // check last event for this item..
117 $this->addEvent('NOTIFY', $w, 'GREYLISTED');
118 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
125 if ($fail || $next_try_min > (2*24*60)) {
126 // fail.. = log and give up..
127 $id = $this->addEvent('NOTIFY', $w, 'FAILED - '. $fail ? $res->toString() : "RETRY TIME EXCEEDED");
128 $w->sent = date('Y-m-d H:i:s');
135 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED');
136 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
138 die("NO HOST AVAILABLE");
144 $mx_records = array();
145 $mx_weight = array();
147 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
148 return araray($fqdn);
151 asort($mx_weight,SORT_NUMERIC);
153 foreach($mx_weight as $k => $weight) {
154 $mxs[] = $mx_records[$k];