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 )',
40 'desc' => 'Force redelivery, even if it has been sent before.',
50 var $table = 'core_notify';
53 $ff = HTML_FlexyFramework::get();
57 //HTML_FlexyFramework::ensureSingle(__FILE__, $this);
62 function get($id,$opts)
65 DB_DataObject::debugLevel($opts['debug']);
67 //DB_DataObject::debugLevel(1);
68 //date_default_timezone_set('UTC');
70 $force = $opts['force'];
72 $w = DB_DataObject::factory($this->table);
75 die("invalid id or time\n");
77 if (!$foce && strtotime($w->act_when) < strtotime($w->sent)) {
78 die("send repeat to early\n");
84 // let's work out the last notification sent to this user..
85 $l = DB_DataObject::factory($this->table);
87 'ontable' => $w->ontable,
89 'person_id' => $w->person_id,
91 $l->whereAdd('id != '. $w->id);
92 $l->orderBy('sent DESC');
94 $ar = $l->fetchAll('sent');
95 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
98 $ev = DB_DataObject::factory('Events');
99 $ev->on_id = $w->id; // int(11)
100 $ev->od_table = $this->table;
102 $ev->orderBy('event_when DESC');
103 $ar = $ev->fetchAll('event_when');
104 $last_event = empty($ar) ? 0 : $ar[0];
107 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
109 $next_try = $next_try_min . ' MINUTES';
111 $email = $this->makeEmail($o, $p,$last);
115 //$p->email = 'alan@akbkhome.com'; //for testing..
116 //print_r($email);exit;
117 // should we fetch the watch that caused it.. - which should contain the method to call..
118 $dom = array_pop(explode('@', $p->email));
120 $mxs = $this->mxs($dom);
124 require_once 'Mail.php';
126 foreach($mxs as $dom) {
128 $mailer = Mail::factory('smtp', array(
132 $res = $mailer->send($p->email, $email['headers'], $email['body']);
136 $w->sent = date('Y-m-d H:i:s');
137 $w->msgid = $email['headers']['Message-Id'];
138 $w->event_id = -1; // sent ok.. - no need to record it..
140 die(date('Y-m-d h:i:s') . " - SENT");
142 // what type of error..
143 list($code, $response) = $mailer->_smtp->getResponse();
145 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
147 // give up after 2 days..
148 if ($code == 421 && $next_try_min < (2*24*60)) {
150 // check last event for this item..
151 $this->addEvent('NOTIFY', $w, 'GREYLISTED');
152 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
154 die(date('Y-m-d h:i:s') . " - GREYLISTED");
159 if ($fail || $next_try_min > (2*24*60)) {
160 // fail.. = log and give up..
161 $id = $this->addEvent('NOTIFY', $w, 'FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED"));
162 $w->sent = date('Y-m-d H:i:s');
166 die(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED"));
169 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED');
170 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
172 die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE");
178 $mx_records = array();
179 $mx_weight = array();
181 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
185 asort($mx_weight,SORT_NUMERIC);
187 foreach($mx_weight as $k => $weight) {
188 $mxs[] = $mx_records[$k];
194 * wrapper to call object->toEmail()
197 function makeEmail($o, $p, $last)
199 return $o->toEmail($p,$last);