2 require_once 'Pman.php';
5 * notification script sender
7 * calls $object->toEmail($person,$last_send) to generate an email struct with
18 class Pman_Core_NotifySend extends Pman
21 var $table = 'core_notify';
24 $ff = HTML_FlexyFramework::get();
28 //HTML_FlexyFramework::ensureSingle(__FILE__, $this);
37 DB_DataObject::debugLevel(1);
38 //date_default_timezone_set('UTC');
41 $w = DB_DataObject::factory($this->table);
43 if (!$w->get($id) || strtotime($w->act_when) < strtotime($w->sent)) {
44 die("invalid id or time");
50 // let's work out the last notification sent to this user..
51 $l = DB_DataObject::factory($this->table);
53 'ontable' => $w->ontable,
55 'person_id' => $w->person_id,
57 $l->whereAdd('id != '. $w->id);
58 $l->orderBy('sent DESC');
60 $ar = $l->fetchAll('sent');
61 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
64 $ev = DB_DataObject::factory('Events');
65 $ev->on_id = $w->id; // int(11)
66 $ev->od_table = $this->table;
68 $ev->orderBy('event_when DESC');
69 $ar = $ev->fetchAll('event_when');
70 $last_event = empty($ar) ? 0 : $ar[0];
73 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
75 $next_try = $next_try_min . ' MINUTES';
78 $email = $o->toEmail($p,$last);
79 // should we fetch the watch that caused it.. - which should contain the method to call..
80 $dom = array_pop(explode('@', $p->email));
82 $mxs = $this->mxs($dom);
88 foreach($mxs as $dom) {
90 $mailer = Mail::factory('smtp', array( 'host' => $dom ));
91 $res = $mailer->send($email['recipients'], $email['headers'], $email['body']);
95 $w->sent = date('Y-m-d H:i:s');
96 $w->msgid = $email['headers']['Message-Id'];
97 $w->event_id = -1; // sent ok.. - no need to record it..
101 // what type of error..
102 list($code, $response) = $mailer->_smtp->getResponse();
104 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
106 // give up after 2 days..
107 if ($code == 451 && $next_try_min < (2*24*60)) {
109 // check last event for this item..
110 $this->addEvent('NOTIFY', $w, 'GREYLISTED');
111 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
118 if ($fail || $next_try_min > (2*24*60)) {
119 // fail.. = log and give up..
120 $id = $this->addEvent('NOTIFY', $w, 'FAILED - '. $fail ? $res->toString() : "RETRY TIME EXCEEDED");
121 $w->sent = date('Y-m-d H:i:s');
128 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED');
129 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
131 die("NO HOST AVAILABLE");
137 $mx_records = array();
138 $mx_weight = array();
140 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
141 return araray($fqdn);
144 asort($mx_weight,SORT_NUMERIC);
146 foreach($mx_weight as $k => $weight) {
147 $mxs[] = $mx_records[$k];