less retries
[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             
108             $this->errorHandler("send repeat to early\n");
109         }
110         if (!empty($opts['debug'])) {
111             print_r($w);
112             $ff = HTML_FlexyFramework::get();
113             if (!isset($ff->Core_Mailer)) {
114                 $ff->Core_Mailer = array();
115             }
116             HTML_FlexyFramework::get()->Core_Mailer['debug'] = true;
117         }
118         
119         $sent = (empty($w->sent) || preg_match('/^0000/', $w->sent)) ? false : true;
120         
121         if (!$force && (!empty($w->msgid) || $sent)) {
122             $ww = clone($w);
123             if (!$sent) { 
124                 $w->sent = $w->sent == '0000-00-00 00:00:00' ? $w->sqlValue('NOW()') :$w->sent; // do not update if sent.....
125                 $w->update($ww);
126             }    
127             $this->errorHandler("message has been sent already.\n");
128         }
129         
130         $o = $w->object();
131         
132         if ($o === false)  {
133             
134             $ev = $this->addEvent('NOTIFY', $w,
135                             "Notification event cleared (underlying object does not exist)" );;
136             $ww = clone($w);
137             $w->sent = $w->sent == '0000-00-00 00:00:00' ? $w->sqlValue('NOW()') :$w->sent; // do not update if sent.....
138             $w->msgid = '';
139             $w->event_id = $ev->id;
140             $w->update($ww);
141             $this->errorHandler(date('Y-m-d h:i:s ') . 
142                      "Notification event cleared (underlying object does not exist)" 
143                     ."\n");
144         }
145      
146         
147         
148         $p = $w->person();
149         
150         if (isset($p->active) && empty($p->active)) {
151             $ev = $this->addEvent('NOTIFY', $w,
152                             "Notification event cleared (not user not active any more)" );;
153             $ww = clone($w);
154             $w->sent = $w->sent == '0000-00-00 00:00:00' ? $w->sqlValue('NOW()') :$w->sent; // do not update if sent.....
155             $w->msgid = '';
156             $w->event_id = $ev->id;
157             $w->update($ww);
158             $this->errorHandler(date('Y-m-d h:i:s ') . 
159                      "Notification event cleared (not user not active any more)" 
160                     ."\n");
161             $this->errorHandler("message has been sent already.\n");
162         }
163         // has it failed mutliple times..
164         
165         if (!empty($w->field) && isset($p->{$w->field .'_fails'}) && $p->{$w->field .'_fails'} > 9) {
166             $ev = $this->addEvent('NOTIFY', $w,
167                             "Notification event cleared (user has to many failures)" );;
168             $ww = clone($w);
169             $w->sent = $w->sqlValue('NOW()'); // do not update if sent.....
170             $w->msgid = '';
171             $w->event_id = $ev->id;
172             $w->update($ww);
173             $this->errorHandler(date('Y-m-d h:i:s ') . 
174                      "Notification event cleared (user has to many failures)" 
175                     ."\n");
176             $this->errorHandler("user has to many failures.\n");
177         }
178         
179         // let's work out the last notification sent to this user..
180         $l = DB_DataObject::factory($this->table);
181         
182         $lar = array(
183                 'ontable' => $w->ontable,
184                 'onid' => $w->onid,
185         );
186         // only newer version of the database us this..
187         if (isset($w->person_table)) {
188             $personid_col = strtolower($w->person_table).'_id';
189             if (isset($w->{$personid_col})) {
190                 $lar[$personid_col] = $w->{$personid_col};
191             }
192         }
193         
194         
195         $l->setFrom( $lar );       
196         $l->whereAdd('id != '. $w->id);
197         $l->orderBy('sent DESC');
198         $l->limit(1);
199         $ar = $l->fetchAll('sent');
200         $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
201         
202         // find last event..
203         $ev = DB_DataObject::factory('Events');
204         $ev->on_id = $w->id;                           // int(11)
205         $ev->on_table = $this->table;
206         $ev->limit(1);
207         $ev->orderBy('event_when DESC');
208         $ar = $ev->fetchAll('event_when');
209         $last_event = empty($ar) ? 0 : $ar[0];
210         $next_try_min = 5;
211         if ($last_event) {
212             $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
213         }
214         $next_try = $next_try_min . ' MINUTES';
215          
216         // this may modify $p->email. (it will not update it though)
217         $email =  $this->makeEmail($o, $p, $last, $w, $force);
218         
219         if ($email === true)  {
220             
221             $ev = $this->addEvent('NOTIFY', $w,
222                             "Notification event cleared (not required any more)" );;
223             $ww = clone($w);
224             $w->sent = (!$w->sent || $w->sent == '0000-00-00 00:00:00') ? $w->sqlValue('NOW()') : $w->sent; // do not update if sent.....
225             $w->msgid = '';
226             $w->event_id = $ev->id;
227             $w->update($ww);
228             $this->errorHandler(date('Y-m-d h:i:s ') . 
229                      "Notification event cleared (not required any more)" 
230                     ."\n");
231         }
232         if (is_a($email, 'PEAR_Error')) {
233             $email =array(
234                 'error' => $email->toString()
235             );
236         }
237         
238         if (empty($p) && !empty($email['recipients'])) {
239             // make a fake person..
240             $p = (object) array(
241                 'email' => $email['recipients']
242             );
243         }
244          
245         if ($email === false || isset($email['error']) || empty($p)) {
246             // object returned 'false' - it does not know how to send it..
247             $ev = $this->addEvent('NOTIFYFAIL', $w, isset($email['error'])  ?
248                             $email['error'] : "INTERNAL ERROR  - We can not handle " . $w->ontable); 
249             $ww = clone($w);
250             $w->sent = (!$w->sent || $w->sent == '0000-00-00 00:00:00') ? $w->sqlValue('NOW()') : $w->sent; // do not update if sent.....
251             $w->msgid = '';
252             $w->event_id = $ev->id;
253             $w->to_email = $p->email; 
254             $w->update($ww);
255             $this->errorHandler(date('Y-m-d h:i:s ') . 
256                     (isset($email['error'])  ?
257                             $email['error'] : "INTERNAL ERROR  - We can not handle " . $w->ontable)
258                     ."\n");
259         }
260         
261         
262         if (isset($email['later'])) {
263             $old = clone($w);
264             $w->act_when = $email['later'];
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 ($mxs === false) {
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                 $w->update($ww);
343                 $this->errorHandler(date('Y-m-d h:i:s') . " - MX LOOKUP FAILED\n");
344             }
345             
346             $ev = $this->addEvent('NOTIFYFAIL', $w, "BAD ADDRESS - BAD DOMAIN - ". $p->email );
347             $w->sent =   $w->sqlValue('NOW()'); // why not updated - used to leave as is?
348             $w->msgid = '';
349             $w->event_id = $ev->id;
350             $w->to_email = $p->email; 
351             $w->update($ww);
352             $this->errorHandler(date('Y-m-d h:i:s') . " - FAILED -  BAD EMAIL - {$p->email} \n");
353             
354             
355         }
356         
357         
358         
359         
360         if (!$force && strtotime($w->act_start) <  strtotime('NOW - 3 DAY')) {
361             $ev = $this->addEvent('NOTIFYFAIL', $w, "BAD ADDRESS - GIVE UP - ". $p->email );
362             $w->sent =  $w->sqlValue('NOW()'); 
363             $w->msgid = '';
364             $w->event_id = $ev->id;
365             $w->to_email = $p->email; 
366             $w->update($ww);
367             $this->errorHandler(date('Y-m-d h:i:s') . " - FAILED -  GAVE UP TO OLD - {$p->email} \n");
368         }
369         
370         
371         
372         $w->to_email = $p->email; 
373         //$this->addEvent('NOTIFY', $w, 'GREYLISTED ' . $p->email . ' ' . $res->toString());
374         // we can only update act_when if it has not been sent already (only happens when running in force mode..)
375         $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;
376         $w->update($ww);
377         
378         $ww = clone($w);   
379         
380         $fail = false;
381         require_once 'Mail.php';
382         
383         $core_domain = DB_DataObject::factory('core_domain');
384         if(!$core_domain->get('domain', $dom)){
385             $core_domain = DB_DataObject::factory('core_domain');
386             $core_domain->setFrom(array(
387                 'domain' => $dom
388             ));
389             $core_domain->insert();
390         }
391                         
392         foreach($mxs as $mx) {
393             
394             if (!isset($ff->Mail['helo'])) {
395                 $this->errorHandler("config Mail[helo] is not set");
396             }
397             $this->debug_str = '';
398             $this->debug("Trying SMTP: $mx / HELO {$ff->Mail['helo']}");
399             $mailer = Mail::factory('smtp', array(
400                     'host'    => $mx ,
401                     'localhost' => $ff->Mail['helo'],
402                     'timeout' => 15,
403                     'socket_options' =>  isset($ff->Mail['socket_options']) ? $ff->Mail['socket_options'] : null,
404                     //'debug' => isset($opts['debug']) ?  1 : 0,
405                     'debug' => 1,
406                     'debug_handler' => array($this, 'debugHandler')
407             ));
408             
409             // if the host is the mail host + it's authenticated add auth details
410             // this normally will happen if you sent  Pman_Core_NotifySend['host']
411              
412             
413             if (isset($ff->Mail['host']) && $ff->Mail['host'] == $mx && !empty($ff->Mail['auth'] )) {
414                 
415                 $mailer->auth = true;
416                 $mailer->username = $ff->Mail['username'];
417                 $mailer->password = $ff->Mail['password'];        
418             }
419             
420             if(!empty($ff->Core_Notify) && !empty($ff->Core_Notify['routes'])){
421                 
422                 // we might want to regex 'office365 as a mx host 
423                 foreach ($ff->Core_Notify['routes'] as $server => $settings){
424                     if(!in_array($dom, $settings['domains'])){
425                         continue;
426                     }
427                     
428                     // what's the minimum timespan.. - if we have 60/hour.. that's 1 every minute.
429                     // if it's newer that '1' minute...
430                     // then shunt it..
431                     
432                     $settings['rate'] = isset( $settings['rate']) ?  $settings['rate']  : 360;
433                     
434                     $seconds = floor((60 * 60) / $settings['rate']);
435                     
436                     $core_notify = DB_DataObject::factory($this->table);
437                     $core_notify->domain_id = $core_domain->id;
438                     $core_notify->whereAdd("
439                         sent >= NOW() - INTERVAL $seconds SECOND
440                     ");
441                     
442                     if($core_notify->count()){
443                         $old = clone($w);
444                         $w->act_when = date("Y-m-d H:i:s", time() + $seconds);
445                         $w->update($old);
446                         $this->errorHandler(date('Y-m-d h:i:s ') . " Too many emails sent by {$dom}");
447                     }
448                      
449                     
450                     
451                     $mailer->host = $server;
452                     $mailer->auth = isset($settings['auth']) ? $settings['auth'] : true;
453                     $mailer->username = $settings['username'];
454                     $mailer->password = $settings['password'];
455                     if (isset($settings['port'])) {
456                         $mailer->port = $settings['port'];
457                     }
458                     if (isset($settings['socket_options'])) {
459                         $mailer->socket_options = $settings['socket_options'];
460                         
461                     }
462                     
463                     
464                     break;
465                 }
466                 
467             }
468         
469             
470             $res = $mailer->send($p->email, $email['headers'], $email['body']);
471              
472             
473             if ($res === true) {
474                 // success....
475                 
476                 $successEventName = (empty($email['successEventName'])) ? 'NOTIFYSENT' : $email['successEventName'];
477                 
478                 $ev = $this->addEvent($successEventName, $w, "{$w->to_email} - {$email['headers']['Subject']}");
479                 
480                 $ev->writeEventLog($this->debug_str);
481                 
482                 if(strtotime($w->act_when) > strtotime("NOW")){
483                     $w->act_when = date('Y-m-d H:i:s');
484                 }
485                 
486                 $w->sent = (!$w->sent || $w->sent == '0000-00-00 00:00:00') ? $w->sqlValue('NOW()') : $w->sent; // do not update if sent.....
487                 $w->msgid = $email['headers']['Message-Id'];
488                 $w->event_id = $ev->id; // sent ok.. - no need to record it..
489                 $w->domain_id = $core_domain->id;
490                 $w->update($ww);
491                 
492                 // enable cc in notify..
493                 if (!empty($email['headers']['Cc'])) {
494                     $cmailer = Mail::factory('smtp',  isset($ff->Mail) ? $ff->Mail : array() );
495                     $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
496                     $cmailer->send($email['headers']['Cc'],
497                                   $email['headers'], $email['body']);
498                     
499                 }
500                 
501                 if (!empty($email['bcc'])) {
502                     $cmailer = Mail::factory('smtp', isset($ff->Mail) ? $ff->Mail : array() );
503                     $email['headers']['Subject'] = "(CC): " . $email['headers']['Subject'];
504                     $res = $cmailer->send($email['bcc'],
505                                   $email['headers'], $email['body']);
506                     if (!$res || is_a($res, 'PEAR_Error')) {
507                         echo "could not send bcc..\n";
508                     } else {
509                         echo "Sent BCC to {$email['bcc']}\n";
510                     }
511                 }
512                 
513                 
514                 $this->errorHandler(date('Y-m-d h:i:s') . " - SENT {$w->id} - {$w->to_email} \n", true);
515             }
516             // what type of error..
517             $code = empty($res->userinfo['smtpcode']) ? -1 : $res->userinfo['smtpcode'];
518             if (!empty($res->code) && $res->code == 10001) {
519                 // fake greylist if timed out.
520                 $code = 421;
521             }
522             
523             if ($code < 0) {
524                 $this->debug($res->message);
525                 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
526             }
527             // give up after 2 days..
528             if (in_array($code, array( 421, 450, 451, 452))   && $next_try_min < (2*24*60)) {
529                 // try again later..
530                 // check last event for this item..
531                 //$errmsg=  $fail ? ($res->userinfo['smtpcode'] . ': ' .$res->toString()) :  " - UNKNOWN ERROR";
532                 $errmsg=  $res->userinfo['smtpcode'] . ': ' .$res->message ;
533                 if (!empty($res->userinfo['smtptext'])) {
534                     $errmsg=  $res->userinfo['smtpcode'] . ':' . $res->userinfo['smtptext'];
535                 }
536                 //print_r($res);
537                 $this->addEvent('NOTIFY', $w, 'GREYLISTED - ' . $errmsg);
538                 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + ' . $retry . ' MINUTES'));
539                 $w->domain_id = $core_domain->id;
540                 $w->update($ww);
541                 
542                 
543                 $this->errorHandler(date('Y-m-d h:i:s') . " - GREYLISTED -  $errmsg \n");
544             }
545             $fail = true;
546             break;
547         }
548         if ($fail || $next_try_min > (2*24*60)) {
549         // fail.. = log and give up..
550             $errmsg=  $fail ? ($res->userinfo['smtpcode'] . ': ' .$res->toString()) :  " - UNKNOWN ERROR";
551             if (isset($res->userinfo['smtptext'])) {
552                 $errmsg=  $res->userinfo['smtpcode'] . ':' . $res->userinfo['smtptext'];
553             }
554             
555             $ev = $this->addEvent('NOTIFYFAIL', $w, ($fail ? "FAILED - " : "RETRY TIME EXCEEDED - ") .
556                        $errmsg);
557             $w->sent = (!$w->sent || $w->sent == '0000-00-00 00:00:00') ? $w->sqlValue('NOW()') : $w->sent; // do not update if sent.....
558             $w->msgid = '';
559             $w->event_id = $ev->id;
560             $w->domain_id = $core_domain->id;
561             $w->update($ww);
562             $this->errorHandler(date('Y-m-d h:i:s') . ' - FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED\n"));
563         }
564         
565         // handle no host availalbe forever...
566         if (strtotime($w->act_start) < strtotime('NOW - 3 DAYS')) {
567             $ev = $this->addEvent('NOTIFYFAIL', $w, "RETRY TIME EXCEEDED - ". $p->email);
568             $w->sent = (!$w->sent || $w->sent == '0000-00-00 00:00:00') ? $w->sqlValue('NOW()') : $w->sent; // do not update if sent.....
569             $w->msgid = '';
570             $w->event_id = $ev->id;
571             $w->domain_id = $core_domain->id;
572             $w->update($ww);
573             $this->errorHandler(date('Y-m-d h:i:s') . " - FAILED - RETRY TIME EXCEEDED\n");
574         }
575         
576         
577         $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED:' . $p->email);
578         $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
579         $w->domain_id = $core_domain->id;
580         $w->update($ww);
581         $this->errorHandler(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE\n");
582
583         
584     }
585     function mxs($fqdn)
586     {
587         $ff = HTML_FlexyFramework::get();
588         if (isset($ff->Pman_Core_NotifySend['host'])) {
589             return array($ff->Pman_Core_NotifySend['host']);
590         }
591         
592         $mx_records = array();
593         $mx_weight = array();
594         $mxs = array();
595         if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
596             if (!checkdnsrr($fqdn)) {
597                 return false;
598             }
599             return array($fqdn);
600         }
601         
602         asort($mx_weight,SORT_NUMERIC);
603         
604         foreach($mx_weight as $k => $weight) {
605             $mxs[] = $mx_records[$k];
606         }
607         return $mxs;
608     }
609     
610     /**
611      * wrapper to call object->toEmail()
612      *
613      * return
614      *   {
615         headers : {AssocArray},
616         body: {String}
617         
618         // optional..
619         error :  {String} // error message in log.
620         send-to: {String} // use to override rcpt
621          
622      }
623      **/
624     function makeEmail($object, $rcpt, $last_sent_date, $notify, $force =false)
625     {
626         $m = 'notify'. $notify->evtype;
627         //var_dump(get_class($object) . '::' .$m);
628         if (!empty($notify->evtype) && method_exists($object,$m)) {
629             echo "calling :" . get_class($object) . '::' .$m . "\n";
630             return $object->$m($rcpt, $last_sent_date, $notify, $force);
631         }
632         
633         $type = explode('::', $notify->evtype);
634         
635         if(!empty($type[1]) && method_exists($object,$type[1])){
636             $m = $type[1];
637             echo "calling :" . get_class($object) . '::' .$m . "\n";
638             return $object->$m($rcpt, $last_sent_date, $notify, $force);
639         }
640                 
641         if (method_exists($object, 'toMailerData')) {
642             return $object->toMailerData(array(
643                 'rcpts'=>$rcpt,
644                 'person'=>$rcpt, // added as mediaoutreach used this?
645             )); //this is core_email - i think it's only used for testing...
646             //var_Dump($object);
647             //exit;
648         }
649         
650         return $object->toEmail($rcpt, $last_sent_date, $notify, $force);
651     }
652     
653     function debug($str)
654     {
655         if (empty($this->cli_args['debug'])) {
656             return;
657             
658         }
659         echo $str . "\n";
660     }
661     function output()
662     {
663         $this->errorHandler("done\n");
664     }
665     var $debug_str = '';
666     
667     function debugHandler ($smtp, $message)
668     {
669         $this->debug_str .= strlen($this->debug_str) ? "\n" : '';
670         $this->debug_str .= $message;
671         //echo $message ."\n";
672     }
673     
674     function errorHandler($msg, $success = false)
675     {
676         if($this->error_handler == 'exception'){
677             if($success){
678                 throw new Pman_Core_NotifySend_Exception_Success($msg);
679             }
680             
681             throw new Pman_Core_NotifySend_Exception_Fail($msg);
682         }
683         
684         die($msg);
685         
686         
687     }
688 }