Smooth mailing features with linux

I've made a smooth mailing system with my linux server that can configure fetchmail from your browser.
Fetchmail is a program which can recieve mails from multiple mailservers and deliver them to you local mail account on your linux system.

The system works this way:

I have a MySQL database where I store the servername, username and password for each account.
I have a PHP-script that fetches the data from the MySQL database and writes a fake fetchmail configuration file with the data into a directory which Apache has write access to.
Then a have setup a CRON job to copy the fake file and overwrite my .fetchmailrc (fetchmail configuration in my home directory) which runs as root and sets the proper permissions for the file so that fetchmail can use it. Fetchmail requires that the .fetchmailrc file has rwx-------- (700) as permission.

The sample script (PHP) for writing the fake file:

// Open the fakefile with write access
$fp = fopen(fakefile.fetchmail,"w+");


//Build the defaults line
$thedefaults = "defaults proto pop3 fetchall";

// Write the defaults line into the file
fwrite($fp,$thedefaults);

// Fetch the data from the MySQL database
while ($rs = mysql_fetch_array($query)) {
	$themailserver = $rs['mailserver'];
	$theusername = $rs['username'];
	$thepassword = $rs['password'];
    
// Build line 1 
	$thestring1 = "\n\npoll ". $themailserver ."\n";

// Build line 2
	$thestring2 = "no dns\n";

// Build line 3 with the account information which comes from the database
// If the username contains numbers we must close it with ''
if(ereg("[0-9]",$theusername)){
	$thestring3 = "username '". $theusername ."' with password ". $thepassword ." is ". $pop3username ." here;";
} else {
	$thestring3 = "username ". $theusername ." with password ". $thepassword ." is ". $pop3username ." here;";
}

// Build all the lines for HTML output so we can see what's being written to the file
	$thewebstring = $thewebstring ."<br>poll ". $themailserver ."<br>no dns <br>username '". $theusername ."' with password '". $thepassword ."' is ". $pop3username ." here;";"poll ". $themailserver ."\nno dns\nusername '". $theusername ."' with password '". $thepassword ."' is ". $pop3username ." here;";
	
// Write the 3 lines into the file
	fwrite($fp,$thestring1);
	fwrite($fp,$thestring2);
	fwrite($fp,$thestring3);	

}
// Close file and we're done
fclose($fp);

I've made a custom bash script for the CRON job to run:

#!/bin/sh

cp /home/httpd/apache/fakefile.fetchmail /home/<yourusername>/.fetchmailrc
chown <yourusername>.<yourusername> /home/<yourusername>/.fetchmailrc
chmod 700 /home/<yourusername>/.fetchmailrc
#echo "Copying <yourusername>s fetchmailrc done"
#date

I've setup a CRON job that runs this script once an hour.

My Fetchmail runs as deamon every 300 seconds (5 min.) and if I add new accounts into the MySQL database they will be fetch in next hour.

If you want to make this work at your system and having problems you can mail me here: Contact

Author: Kevin Steffer 2001-09-01