NotifySend.php
[Pman.Core] / NotifySend.php
1 <?php
2 require_once 'Pman.php';
3
4 /**
5  * notification script sender - designed to be run by the Notify script - with many children running
6  * in parallel.
7  *
8  * called with an id of a core_notify element
9  *
10  * uses core_notify - to find an event to object and person.
11  *
12  * uses Events table to log failures
13  * 
14  * 
15  * calls $object->toEmail($person,$last_send, $notify) to generate an email struct with
16  *  array (
17  *      headers =>
18  *      recipients =>
19  *      body =>
20  *  )
21  *
22  *
23  * Note uses configuration
24  *
25  * Pman_Core_NotifySend[host] = 'localhost' << to override direct sending..
26  * Mail[helo] << helo host name
27  * Mail[socket_options] << any socket option.
28  */
29 class Pman_Core_NotifySend_Exception_Success extends Exception {}
30 class Pman_Core_NotifySend_Exception_Fail extends Exception {}
31
32
33 class Pman_Core_NotifySend extends Pman
34 {
35     static $cli_desc = "Send out single notification email (usually called from  Core/Notify)";
36     
37     static $cli_opts = array(
38         'debug' => array(
39             'desc' => 'Turn on debugging (see DataObjects debugLevel )',
40             'default' => 0,
41             'short' => 'v',
42             'min' => 0,
43             'max' => 0,
44             
45         ),
46         'DB_DataObject-debug' => array(
47             'desc' => 'Turn on debugging (see DataObjects debugLevel )',
48             'default' => 0,
49             'short' => 'd',
50             'min' => 1,
51             'max' => 1,
52             
53         ),
54         'force' => array(
55             'desc' => 'Force redelivery, even if it has been sent before or not queued...',
56             'default' => 0,
57             'short' => 'f',
58             'min' => 0,
59             'max' => 0,
60         ),
61         'send-to' => array(
62             'desc' => 'Send the message to this address, rather than the one listed.',
63             'default' => '',
64             'short' => 't',
65             'min' => 0,
66             'max' => 1,
67         )
68         
69         
70         
71     );
72     var $table = 'core_notify';
73     var $error_handler = 'die';
74     
75     function getAuth()
76     {
77         $ff = HTML_FlexyFramework::get();
78         if (!$ff->cli) {
79             $this->errorHandler("access denied");
80         }
81         //HTML_FlexyFramework::ensureSingle(__FILE__, $this);
82         return true;
83         
84     }
85    
86     function get($id,$opts=array())
87     {
88
89         //print_r($opts);
90         if (!empty($opts['DB_DataObject-debug'])) {
91             DB_DataObject::debugLevel($opts['DB_DataObject-debug']);
92         }
93         
94         //DB_DataObject::debugLevel(1);
95         //date_default_timezone_set('UTC');
96         // phpinfo();exit;
97         $force = empty($opts['force']) ? 0 : 1;
98         
99         $w = DB_DataObject::factory($this->table);
100
101         if (!$w->get($id)) {
102             $this->errorHandler("invalid id\n");
103         }
104
105         if (!$force && !empty($w->sent) && strtotime($w->act_when) < strtotime($w->sent)) {
106              
107             $this->errorHandler("already sent - repeat to early\n");
108         }
109         if (!empty($opts['debug'])) {
110             print_r($w);
111             $ff = HTML_FlexyFramework::get();
112             if (!isset($ff->Core_Mailer)) {
113                 $ff->Core_Mailer = array();
114             }
115             HTML_FlexyFramework::get()->Core_Mailer['debug'] = true;
116         }
117         
118         $sent = (empty($w->sent) || preg_match('/^0000/', $w->sent)) ? false : true;
119         
120         if (!$force && (!empty($w->msgid) || $sent)) {
121             $ww = clone($w);
122             if (!$sent) {   // fix sent.
123                 $w->sent = $w->sent == '0000-00-00 00:00:00' ? $w->sqlValue('NOW()') :$w->sent; // do not update if sent.....
124                 $w->update($ww);
125             }    
126             $this->errorHandler("message has been sent already.\n");
127         }
128         
129         $o = $w->object();
130         
131         if ($o === false)  {
132             
133             
134             
135             
136             $ev = $this->addEvent('NOTIFY', $w,
137                             "Notification event cleared (underlying object does not exist)" );
138             $w->flagSent($ev, '');
139              
140             $this->errorHandler(date('Y-m-d h:i:s ') . 
141                      "Notification event cleared (underlying object does not exist)" 
142                     ."\n");
143         }
144      
145         
146         
147         $p = $w->person();
148         
149         if (isset($p->active) && empty($p->active)) {
150             $ev = $this->addEvent('NOTIFY', $w,
151                             "Notification event cleared (not user not active any more)" );;
152              $w->flagSent($ev, '');
153            
154             $this->errorHandler(date('Y-m-d h:i:s ') . 
155                      "Notification event cleared (not user not active any more)" 
156                     ."\n");
157         }
158         // has it failed mutliple times..
159         
160         if (!empty($w->field) && isset($p->{$w->field .'_fails'}) && $p->{$w->field .'_fails'} > 9) {
161             $ev = $this->addEvent('NOTIFY', $w,
162                             "Notification event cleared (user has to many failures)" );;
163             $w->flagSent($ev, '');
164             
165             
166             $this->errorHandler(date('Y-m-d h:i:s ') . $ev->remarks);
167             
168         }
169         
170         // let's work out the last notification sent to this user..
171         $l = DB_DataObject::factory($this->table);
172         
173         $lar = array(
174                 'ontable' => $w->ontable,
175                 'onid' => $w->onid,
176         );
177         // only newer version of the database us this..
178         if (isset($w->person_table)) {
179             $personid_col = strtolower($w->person_table).'_id';
180             if (isset($w->{$personid_col})) {
181                 $lar[$personid_col] = $w->{$personid_col};
182             }
183         }
184         
185         
186         $l->setFrom( $lar );       
187         $l->whereAdd('id != '. $w->id);
188         $l->orderBy('sent DESC');
189         $l->limit(1);
190         $ar = $l->fetchAll('sent');
191         $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
192         
193         // find last event..
194         $ev = DB_DataObject::factory('Events');
195         $ev->on_id = $w->id;                           // int(11)
196         $ev->on_table = $this->table;
197         $ev->limit(1);
198         $ev->orderBy('event_when DESC');
199         $ar = $ev->fetchAll('event_when');
200         $last_event = empty($ar) ? 0 : $ar[0];
201         $next_try_min = 5;
202         if ($last_event) {
203             $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
204         }
205         $next_try = $next_try_min . ' MINUTES';
206          
207         // this may modify $p->email. (it will not update it though)
208         $email =  $this->makeEmail($o, $p, $last, $w, $force);
209         
210         if ($email === true)  {
211             
212             $ev = $this->addEvent('NOTIFY', $w,
213                             "Notification event cleared (not required any more)" );;
214             $ww = clone($w);
215             $w->sent = (!$w->sent || $w->sent == '0000-00-00 00:00:00') ? $w->sqlValue('NOW()') : $w->sent; // do not update if sent.....
216             $w->msgid = '';
217             $w->event_id = $ev->id;
218             $w->update($ww);
219             $this->errorHandler(date('Y-m-d h:i:s ') . 
220                      "Notification event cleared (not required any more)" 
221                     ."\n");
222         }
223         if (is_a($email, 'PEAR_Error')) {
224             $email =array(
225                 'error' => $email->toString()
226             );
227         }
228         
229         if (empty($p) && !empty($email['recipients'])) {
230             // make a fake person..
231             $p = (object) array(
232                 'email' => $email['recipients']
233             );
234         }
235          
236         if ($email === false || isset($email['error']) || empty($p)) {
237             // object returned 'false' - it does not know how to send it..
238             $ev = $this->addEvent('NOTIFYFAIL', $w, isset($email['error'])  ?
239                             $email['error'] : "INTERNAL ERROR  - We can not handle " . $w->ontable); 
240             $ww = clone($w);
241             $w->sent = (!$w->sent || $w->sent == '0000-00-00 00:00:00') ? $w->sqlValue('NOW()') : $w->sent; // do not update if sent.....
242             $w->msgid = '';
243             $w->event_id = $ev->id;
244             $w->to_email = $p->email; 
245             $w->update($ww);
246             $this->errorHandler(date('Y-m-d h:i:s ') . 
247                     (isset($email['error'])  ?
248                             $email['error'] : "INTERNAL ERROR  - We can not handle " . $w->ontable)
249                     ."\n");
250         }
251         
252          
253         
254         if (isset($email['later'])) {
255             $old = clone($w);
256             $w->act_when = $email['later'];
257             $this->updateServer($w);
258             $w->update($old);
259             $this->errorHandler(date('Y-m-d h:i:s ') . " Delivery postponed by email creator to {$email['later']}");
260         }
261         
262          
263         if (empty($email['headers']['Message-Id'])) {
264             $HOST = gethostname();
265             $email['headers']['Message-Id'] = "<{$this->table}-{$id}@{$HOST}>";
266             
267         }
268         
269         
270             
271         
272         //$p->email = 'alan@akbkhome.com'; //for testing..
273         //print_r($email);exit;
274         // should we fetch the watch that caused it.. - which should contain the method to call..
275         // --send-to=test@xxx.com
276        
277         if (!empty($email['send-to'])) {
278             $p->email = $email['send-to'];
279         }
280          if (!empty($opts['send-to'])) {
281             $p->email = $opts['send-to'];
282         }
283         
284             // since some of them have spaces?!?!
285         $p->email = trim($p->email);
286       
287         
288         require_once 'Validate.php';
289         if (!Validate::email($p->email, true)) {
290             $ev = $this->addEvent('NOTIFYFAIL', $w, "INVALID ADDRESS: " . $p->email);
291             $ww = clone($w);
292             $w->sent = (!$w->sent || $w->sent == '0000-00-00 00:00:00') ? $w->sqlValue('NOW()') : $w->sent; // do not update if sent.....
293             $w->msgid = '';
294             $w->event_id = $ev->id;
295             $w->to_email = $p->email; 
296             $w->update($ww);
297             $this->errorHandler(date('Y-m-d h:i:s ') . "INVALID ADDRESS: " . $p->email. "\n");
298             
299         }
300         
301         
302         $ff = HTML_FlexyFramework::get();
303         
304         $explode_email = explode('@', $p->email);
305         $dom = array_pop($explode_email);
306         
307         $mxs = $this->mxs($dom);
308         $ww = clone($w);
309
310         // we might fail doing this...
311         // need to handle temporary failure..
312        
313         
314           // we try for 2 days..
315         $retry = 15;
316         if (strtotime($w->act_start) <  strtotime('NOW - 1 HOUR')) {
317             // older that 1 hour.
318             $retry = 60;
319         }
320         
321         if (strtotime($w->act_start) <  strtotime('NOW - 1 DAY')) {
322             // older that 1 day.
323             $retry = 120;
324         }
325         if (strtotime($w->act_start) <  strtotime('NOW - 2 DAY')) {
326             // older that 1 day.
327             $retry = 240;
328         }
329         
330         if (empty($mxs)) {
331             // only retry for 1 day if the MX issue..
332             if ($retry < 240) {
333                 $this->addEvent('NOTIFY', $w, 'MX LOOKUP FAILED ' . $dom );
334                 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
335                 $this->updateServer($w);
336                 $w->update($ww);
337                 $this->errorHandler(date('Y-m-d h:i:s') . " - MX LOOKUP FAILED {$dom}\n");
338             }
339             
340             $ev = $this->addEvent('NOTIFYFAIL', $w, "BAD ADDRESS - BAD DOMAIN - ". $p->email );
341             $w->sent =   $w->sqlValue('NOW()'); // why not updated - used to leave as is?
342             $w->msgid = '';
343             $w->event_id = $ev->id;
344             $w->to_email = $p->email; 
345             $w->update($ww);
346             $this->errorHandler(date('Y-m-d h:i:s') . " - FAILED -  BAD DOMAIN - {$p->email} \n");
347             
348             
349         }
350         
351         
352         
353         
354         if (!$force && strtotime($w->act_start) <  strtotime('NOW - 3 DAY')) {
355             $ev = $this->addEvent('NOTIFYFAIL', $w, "BAD ADDRESS - GIVE UP - ". $p->email );
356             $w->sent =  $w->sqlValue('NOW()'); 
357             $w->msgid = '';
358             $w->event_id = $ev->id;
359             $w->to_email = $p->email; 
360             $w->update($ww);
361             $this->errorHandler(date('Y-m-d h:i:s') . " - FAILED -  GAVE UP TO OLD - {$p->email} \n");
362         }
363         
364         
365         
366         $w->to_email = $p->email; 
367         //$this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
368         // we can only update act_when if it has not been sent already (only happens when running in force mode..)
369         // set act when if it's empty...
370         $w->act_when =  (!$w->act_when || $w->act_when == '0000-00-00 00:00:00') ? date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES')) : $w->act_when;
371         
372         $w->update($ww);
373         
374         $ww = clone($w);   
375         
376         $fail = false;
377         require_once 'Mail.php';
378         
379         $core_domain = DB_DataObject::factory('core_domain');
380         if(!$core_domain->get('domain', $dom)){
381             $core_domain = DB_DataObject::factory('core_domain');
382             $core_domain->setFrom(array(
383                 'domain' => $dom
384             ));
385             $core_domain->insert();
386         }
387          
388         
389         $this->initHelo();
390         
391         if (!isset($ff->Mail['helo'])) {
392             $this->errorHandler("config Mail[helo] is not set");
393         }
394         
395         
396                         
397         foreach($mxs as $mx) {
398             
399            
400             $this->debug_str = '';
401             $this->debug("Trying SMTP: $mx / HELO {$ff->Mail['helo']}");
402             $mailer = Mail::factory('smtp', array(
403                     'host'    => $mx ,
404                     'localhost' => $ff->Mail['helo'],
405                     'timeout' => 15,
406                     'socket_options' =>  isset($ff->Mail['socket_options']) ? $ff->Mail['socket_options'] : null,
407                     //'debug' => isset($opts['debug']) ?  1 : 0,
408                     'debug' => 1,
409                     'debug_handler' => array($this, 'debugHandler')
410             ));
411             
412             // if the host is the mail host + it's authenticated add auth details
413             // this normally will happen if you sent  Pman_Core_NotifySend['host']
414              
415             
416             if (isset($ff->Mail['host']) && $ff->Mail['host'] == $mx && !empty($ff->Mail['auth'] )) {
417                 
418                 $mailer->auth = true;
419                 $mailer->username = $ff->Mail['username'];
420                 $mailer->password = $ff->Mail['password'];        
421             }
422             
423             if(!empty($ff->Core_Notify) && !empty($ff->Core_Notify['routes'])){
424                 
425                 // we might want to regex 'office365 as a mx host 
426                 foreach ($ff->Core_Notify['routes'] as $server => $settings){
427                     if(!in_array($dom, $settings['domains'])){
428                         continue;
429                     }
430                     
431                     // what's the minimum timespan.. - if we have 60/hour.. that's 1 every minute.
432                     // if it's newer that '1' minute...
433                     // then shunt it..
434                     
435                     $settings['rate'] = isset( $settings['rate']) ?  $settings['rate']  : 360;
436                     
437                     $seconds = floor((60 * 60) / $settings['rate']);
438                     
439                     $core_notify = DB_DataObject::factory($this->table);
440                     $core_notify->domain_id = $core_domain->id;
441                     $core_notify->whereAdd("
442                         sent >= NOW() - INTERVAL $seconds SECOND
443                     ");
444                     
445                     if($core_notify->count()){
446                         $old = clone($w);
447                         $w->act_when = date("Y-m-d H:i:s", time() + $seconds);
448                         $this->updateServer($w);
449                         $w->update($old);
450                         $this->errorHandler(date('Y-m-d h:i:s ') . " Too many emails sent by {$dom}");
451                     }
452                      
453                     
454                     
455                     $mailer->host = $server;
456                     $mailer->auth = isset($settings['auth']) ? $settings['auth'] : true;
457                     $mailer->username = $settings['username'];
458                     $mailer->password = $settings['password'];
459                     if (isset($settings['port'])) {
460                         $mailer->port = $settings['port'];
461                     }
462                     if (isset($settings['socket_options'])) {
463                         $mailer->socket_options = $settings['socket_options'];
464                         
465                     }
466                     
467                     
468                     break;
469                 }
470                 
471             }
472         
473             
474             $res = $mailer->send($p->email, $email['headers'], $email['body']);
475              
476             
477             if ($res === true) {
478                 // success....
479                 
480                 $successEventName = (empty($email['successEventName'])) ? 'NOTIFYSENT' : $email['successEventName'];
481                 
482                 $ev = $this->addEvent($successEventName, $w, "{$w->to_email} - {$email['headers']['Subject']}");
483                 
484                 $ev->writeEventLog($this->debug_str);
485                 
486                 if(strtotime($w->act_when) > strtotime("NOW")){
487                     $w->act_when = date('Y-m-d H:i:s');
488                 }
489                 
490                 $w->sent = (!$w->sent || $w->sent == '0000-00-00 00:00:00') ? $w->sqlValue('NOW()') : $w->sent; // do not update if sent.....
491                 $w->msgid = $email['headers']['Message-Id'];
492                 $w->event_id = $ev->id; // sent ok.. - no need to record it..
493                 $w->domain_id = $core_domain->id;
494                 $w->update($ww);
495                 
496                 // enable cc in notify..
497                 if (!empty($email['headers']['Cc'])) {
498                     $cmailer = Mail::factory('smtp',  isset($ff->Mail) ? $ff->Mail : array() );
499                     $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
500                     $cmailer->send($email['headers']['Cc'],
501                                   $email['headers'], $email['body']);
502                     
503                 }
504                 
505                 if (!empty($email['bcc'])) {
506                     $cmailer = Mail::factory('smtp', isset($ff->Mail) ? $ff->Mail : array() );
507                     $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
508                     $res = $cmailer->send($email['bcc'],
509                                   $email['headers'], $email['body']);
510                     if (!$res || is_a($res, 'PEAR_Error')) {
511                         echo "could not send bcc..\n";
512                     } else {
513                         echo "Sent BCC to {$email['bcc']}\n";
514                     }
515                 }
516                 
517                 
518                 $this->errorHandler(date('Y-m-d h:i:s') . " - SENT {$w->id} - {$w->to_email} \n", true);
519             }
520             // what type of error..
521             $code = empty($res->userinfo['smtpcode']) ? -1 : $res->userinfo['smtpcode'];
522             if (!empty($res->code) && $res->code == 10001) {
523                 // fake greylist if timed out.
524                 $code = 421;
525             }
526             
527             if ($code < 0) {
528                 $this->debug($res->message);
529                 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
530             }
531             // give up after 2 days..
532             if (in_array($code, array( 421, 450, 451, 452))   && $next_try_min < (2*24*60)) {
533                 // try again later..
534                 // check last event for this item..
535                 //$errmsg=  $fail ? ($res->userinfo['smtpcode'] . ': ' .$res->toString()) :  " - UNKNOWN ERROR";
536                 $errmsg=  $res->userinfo['smtpcode'] . ': ' .$res->message ;
537                 if (!empty($res->userinfo['smtptext'])) {
538                     $errmsg=  $res->userinfo['smtpcode'] . ':' . $res->userinfo['smtptext'];
539                 }
540                 //print_r($res);
541                 $this->addEvent('NOTIFY', $w, 'GREYLISTED - ' . $errmsg);
542                 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
543                 $this->updateServer($w);
544                 $w->domain_id = $core_domain->id;
545                 $w->update($ww);
546                 
547                 
548                 $this->errorHandler(date('Y-m-d h:i:s') . " - GREYLISTED -  $errmsg \n");
549             }
550             $fail = true;
551             break;
552         }
553         if ($fail || $next_try_min > (2*24*60)) {
554         // fail.. = log and give up..
555             $errmsg=  $fail ? ($res->userinfo['smtpcode'] . ': ' .$res->toString()) :  " - UNKNOWN ERROR";
556             if (isset($res->userinfo['smtptext'])) {
557                 $errmsg=  $res->userinfo['smtpcode'] . ':' . $res->userinfo['smtptext'];
558             }
559             
560             $ev = $this->addEvent('NOTIFYFAIL', $w, ($fail ? "FAILED - " : "RETRY TIME EXCEEDED - ") .
561                        $errmsg);
562             $w->sent = (!$w->sent || $w->sent == '0000-00-00 00:00:00') ? $w->sqlValue('NOW()') : $w->sent; // do not update if sent.....
563             $w->msgid = '';
564             $w->event_id = $ev->id;
565             $w->domain_id = $core_domain->id;
566             $w->update($ww);
567             $this->errorHandler(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED\n"));
568         }
569         
570         // handle no host availalbe forever...
571         if (strtotime($w->act_start) < strtotime('NOW - 3 DAYS')) {
572             $ev = $this->addEvent('NOTIFYFAIL', $w, "RETRY TIME EXCEEDED - ". $p->email);
573             $w->sent = (!$w->sent || $w->sent == '0000-00-00 00:00:00') ? $w->sqlValue('NOW()') : $w->sent; // do not update if sent.....
574             $w->msgid = '';
575             $w->event_id = $ev->id;
576             $w->domain_id = $core_domain->id;
577             $w->update($ww);
578             $this->errorHandler(date('Y-m-d h:i:s') . " - FAILED - RETRY TIME EXCEEDED\n");
579         }
580         
581         
582         $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED:' . $p->email);
583         $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
584         
585         $this->updateServer($w);
586         
587         $w->domain_id = $core_domain->id;
588         $w->update($ww);
589         $this->errorHandler(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE\n");
590
591         
592     }
593     function mxs($fqdn)
594     {
595         $ff = HTML_FlexyFramework::get();
596         if (isset($ff->Pman_Core_NotifySend['host'])) {
597             return array($ff->Pman_Core_NotifySend['host']);
598         }
599         
600         $mx_records = array();
601         $mx_weight = array();
602         $mxs = array();
603         if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
604             if (!checkdnsrr($fqdn)) {
605                 return false;
606             }
607             return array($fqdn);
608         }
609         
610         asort($mx_weight,SORT_NUMERIC);
611         
612         foreach($mx_weight as $k => $weight) {
613             if (!empty($mx_records[$k])) {
614                 $mxs[] = $mx_records[$k];
615             }
616         }
617         return empty($mxs) ? false : $mxs;
618     }
619     
620     /**
621      * wrapper to call object->toEmail()
622      *
623      * return
624      *   {
625         headers : {AssocArray},
626         body: {String}
627         
628         // optional..
629         error :  {String} // error message in log.
630         send-to: {String} // use to override rcpt
631          
632      }
633      **/
634     function makeEmail($object, $rcpt, $last_sent_date, $notify, $force =false)
635     {
636         $m = 'notify'. $notify->evtype;
637         //var_dump(get_class($object) . '::' .$m);
638         if (!empty($notify->evtype) && method_exists($object,$m)) {
639             echo "calling :" . get_class($object) . '::' .$m . "\n";
640             return $object->$m($rcpt, $last_sent_date, $notify, $force);
641         }
642         
643         $type = explode('::', $notify->evtype);
644         
645         if(!empty($type[1]) && method_exists($object,$type[1])){
646             $m = $type[1];
647             echo "calling :" . get_class($object) . '::' .$m . "\n";
648             return $object->$m($rcpt, $last_sent_date, $notify, $force);
649         }
650         // fallback if evtype is empty..
651         
652         if (method_exists($object, 'toMailerData')) {
653             return $object->toMailerData(array(
654                 'rcpts'=>$rcpt,
655                 'person'=>$rcpt, // added as mediaoutreach used this?
656             )); //this is core_email - i think it's only used for testing...
657             //var_Dump($object);
658             //exit;
659         }
660         if (method_exists($object, 'toEmail')) {
661             return $object->toEmail($rcpt, $last_sent_date, $notify, $force);
662         }
663         // no way to send this.. - this needs to handle core_notify how we have used it for the approval stuff..
664         
665         return false;
666     }
667     
668     function debug($str)
669     {
670         if (empty($this->cli_args['debug'])) {
671             return;
672             
673         }
674         echo $str . "\n";
675     }
676     function output()
677     {
678         $this->errorHandler("done\n");
679     }
680     var $debug_str = '';
681     
682     function debugHandler ($smtp, $message)
683     {
684         $this->debug_str .= strlen($this->debug_str) ? "\n" : '';
685         $this->debug_str .= $message;
686         //echo $message ."\n";
687     }
688     
689     function errorHandler($msg, $success = false)
690     {
691         if($this->error_handler == 'exception'){
692             if($success){
693                 throw new Pman_Core_NotifySend_Exception_Success($msg);
694             }
695             
696             throw new Pman_Core_NotifySend_Exception_Fail($msg);
697         }
698         
699         die($msg ."\n");
700         
701         
702     }
703     
704     function updateServer($w)
705     {
706         $ff = HTML_FlexyFramework::get();
707          
708         if (empty($ff->Core_Notify['servers'])) {
709             return;
710         }
711         // some classes dont support server routing
712         if (!property_exists($w, 'server_id')) {
713             return;
714         }
715         // next server..
716         $w->server_id = ($w->server_id + 1) % count(array_keys($ff->Core_Notify['servers']));
717          
718     }
719     
720      function initHelo()
721     {
722         $ff = HTML_FlexyFramework::get();
723         
724         if (isset($ff->Core_Notify['servers-non-pool'])  &&
725             isset($ff->Core_Notify['servers-non-pool'][gethostname()]) &&
726             isset($ff->Core_Notify['servers-non-pool'][gethostname()]['helo']) ) {
727             $ff->Mail['helo'] = $ff->Core_Notify['servers-non-pool'][gethostname()]['helo'];
728             return;
729         }
730         
731         if (empty($ff->Core_Notify['servers'])) {
732             return;
733         }
734         if (!isset($ff->Core_Notify['servers'][gethostname()]) || !isset($ff->Core_Notify['servers'][gethostname()]['helo']) ) {
735             $this->jerr("Core_Notify['servers']['" . gethostname() . "']['helo'] not set");
736         }
737         $ff->Mail['helo'] = $ff->Core_Notify['servers'][gethostname()]['helo'];
738         
739     }
740     
741 }