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