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");
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->on_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, $w);
136 if ($email === true) {
138 $ev = $this->addEvent('NOTIFY', $w,
139 "Notification event cleared (not required any more)" );;
141 $w->sent = date('Y-m-d H:i:s');
143 $w->event_id = $ev->id;
145 die(date('Y-m-d h:i:s ') .
146 "Notification event cleared (not required any more)"
152 if ($email === false || isset($email['error'])) {
153 // object returned 'false' - it does not know how to send it..
154 $ev = $this->addEvent('NOTIFY', $w, isset($email['error']) ?
155 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable);
157 $w->sent = date('Y-m-d H:i:s');
159 $w->event_id = $ev->id;
161 die(date('Y-m-d h:i:s ') .
162 (isset($email['error']) ?
163 $email['error'] : "INTERNAL ERROR - We can not handle " . $w->ontable)
171 if (isset($email['later'])) {
173 $w->act_when = $email['later'];
175 die(date('Y-m-d h:i:s ') . " Delivery postponed by email creator");
179 if (!isset($email['headers']['Message-Id'])) {
180 $HOST = gethostname();
181 $email['headers']['Message-Id'] = "<{$this->table}-{$id}@{$HOST}>";
184 //$p->email = 'alan@akbkhome.com'; //for testing..
185 //print_r($email);exit;
186 // should we fetch the watch that caused it.. - which should contain the method to call..
187 // --send-to=test@xxx.com
188 if (!empty($opts['send-to'])) {
189 $p->email = $opts['send-to'];
192 require_once 'Validate.php';
193 if (!Validate::email($p->email, true)) {
194 $ev = $this->addEvent('NOTIFY', $w, "INVALID ADDRESS: " . $p->email);
196 $w->sent = date('Y-m-d H:i:s');
198 $w->event_id = $ev->id;
200 die(date('Y-m-d h:i:s ') . "INVALID ADDRESS: " . $p->email. "\n");
205 $ff = HTML_FlexyFramework::get();
207 $dom = array_pop(explode('@', $p->email));
209 $mxs = $this->mxs($dom);
213 if ($mxs === false) {
214 $ev = $this->addEvent('NOTIFY', $w, "BAD ADDRESS - ". $p->email );
215 $w->sent = date('Y-m-d H:i:s');
217 $w->event_id = $ev->id;
219 die(date('Y-m-d h:i:s') . " - FAILED - BAD EMAIL - {$p->email} \n");
227 require_once 'Mail.php';
229 foreach($mxs as $dom) {
233 if (!isset($ff->Mail['helo'])) {
234 die("config Mail[helo] is not set");
237 $this->debug("Trying SMTP: $dom / HELO {$ff->Mail['helo']}");
238 $mailer = Mail::factory('smtp', array(
240 'localhost' => $ff->Mail['helo'],
243 $res = $mailer->send($p->email, $email['headers'], $email['body']);
247 $ev = $this->addEvent('NOTIFY', $w, 'SUCCESS: ' .$email['headers']['Subject']);
251 $w->sent = date('Y-m-d H:i:s');
252 $w->msgid = $email['headers']['Message-Id'];
253 $w->event_id = $ev->id; // sent ok.. - no need to record it..
256 // enable cc in notify..
257 if (!empty($email['headers']['Cc'])) {
258 $mailer = Mail::factory('smtp', array(
262 $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
263 $mailer->send($email['headers']['Cc'],
264 $email['headers'], $email['body']);
271 die(date('Y-m-d h:i:s') . " - SENT\n");
273 // what type of error..
274 $code = empty($res->userinfo['smtpcode']) ? -1 : $res->userinfo['smtpcode'];
276 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
278 // give up after 2 days..
279 if (in_array($code, array( 421, 450, 451, 452)) && $next_try_min < (2*24*60)) {
281 // check last event for this item..
282 $this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
283 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
285 die(date('Y-m-d h:i:s') . " - GREYLISTED\n");
290 if ($fail || $next_try_min > (2*24*60)) {
291 // fail.. = log and give up..
292 $ev = $this->addEvent('NOTIFY', $w, "RETRY TIME EXCEEDED - ".
294 ($fail ? ($res->userinfo['smtpcode'] . ' : ' .$res->toString()) : " - UNKNOWN ERROR"));
295 $w->sent = date('Y-m-d H:i:s');
297 $w->event_id = $ev->id;
299 die(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED\n"));
302 // handle no host availalbe forever...
303 if (strtotime($w->act_start) < strtotime('NOW - 3 DAYS')) {
304 $ev = $this->addEvent('NOTIFY', $w, "RETRY TIME EXCEEDED - ". $p->email);
305 $w->sent = date('Y-m-d H:i:s');
307 $w->event_id = $ev->id;
309 die(date('Y-m-d h:i:s') . " - FAILED - RETRY TIME EXCEEDED\n");
315 $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED:' . $p->email);
316 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
318 die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE\n");
324 $mx_records = array();
325 $mx_weight = array();
327 if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
328 if (!checkdnsrr($fqdn)) {
334 asort($mx_weight,SORT_NUMERIC);
336 foreach($mx_weight as $k => $weight) {
337 $mxs[] = $mx_records[$k];
343 * wrapper to call object->toEmail()
347 headers : {AssocArray},
351 error : {String} // error message in log.
352 send-to: {String} // use to override rcpt
356 function makeEmail($object, $rcpt, $last_sent_date, $notify)
358 return $object->toEmail($rcpt, $last_sent_date, $notify);
363 if (empty($this->cli_args['debug'])) {