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 var $table = 'core_notify';
43 $ff = HTML_FlexyFramework::get();
47 //HTML_FlexyFramework::ensureSingle(__FILE__, $this);
52 function get($id,$opts)
55 DB_DataObject::debugLevel($opts['debug']);
57 //DB_DataObject::debugLevel(1);
58 //date_default_timezone_set('UTC');
61 $w = DB_DataObject::factory($this->table);
64 die("invalid id or time\n");
66 if (strtotime($w->act_when) < strtotime($w->sent)) {
67 die("send repeat to early\n");
73 // let's work out the last notification sent to this user..
74 $l = DB_DataObject::factory($this->table);
76 'ontable' => $w->ontable,
78 'person_id' => $w->person_id,
80 $l->whereAdd('id != '. $w->id);
81 $l->orderBy('sent DESC');
83 $ar = $l->fetchAll('sent');
84 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
87 $ev = DB_DataObject::factory('Events');
88 $ev->on_id = $w->id; // int(11)
89 $ev->od_table = $this->table;
91 $ev->orderBy('event_when DESC');
92 $ar = $ev->fetchAll('event_when');
93 $last_event = empty($ar) ? 0 : $ar[0];
96 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
98 $next_try = $next_try_min . ' MINUTES';
100 $email = $this->makeEmail($o, $p,$last);
104 //$p->email = 'alan@akbkhome.com'; //for testing..
105 //print_r($email);exit;
106 // should we fetch the watch that caused it.. - which should contain the method to call..
107 $dom = array_pop(explode('@', $p->email));
109 $mxs = $this->mxs($dom);
113 require_once 'Mail.php';
115 foreach($mxs as $dom) {
117 $mailer = Mail::factory('smtp', array(
121 $res = $mailer->send($p->email, $email['headers'], $email['body']);
125 $w->sent = date('Y-m-d H:i:s');
126 $w->msgid = $email['headers']['Message-Id'];
127 $w->event_id = -1; // sent ok.. - no need to record it..
129 die(date('Y-m-d h:i:s') . " - SENT");
131 // what type of error..
132 list($code, $response) = $mailer->_smtp->getResponse();
134 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
136 // give up after 2 days..
137 if ($code == 421 && $next_try_min < (2*24*60)) {
139 // check last event for this item..
140 $this->addEvent('NOTIFY', $w, 'GREYLISTED');
141 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
143 die(date('Y-m-d h:i:s') . " - GREYLISTED");
148 if ($fail || $next_try_min > (2*24*60)) {
149 // fail.. = log and give up..
150 $id = $this->addEvent('NOTIFY', $w, 'FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED"));
151 $w->sent = date('Y-m-d H:i:s');
155 die(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED"));
158 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED');
159 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
161 die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE");
167 $mx_records = array();
168 $mx_weight = array();
170 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
174 asort($mx_weight,SORT_NUMERIC);
176 foreach($mx_weight as $k => $weight) {
177 $mxs[] = $mx_records[$k];
183 * wrapper to call object->toEmail()
186 function makeEmail($o, $p, $last)
188 return $o->toEmail($p,$last);