[imp] Fetchmail memories...

Rick Romero rick at havokmon.com
Tue Feb 26 01:54:51 UTC 2013


Quoting Michael M Slusarz <slusarz at horde.org>:

> Quoting cjdl01 <cjdl01 at brokensolstice.com>:
>
>> I have searched the mail list archives, and I didn't really see an  
>> answer to this (though I have seen the question posited a few  
>> times).  Will there ever be a fetchmail setting in imp again, like  
>> there was in horde3?  I (and my users) really liked that function,  
>> and it was nice that anyone could set it up (they didn't need to  
>> get with me).
>
> Fetchmail was worthless in many cases since a fetchmail process  
> could easily exceed the maximum PHP process time/memory limit.
>

I agree.  The best part was the interface, but to login to Horde to  
have Fetchmail run was silly.
I was really hoping it would stick around - I have yet to upgrade in  
part because of this loss of functionality.  Although for me, what's  
really lost was the interface to store/manage the remote host info,  
not the actual fetchmail function.

For me, it's much more useful to extract the data to a fetchmail file.  
  The big issue with running Fetchmail like this is SSL issues, if you  
can't validate the remote cert, Fetchmail will just barf and stop the  
whole process - I think. I've had this for a while, have fun with it.

Looks like I originally wrote this like 10 years ago, so acronyms  
apply with prejudice - IIRC, YMMV, IANAL, blah blah. :)

Notes
1. Below code snippit requires additional field in the Fetchmail prefs  
called 'serverfetch', to trigger creation of fetchmail.rc file entry
2. One line per remote mailbox.
3. Run as (on FreeBSD) -   /usr/local/bin/fetchmail -s -f  
/home/root/fetchmail/fakefilefetchmail --auth password 2>&1 >/dev/null
4. Far from secure, though you only need outgoing connectivity from an  
internal machine.  No need for anyone other than an admin to be on it  
(where they could see user's passwords in a 'ps')
5. All users get dumped to a single instance - watch run times

<important php snippet>

$query=mysql_query("SELECT pref_value,pref_uid from horde.horde_prefs  
where pref_scope LIKE 'imp' and pref_name LIKE 'fm_accounts' ORDER by  
pref_uid");

/* Always dump mysql_error(), just in case */
if (mysql_num_rows($query) == 0){
         die('MySQL Error: ' . mysql_error());
}
$last_email="None";
$email_fm_count=0;
$localdomains=array('localdom1.com', 'localdom2.com');  // Used to  
prevent fetching 'self'


/* Begin Loop of results (One result per uid)*/
for ($i = 0; $i<mysql_num_rows($query); $i++){
   $result = mysql_result($query,$i , 0);
   $email = mysql_result($query,$i,1);
   if (!$result) {
         die('Could not query:' . mysql_error());
   }

   $newarray=unserialize($result);
   //print_r(array_values($newarray));

   // for each array in $newarray
   /* Begin Array loop to create values (possibly multiple arrays per  
result) */
   foreach ($newarray as $curarray){

         if (is_array($curarray)){
             if ($curarray['serverfetch']  == 1){
               if (preg_match("/imap/i", $curarray['driver']) ) {

                 //print_r(array_values($curarray));
                 //echo "ServerFetch is on \n";
                 $enablessl = "";
                 $sslpath = " sslcertpath /usr/local/share/certs/ ";
                 $sslpath ="";
                 $remotebox = "";
                 $proto = "";
                 $keep = "";
                 $fetchall = "";
                 $uidl = "";
                 $fastuidl = "";
                 $v_domain = "";
                 $vl_domain = "";
                 //Limit number of Fetchmail accounts per mailbox
                 if ($last_email == $email ){
                         $email_fm_count++;
                 }else {
                         $email_fm_count=1;
                         $last_email = $email;
                 }
//              echo $last_email . " " . $email_fm_count . "\n";
                 if ($email_fm_count > 8) { break; }  // 8 accounts  
per local mailbox

                 if ($curarray['server'] == "") {break;} //Skip empty server
                 if (strlen( $curarray['server'] ) < 5 ) {break; } //  
Skip bad server
                 if (strlen( $curarray['username'] ) < 3 ) {break; }
                 if ($curarray['username'] == $email) {break;} //Skip  
fetch to self
                 $v_domain = explode('.', $curarray['server']);
                 $v_domain = array_reverse($v_domain);
                 //print_r($v_domain);
                 $vl_domain = $v_domain[1].".". $v_domain[0];
                 //print $vl_domain."\n";
                 if (in_array($vl_domain, $localdomains)) { break; }   
// Skip 'alias' hack

                 if (preg_match("/gmail/i",  
$curarray['server'])){$enablessl="ssl" . $sslpath;}
                 if (preg_match("/slashmail/i",  
$curarray['server'])){$enablessl="ssl" . $sslpath;}
                 if (preg_match("/pop/i", $curarray['protocol'])){
                         $proto="POP3";
                         if (preg_match("/mail.com/i", $curarray['server'])){
                                 $fastuidl = " fastuidl 1 ";
                                 }
                         $uidl = " uidl ";
                         }
                 if (preg_match("/imap/i",  
$curarray['protocol'])){$proto="IMAP";}
                 if (preg_match("/ssl/i",  
$curarray['protocol'])){$enablessl="ssl" . $sslpath;}
                 if ($curarray['rmailbox'] && $proto != "POP3"){  
$remotebox = "folder " . addslashes($curarray['rmailbox']); }
                 if ($curarray['del'] != 1){ $keep = "keep"; }
                 if ($curarray['onlynew'] != 1){$fetchall = "fetchall"; }
                 $fmstring1 = "poll " . $curarray['server'] . $uidl .  
" timeout 5 proto " . $proto;
                 $fmstring2 = " user \"" .  
addslashes($curarray['username']) . "\" pass \"" .  
$curarray['password'] . "\"";
                 $fmstring3 = " smtpname " . $email . $fastuidl;
                 $fmstring4 = " " . $remotebox . " " . $keep . " " .  
$fetchall . " " . $enablessl . $sslpath;
                 //$fmstring5 = " smtphost 172.16.100.51,172.16.100.52 ";
                 $fmstring5 = " smtphost mail.domain.com ";
                 $fmstring = $fmstring1 . $fmstring2 . $fmstring3 .  
$fmstring4 . $fmstring5 . "\n";
                 if (!is_resource($fp)){
                         $fp =  
fopen('/home/root/fetchmail/fakefilefetchmail',"w+");
                 }
                 //echo $fmstring . "\n";
                 // Write the line into the file
                 fwrite($fp,$fmstring);
               }
            }
         }
   }
}

</end important php snippet>




More information about the imp mailing list