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['DB_DataObject-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");
97 if (!empty($opts['debug'])) {
102 if (!$force && !empty($w->msgid)) {
104 $w->sent = $w->sqlValue("NOW()");
107 die("message has been sent already.\n");
114 if (isset($p->active) && empty($p->active)) {
115 $ev = $this->addEvent('NOTIFY', $w,
116 "Notification event cleared (not user not active any more)" );;
118 $w->sent = date('Y-m-d H:i:s');
120 $w->event_id = $ev->id;
122 die(date('Y-m-d h:i:s ') .
123 "Notification event cleared (not user not active any more)"
125 die("message has been sent already.\n");
129 // let's work out the last notification sent to this user..
130 $l = DB_DataObject::factory($this->table);
132 'ontable' => $w->ontable,
134 'person_id' => $w->person_id,
136 $l->whereAdd('id != '. $w->id);
137 $l->orderBy('sent DESC');
139 $ar = $l->fetchAll('sent');
140 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
143 $ev = DB_DataObject::factory('Events');
144 $ev->on_id = $w->id; // int(11)
145 $ev->on_table = $this->table;
147 $ev->orderBy('event_when DESC');
148 $ar = $ev->fetchAll('event_when');
149 $last_event = empty($ar) ? 0 : $ar[0];
152 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
154 $next_try = $next_try_min . ' MINUTES';
156 // this may modify $p->email. (it will not update it though)
157 $email = $this->makeEmail($o, $p, $last, $w, $force);
160 if ($email === true) {
162 $ev = $this->addEvent('NOTIFY', $w,
163 "Notification event cleared (not required any more)" );;
165 $w->sent = date('Y-m-d H:i:s');
167 $w->event_id = $ev->id;
169 die(date('Y-m-d h:i:s ') .
170 "Notification event cleared (not required any more)"
176 if ($email === false || isset($email['error'])) {
177 // object returned 'false' - it does not know how to send it..
178 $ev = $this->addEvent('NOTIFY', $w, isset($email['error']) ?
179 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable);
181 $w->sent = date('Y-m-d H:i:s');
183 $w->event_id = $ev->id;
185 die(date('Y-m-d h:i:s ') .
186 (isset($email['error']) ?
187 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable)
195 if (isset($email['later'])) {
197 $w->act_when = $email['later'];
199 die(date('Y-m-d h:i:s ') . " Delivery postponed by email creator");
203 if (!isset($email['headers']['Message-Id'])) {
204 $HOST = gethostname();
205 $email['headers']['Message-Id'] = "<{$this->table}-{$id}@{$HOST}>";
208 //$p->email = 'alan@akbkhome.com'; //for testing..
209 //print_r($email);exit;
210 // should we fetch the watch that caused it.. - which should contain the method to call..
211 // --send-to=test@xxx.com
212 if (!empty($opts['send-to'])) {
213 $p->email = $opts['send-to'];
215 if (!empty($email['send-to'])) {
216 $p->email = $email['send-to'];
218 require_once 'Validate.php';
219 if (!Validate::email($p->email, true)) {
220 $ev = $this->addEvent('NOTIFY', $w, "INVALID ADDRESS: " . $p->email);
222 $w->sent = date('Y-m-d H:i:s');
224 $w->event_id = $ev->id;
226 die(date('Y-m-d h:i:s ') . "INVALID ADDRESS: " . $p->email. "\n");
231 $ff = HTML_FlexyFramework::get();
233 $dom = array_pop(explode('@', $p->email));
235 $mxs = $this->mxs($dom);
239 if ($mxs === false) {
240 $ev = $this->addEvent('NOTIFY', $w, "BAD ADDRESS - ". $p->email );
241 $w->sent = date('Y-m-d H:i:s');
243 $w->event_id = $ev->id;
245 die(date('Y-m-d h:i:s') . " - FAILED - BAD EMAIL - {$p->email} \n");
251 // we try for 3 days..
253 if (strtotime($w->act_start) < strtotime('NOW - 1 HOUR')) {
254 // older that 1 hour.
258 if (strtotime($w->act_start) < strtotime('NOW - 1 DAY')) {
262 if (strtotime($w->act_start) < strtotime('NOW - 2 DAY')) {
266 if (strtotime($w->act_start) < strtotime('NOW - 14 DAY')) {
267 $ev = $this->addEvent('NOTIFY', $w, "BAD ADDRESS - ". $p->email );
268 $w->sent = date('Y-m-d H:i:s');
270 $w->event_id = $ev->id;
272 die(date('Y-m-d h:i:s') . " - FAILED - GAVE UP TO OLD - {$p->email} \n");
277 $w->to_email = $p->email;
278 //$this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
279 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
286 require_once 'Mail.php';
288 foreach($mxs as $dom) {
292 if (!isset($ff->Mail['helo'])) {
293 die("config Mail[helo] is not set");
296 $this->debug("Trying SMTP: $dom / HELO {$ff->Mail['helo']}");
297 $mailer = Mail::factory('smtp', array(
299 'localhost' => $ff->Mail['helo'],
303 $res = $mailer->send($p->email, $email['headers'], $email['body']);
311 $ev = $this->addEvent('NOTIFYSENT', $w, "{$w->to_email} - {$email['headers']['Subject']}");
315 $w->sent = date('Y-m-d H:i:s');
316 $w->msgid = $email['headers']['Message-Id'];
317 $w->event_id = $ev->id; // sent ok.. - no need to record it..
320 // enable cc in notify..
321 if (!empty($email['headers']['Cc'])) {
322 $mailer = Mail::factory('smtp', array(
326 $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
327 $mailer->send($email['headers']['Cc'],
328 $email['headers'], $email['body']);
335 die(date('Y-m-d h:i:s') . " - SENT\n");
337 // what type of error..
338 $code = empty($res->userinfo['smtpcode']) ? -1 : $res->userinfo['smtpcode'];
339 if (!empty($res->code) && $res->code == 10001) {
340 // fake greylist if timed out.
345 $this->debug($res->message);
346 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
348 // give up after 2 days..
349 if (in_array($code, array( 421, 450, 451, 452)) && $next_try_min < (2*24*60)) {
351 // check last event for this item..
354 $this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
355 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
357 die(date('Y-m-d h:i:s') . " - GREYLISTED\n");
362 if ($fail || $next_try_min > (2*24*60)) {
363 // fail.. = log and give up..
364 $ev = $this->addEvent('NOTIFY', $w, ($fail ? "FAILED - " : "RETRY TIME EXCEEDED - ") .
366 ($fail ? ($res->userinfo['smtpcode'] . ' : ' .$res->toString()) : " - UNKNOWN ERROR"));
367 $w->sent = date('Y-m-d H:i:s');
369 $w->event_id = $ev->id;
371 die(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED\n"));
374 // handle no host availalbe forever...
375 if (strtotime($w->act_start) < strtotime('NOW - 3 DAYS')) {
376 $ev = $this->addEvent('NOTIFY', $w, "RETRY TIME EXCEEDED - ". $p->email);
377 $w->sent = date('Y-m-d H:i:s');
379 $w->event_id = $ev->id;
381 die(date('Y-m-d h:i:s') . " - FAILED - RETRY TIME EXCEEDED\n");
387 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED:' . $p->email);
388 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
390 die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE\n");
396 $mx_records = array();
397 $mx_weight = array();
399 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
400 if (!checkdnsrr($fqdn)) {
406 asort($mx_weight,SORT_NUMERIC);
408 foreach($mx_weight as $k => $weight) {
409 $mxs[] = $mx_records[$k];
415 * wrapper to call object->toEmail()
419 headers : {AssocArray},
423 error : {String} // error message in log.
424 send-to: {String} // use to override rcpt
428 function makeEmail($object, $rcpt, $last_sent_date, $notify, $force =false)
430 $m = 'notify'. $notify->evtype;
433 if (!empty($notify->evtype) && method_exists($object,$m)) {
434 return $object->$m($rcpt, $last_sent_date, $notify, $force);
438 return $object->toEmail($rcpt, $last_sent_date, $notify, $force);
443 if (empty($this->cli_args['debug'])) {