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");
115 $ev = $this->addEvent('NOTIFY', $w,
116 "Notification event cleared (underlying object does not exist)" );;
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 (underlying object does not exist)"
131 if (isset($p->active) && empty($p->active)) {
132 $ev = $this->addEvent('NOTIFY', $w,
133 "Notification event cleared (not user not active any more)" );;
135 $w->sent = date('Y-m-d H:i:s');
137 $w->event_id = $ev->id;
139 die(date('Y-m-d h:i:s ') .
140 "Notification event cleared (not user not active any more)"
142 die("message has been sent already.\n");
146 // let's work out the last notification sent to this user..
147 $l = DB_DataObject::factory($this->table);
149 'ontable' => $w->ontable,
151 'person_id' => $w->person_id,
153 $l->whereAdd('id != '. $w->id);
154 $l->orderBy('sent DESC');
156 $ar = $l->fetchAll('sent');
157 $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
160 $ev = DB_DataObject::factory('Events');
161 $ev->on_id = $w->id; // int(11)
162 $ev->on_table = $this->table;
164 $ev->orderBy('event_when DESC');
165 $ar = $ev->fetchAll('event_when');
166 $last_event = empty($ar) ? 0 : $ar[0];
169 $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
171 $next_try = $next_try_min . ' MINUTES';
173 // this may modify $p->email. (it will not update it though)
174 $email = $this->makeEmail($o, $p, $last, $w, $force);
177 if ($email === true) {
179 $ev = $this->addEvent('NOTIFY', $w,
180 "Notification event cleared (not required any more)" );;
182 $w->sent = date('Y-m-d H:i:s');
184 $w->event_id = $ev->id;
186 die(date('Y-m-d h:i:s ') .
187 "Notification event cleared (not required any more)"
193 if ($email === false || isset($email['error'])) {
194 // object returned 'false' - it does not know how to send it..
195 $ev = $this->addEvent('NOTIFY', $w, isset($email['error']) ?
196 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable);
198 $w->sent = date('Y-m-d H:i:s');
200 $w->event_id = $ev->id;
202 die(date('Y-m-d h:i:s ') .
203 (isset($email['error']) ?
204 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable)
212 if (isset($email['later'])) {
214 $w->act_when = $email['later'];
216 die(date('Y-m-d h:i:s ') . " Delivery postponed by email creator");
220 if (!isset($email['headers']['Message-Id'])) {
221 $HOST = gethostname();
222 $email['headers']['Message-Id'] = "<{$this->table}-{$id}@{$HOST}>";
225 //$p->email = 'alan@akbkhome.com'; //for testing..
226 //print_r($email);exit;
227 // should we fetch the watch that caused it.. - which should contain the method to call..
228 // --send-to=test@xxx.com
230 if (!empty($email['send-to'])) {
231 $p->email = $email['send-to'];
233 if (!empty($opts['send-to'])) {
234 $p->email = $opts['send-to'];
238 require_once 'Validate.php';
239 if (!Validate::email($p->email, true)) {
240 $ev = $this->addEvent('NOTIFY', $w, "INVALID ADDRESS: " . $p->email);
242 $w->sent = date('Y-m-d H:i:s');
244 $w->event_id = $ev->id;
246 die(date('Y-m-d h:i:s ') . "INVALID ADDRESS: " . $p->email. "\n");
251 $ff = HTML_FlexyFramework::get();
253 $dom = array_pop(explode('@', $p->email));
255 $mxs = $this->mxs($dom);
259 if ($mxs === false) {
260 $ev = $this->addEvent('NOTIFY', $w, "BAD ADDRESS - ". $p->email );
261 $w->sent = date('Y-m-d H:i:s');
263 $w->event_id = $ev->id;
265 die(date('Y-m-d h:i:s') . " - FAILED - BAD EMAIL - {$p->email} \n");
271 // we try for 3 days..
273 if (strtotime($w->act_start) < strtotime('NOW - 1 HOUR')) {
274 // older that 1 hour.
278 if (strtotime($w->act_start) < strtotime('NOW - 1 DAY')) {
282 if (strtotime($w->act_start) < strtotime('NOW - 2 DAY')) {
286 if (strtotime($w->act_start) < strtotime('NOW - 14 DAY')) {
287 $ev = $this->addEvent('NOTIFY', $w, "BAD ADDRESS - ". $p->email );
288 $w->sent = date('Y-m-d H:i:s');
290 $w->event_id = $ev->id;
292 die(date('Y-m-d h:i:s') . " - FAILED - GAVE UP TO OLD - {$p->email} \n");
297 $w->to_email = $p->email;
298 //$this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
299 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
306 require_once 'Mail.php';
308 foreach($mxs as $dom) {
312 if (!isset($ff->Mail['helo'])) {
313 die("config Mail[helo] is not set");
316 $this->debug("Trying SMTP: $dom / HELO {$ff->Mail['helo']}");
317 $mailer = Mail::factory('smtp', array(
319 'localhost' => $ff->Mail['helo'],
323 $res = $mailer->send($p->email, $email['headers'], $email['body']);
331 $ev = $this->addEvent('NOTIFYSENT', $w, "{$w->to_email} - {$email['headers']['Subject']}");
335 $w->sent = date('Y-m-d H:i:s');
336 $w->msgid = $email['headers']['Message-Id'];
337 $w->event_id = $ev->id; // sent ok.. - no need to record it..
340 // enable cc in notify..
341 if (!empty($email['headers']['Cc'])) {
342 $mailer = Mail::factory('smtp', array(
346 $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
347 $mailer->send($email['headers']['Cc'],
348 $email['headers'], $email['body']);
355 die(date('Y-m-d h:i:s') . " - SENT\n");
357 // what type of error..
358 $code = empty($res->userinfo['smtpcode']) ? -1 : $res->userinfo['smtpcode'];
359 if (!empty($res->code) && $res->code == 10001) {
360 // fake greylist if timed out.
365 $this->debug($res->message);
366 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
368 // give up after 2 days..
369 if (in_array($code, array( 421, 450, 451, 452)) && $next_try_min < (2*24*60)) {
371 // check last event for this item..
374 $this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
375 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
377 die(date('Y-m-d h:i:s') . " - GREYLISTED\n");
382 if ($fail || $next_try_min > (2*24*60)) {
383 // fail.. = log and give up..
384 $ev = $this->addEvent('NOTIFY', $w, ($fail ? "FAILED - " : "RETRY TIME EXCEEDED - ") .
386 ($fail ? ($res->userinfo['smtpcode'] . ' : ' .$res->toString()) : " - UNKNOWN ERROR"));
387 $w->sent = date('Y-m-d H:i:s');
389 $w->event_id = $ev->id;
391 die(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED\n"));
394 // handle no host availalbe forever...
395 if (strtotime($w->act_start) < strtotime('NOW - 3 DAYS')) {
396 $ev = $this->addEvent('NOTIFY', $w, "RETRY TIME EXCEEDED - ". $p->email);
397 $w->sent = date('Y-m-d H:i:s');
399 $w->event_id = $ev->id;
401 die(date('Y-m-d h:i:s') . " - FAILED - RETRY TIME EXCEEDED\n");
407 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED:' . $p->email);
408 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
410 die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE\n");
416 $mx_records = array();
417 $mx_weight = array();
419 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
420 if (!checkdnsrr($fqdn)) {
426 asort($mx_weight,SORT_NUMERIC);
428 foreach($mx_weight as $k => $weight) {
429 $mxs[] = $mx_records[$k];
435 * wrapper to call object->toEmail()
439 headers : {AssocArray},
443 error : {String} // error message in log.
444 send-to: {String} // use to override rcpt
448 function makeEmail($object, $rcpt, $last_sent_date, $notify, $force =false)
450 $m = 'notify'. $notify->evtype;
453 if (!empty($notify->evtype) && method_exists($object,$m)) {
454 return $object->$m($rcpt, $last_sent_date, $notify, $force);
457 if (!method_exists($object, 'toEmail')) {
461 return $object->toEmail($rcpt, $last_sent_date, $notify, $force);
466 if (empty($this->cli_args['debug'])) {