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