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 or not queued...',
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);
77 if (!$force && strtotime($w->act_when) < strtotime($w->sent)) {
80 die("send repeat to early\n");
83 if (!$forced && !empty($w->msgid)) {
84 die("message has been sent already.\n");
91 // let's work out the last notification sent to this user..
92 $l = DB_DataObject::factory($this->table);
94 'ontable' => $w->ontable,
96 'person_id' => $w->person_id,
98 $l->whereAdd('id != '. $w->id);
99 $l->orderBy('sent DESC');
101 $ar = $l->fetchAll('sent');
102 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
105 $ev = DB_DataObject::factory('Events');
106 $ev->on_id = $w->id; // int(11)
107 $ev->od_table = $this->table;
109 $ev->orderBy('event_when DESC');
110 $ar = $ev->fetchAll('event_when');
111 $last_event = empty($ar) ? 0 : $ar[0];
114 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
116 $next_try = $next_try_min . ' MINUTES';
118 $email = $this->makeEmail($o, $p,$last);
122 //$p->email = 'alan@akbkhome.com'; //for testing..
123 //print_r($email);exit;
124 // should we fetch the watch that caused it.. - which should contain the method to call..
125 $dom = array_pop(explode('@', $p->email));
127 $mxs = $this->mxs($dom);
131 require_once 'Mail.php';
133 foreach($mxs as $dom) {
135 $mailer = Mail::factory('smtp', array(
139 $res = $mailer->send($p->email, $email['headers'], $email['body']);
143 $w->sent = date('Y-m-d H:i:s');
144 $w->msgid = $email['headers']['Message-Id'];
145 $w->event_id = -1; // sent ok.. - no need to record it..
147 die(date('Y-m-d h:i:s') . " - SENT");
149 // what type of error..
150 list($code, $response) = $mailer->_smtp->getResponse();
152 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
154 // give up after 2 days..
155 if ($code == 421 && $next_try_min < (2*24*60)) {
157 // check last event for this item..
158 $this->addEvent('NOTIFY', $w, 'GREYLISTED');
159 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
161 die(date('Y-m-d h:i:s') . " - GREYLISTED");
166 if ($fail || $next_try_min > (2*24*60)) {
167 // fail.. = log and give up..
168 $id = $this->addEvent('NOTIFY', $w, 'FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED"));
169 $w->sent = date('Y-m-d H:i:s');
173 die(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED"));
176 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED');
177 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
179 die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE");
185 $mx_records = array();
186 $mx_weight = array();
188 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
192 asort($mx_weight,SORT_NUMERIC);
194 foreach($mx_weight as $k => $weight) {
195 $mxs[] = $mx_records[$k];
201 * wrapper to call object->toEmail()
204 function makeEmail($o, $p, $last)
206 return $o->toEmail($p,$last);