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'])) {
101 $sent = (empty($w->sent) || preg_match('/^0000/', $w->sent)) ? false : true;
103 if (!$force && (!empty($w->msgid) || $sent)) {
106 $w->sent = $w->sqlValue("NOW()");
109 die("message has been sent already.\n");
117 $ev = $this->addEvent('NOTIFY', $w,
118 "Notification event cleared (underlying object does not exist)" );;
120 $w->sent = date('Y-m-d H:i:s');
122 $w->event_id = $ev->id;
124 die(date('Y-m-d h:i:s ') .
125 "Notification event cleared (underlying object does not exist)"
133 if (isset($p->active) && empty($p->active)) {
134 $ev = $this->addEvent('NOTIFY', $w,
135 "Notification event cleared (not user not active any more)" );;
137 $w->sent = date('Y-m-d H:i:s');
139 $w->event_id = $ev->id;
141 die(date('Y-m-d h:i:s ') .
142 "Notification event cleared (not user not active any more)"
144 die("message has been sent already.\n");
148 // let's work out the last notification sent to this user..
149 $l = DB_DataObject::factory($this->table);
151 'ontable' => $w->ontable,
153 'person_id' => $w->person_id,
155 $l->whereAdd('id != '. $w->id);
156 $l->orderBy('sent DESC');
158 $ar = $l->fetchAll('sent');
159 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
162 $ev = DB_DataObject::factory('Events');
163 $ev->on_id = $w->id; // int(11)
164 $ev->on_table = $this->table;
166 $ev->orderBy('event_when DESC');
167 $ar = $ev->fetchAll('event_when');
168 $last_event = empty($ar) ? 0 : $ar[0];
171 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
173 $next_try = $next_try_min . ' MINUTES';
175 // this may modify $p->email. (it will not update it though)
176 $email = $this->makeEmail($o, $p, $last, $w, $force);
179 if ($email === true) {
181 $ev = $this->addEvent('NOTIFY', $w,
182 "Notification event cleared (not required any more)" );;
184 $w->sent = date('Y-m-d H:i:s');
186 $w->event_id = $ev->id;
188 die(date('Y-m-d h:i:s ') .
189 "Notification event cleared (not required any more)"
195 if ($email === false || isset($email['error'])) {
196 // object returned 'false' - it does not know how to send it..
197 $ev = $this->addEvent('NOTIFY', $w, isset($email['error']) ?
198 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable);
200 $w->sent = date('Y-m-d H:i:s');
202 $w->event_id = $ev->id;
204 die(date('Y-m-d h:i:s ') .
205 (isset($email['error']) ?
206 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable)
214 if (isset($email['later'])) {
216 $w->act_when = $email['later'];
218 die(date('Y-m-d h:i:s ') . " Delivery postponed by email creator");
222 if (empty($email['headers']['Message-Id'])) {
223 $HOST = gethostname();
224 $email['headers']['Message-Id'] = "<{$this->table}-{$id}@{$HOST}>";
227 //$p->email = 'alan@akbkhome.com'; //for testing..
228 //print_r($email);exit;
229 // should we fetch the watch that caused it.. - which should contain the method to call..
230 // --send-to=test@xxx.com
232 if (!empty($email['send-to'])) {
233 $p->email = $email['send-to'];
235 if (!empty($opts['send-to'])) {
236 $p->email = $opts['send-to'];
238 // since some of them have spaces?!?!
239 $p->email = trim($p->email);
243 require_once 'Validate.php';
244 if (!Validate::email($p->email, true)) {
245 $ev = $this->addEvent('NOTIFY', $w, "INVALID ADDRESS: " . $p->email);
247 $w->sent = date('Y-m-d H:i:s');
249 $w->event_id = $ev->id;
251 die(date('Y-m-d h:i:s ') . "INVALID ADDRESS: " . $p->email. "\n");
256 $ff = HTML_FlexyFramework::get();
258 $dom = array_pop(explode('@', $p->email));
260 $mxs = $this->mxs($dom);
264 if ($mxs === false) {
265 $ev = $this->addEvent('NOTIFY', $w, "BAD ADDRESS - ". $p->email );
266 $w->sent = date('Y-m-d H:i:s');
268 $w->event_id = $ev->id;
270 die(date('Y-m-d h:i:s') . " - FAILED - BAD EMAIL - {$p->email} \n");
276 // we try for 3 days..
278 if (strtotime($w->act_start) < strtotime('NOW - 1 HOUR')) {
279 // older that 1 hour.
283 if (strtotime($w->act_start) < strtotime('NOW - 1 DAY')) {
287 if (strtotime($w->act_start) < strtotime('NOW - 2 DAY')) {
291 if (strtotime($w->act_start) < strtotime('NOW - 14 DAY')) {
292 $ev = $this->addEvent('NOTIFY', $w, "BAD ADDRESS - ". $p->email );
293 $w->sent = date('Y-m-d H:i:s');
295 $w->event_id = $ev->id;
297 die(date('Y-m-d h:i:s') . " - FAILED - GAVE UP TO OLD - {$p->email} \n");
302 $w->to_email = $p->email;
303 //$this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
304 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
310 require_once 'Mail.php';
312 foreach($mxs as $dom) {
316 if (!isset($ff->Mail['helo'])) {
317 die("config Mail[helo] is not set");
320 $this->debug("Trying SMTP: $dom / HELO {$ff->Mail['helo']}");
321 $mailer = Mail::factory('smtp', array(
323 'localhost' => $ff->Mail['helo'],
327 $res = $mailer->send($p->email, $email['headers'], $email['body']);
333 $ev = $this->addEvent('NOTIFYSENT', $w, "{$w->to_email} - {$email['headers']['Subject']}");
337 $w->sent = date('Y-m-d H:i:s');
338 $w->msgid = $email['headers']['Message-Id'];
339 $w->event_id = $ev->id; // sent ok.. - no need to record it..
342 // enable cc in notify..
343 if (!empty($email['headers']['Cc'])) {
344 $mailer = Mail::factory('smtp', array(
348 $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
349 $mailer->send($email['headers']['Cc'],
350 $email['headers'], $email['body']);
354 if (!empty($email['bcc'])) {
355 $mailer = Mail::factory('smtp', array(
359 $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
360 $mailer->send($email['bcc'],
361 $email['headers'], $email['body']);
366 die(date('Y-m-d h:i:s') . " - SENT\n");
368 // what type of error..
369 $code = empty($res->userinfo['smtpcode']) ? -1 : $res->userinfo['smtpcode'];
370 if (!empty($res->code) && $res->code == 10001) {
371 // fake greylist if timed out.
376 $this->debug($res->message);
377 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
379 // give up after 2 days..
380 if (in_array($code, array( 421, 450, 451, 452)) && $next_try_min < (2*24*60)) {
382 // check last event for this item..
385 $this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
386 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
388 die(date('Y-m-d h:i:s') . " - GREYLISTED\n");
393 if ($fail || $next_try_min > (2*24*60)) {
394 // fail.. = log and give up..
395 $ev = $this->addEvent('NOTIFY', $w, ($fail ? "FAILED - " : "RETRY TIME EXCEEDED - ") .
397 ($fail ? ($res->userinfo['smtpcode'] . ' : ' .$res->toString()) : " - UNKNOWN ERROR"));
398 $w->sent = date('Y-m-d H:i:s');
400 $w->event_id = $ev->id;
402 die(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED\n"));
405 // handle no host availalbe forever...
406 if (strtotime($w->act_start) < strtotime('NOW - 3 DAYS')) {
407 $ev = $this->addEvent('NOTIFY', $w, "RETRY TIME EXCEEDED - ". $p->email);
408 $w->sent = date('Y-m-d H:i:s');
410 $w->event_id = $ev->id;
412 die(date('Y-m-d h:i:s') . " - FAILED - RETRY TIME EXCEEDED\n");
418 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED:' . $p->email);
419 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
421 die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE\n");
427 $mx_records = array();
428 $mx_weight = array();
430 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
431 if (!checkdnsrr($fqdn)) {
437 asort($mx_weight,SORT_NUMERIC);
439 foreach($mx_weight as $k => $weight) {
440 $mxs[] = $mx_records[$k];
446 * wrapper to call object->toEmail()
450 headers : {AssocArray},
454 error : {String} // error message in log.
455 send-to: {String} // use to override rcpt
459 function makeEmail($object, $rcpt, $last_sent_date, $notify, $force =false)
461 $m = 'notify'. $notify->evtype;
464 if (!empty($notify->evtype) && method_exists($object,$m)) {
465 return $object->$m($rcpt, $last_sent_date, $notify, $force);
468 if (!method_exists($object, 'toEmail')) {
472 return $object->toEmail($rcpt, $last_sent_date, $notify, $force);
477 if (empty($this->cli_args['debug'])) {