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