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 )',
39 'DB_DataObject-debug' => array(
40 'desc' => 'Turn on debugging (see DataObjects debugLevel )',
48 'desc' => 'Force redelivery, even if it has been sent before or not queued...',
55 'desc' => 'Send the message to this address, rather than the one listed.',
65 var $table = 'core_notify';
68 $ff = HTML_FlexyFramework::get();
72 //HTML_FlexyFramework::ensureSingle(__FILE__, $this);
77 function get($id,$opts)
79 if ($opts['DB_DataObject-debug']) {
80 DB_DataObject::debugLevel($opts['debug']);
82 //DB_DataObject::debugLevel(1);
83 //date_default_timezone_set('UTC');
85 $force = empty($opts['force']) ? 0 : 1;
87 $w = DB_DataObject::factory($this->table);
92 if (!$force && strtotime($w->act_when) < strtotime($w->sent)) {
95 die("send repeat to early\n");
98 if (!$force && !empty($w->msgid)) {
99 die("message has been sent already.\n");
106 // let's work out the last notification sent to this user..
107 $l = DB_DataObject::factory($this->table);
109 'ontable' => $w->ontable,
111 'person_id' => $w->person_id,
113 $l->whereAdd('id != '. $w->id);
114 $l->orderBy('sent DESC');
116 $ar = $l->fetchAll('sent');
117 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
120 $ev = DB_DataObject::factory('Events');
121 $ev->on_id = $w->id; // int(11)
122 $ev->od_table = $this->table;
124 $ev->orderBy('event_when DESC');
125 $ar = $ev->fetchAll('event_when');
126 $last_event = empty($ar) ? 0 : $ar[0];
129 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
131 $next_try = $next_try_min . ' MINUTES';
133 $email = $this->makeEmail($o, $p,$last, $ev);
137 if (!isset($email['headers']['Message-Id'])) {
138 $HOST = gethostname();
139 $email['headers']['Message-Id'] = "<{$this->table}-{$id}@{$HOST}>";
142 //$p->email = 'alan@akbkhome.com'; //for testing..
143 //print_r($email);exit;
144 // should we fetch the watch that caused it.. - which should contain the method to call..
146 if (!empty($opts['send-to'])) {
147 $p->email = $opts['send-to'];
151 $dom = array_pop(explode('@', $p->email));
153 $mxs = $this->mxs($dom);
157 require_once 'Mail.php';
159 foreach($mxs as $dom) {
160 $this->debug("Trying SMTP: $dom");
161 $mailer = Mail::factory('smtp', array(
165 $res = $mailer->send($p->email, $email['headers'], $email['body']);
169 $w->sent = date('Y-m-d H:i:s');
170 $w->msgid = $email['headers']['Message-Id'];
171 $w->event_id = -1; // sent ok.. - no need to record it..
173 die(date('Y-m-d h:i:s') . " - SENT\n");
175 // what type of error..
176 $code = empty($res->smtpcode) ? -1 : $res->smtpcode;
178 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
180 // give up after 2 days..
181 if (in_array($code, array( 421, 451, 452)) && $next_try_min < (2*24*60)) {
183 // check last event for this item..
184 $this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
185 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
187 die(date('Y-m-d h:i:s') . " - GREYLISTED\n");
192 if ($fail || $next_try_min > (2*24*60)) {
193 // fail.. = log and give up..
194 $id = $this->addEvent('NOTIFY', $w, "RETRY TIME EXCEEDED - ".
196 ($fail ? ($res->smtpcode . ' : ' .$res->toString()) : " - UNKNOWN ERROR"));
197 $w->sent = date('Y-m-d H:i:s');
201 die(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED\n"));
204 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED:' . $p->email);
205 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
207 die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE\n");
213 $mx_records = array();
214 $mx_weight = array();
216 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
220 asort($mx_weight,SORT_NUMERIC);
222 foreach($mx_weight as $k => $weight) {
223 $mxs[] = $mx_records[$k];
229 * wrapper to call object->toEmail()
232 function makeEmail($o, $p, $last, $notify)
234 return $o->toEmail($p,$last, $notify);
239 if (empty($this->cli_args['debug'])) {