Web Hosting Talk







View Full Version : Cpanel, remote mail and localdomains / remotedomains


hknight
07-18-2007, 12:47 PM
Hello,

I use Cpanel and WHM / (with Linux and PHP) to host websites for all my clients.

I own and manage my own server and have full root access.

I use a third-party service to provide email for all my clients.

So will NEVER provide email services for ANY of my clients.

The problem is that contact forms don't work-- my server won't send them an email.

This can be fixed if I MANUALLY remove each domains entry from /etc/localdomains and add it to /etc/remotedomains.

Every time I get a new client I have to log in as root and do this.

But I am lazy, and use Cpanel/WHM so I DON'T have to log in as root.... Everything else is automated.

Is there a simple way to hack this so I NEVER have to do this again?

Thanks!

DaveDark
07-19-2007, 11:11 AM
You can use /scripts/postwwwacct to make changes each time an account is created. Just create /scripts/postwwwacct and whatever you put in it will be run each time an account is created. You can grab the domain name and remove the domain from localdomains and add it to remotedomains with a script. Examples of grabbing the domain name:

Accessing the data in PHP:

‹?php

$opts = array();
$argv0 = array_shift($argv);
while(count($argv)) {
$key = array_shift($argv);
$value = array_shift($argv);
$opts[$key] = $value;
}
?›
$domain = $opts['domain'];

Accessing the data in Perl:

my %OPTS = @ARGV;
my $domain = $OPTS{'domain'};

hknight
07-19-2007, 12:44 PM
Thanks, Dave.

Your idea sounds promising however I don't fully understand.

If I put php code in /scripts/postwwwacct it will be executed?

The following PHP code deletes everything from '/etc/localdomains' and adds a domain to '/etc/remotedomains'

So what exactly should I do with my PHP code?

<?php

$localDomains = '/etc/localdomains'; // Remove from this file
$remoteDomains = '/etc/remotedomains'; // Add to this file

$opts = array();
$argv0 = array_shift($argv);

while(count($argv)) {
$key = array_shift($argv);
$value = array_shift($argv);
$opts[$key] = $value;
}

$newDomain = $opts['domain'];

$f=fopen($localDomains,"w");
fclose($f);

$file=file_get_contents($remoteDomains);
if (strpos($file, $newDomain)===false){
file_put_contents($remoteDomains, $file."\n".$newDomain);
}

?>

cPanelDavidG
07-19-2007, 01:34 PM
Thanks, Dave.

Your idea sounds promising however I don't fully understand.

If I put php code in /scripts/postwwwacct it will be executed?

The following PHP code deletes everything from '/etc/localdomains' and adds a domain to '/etc/remotedomains'

So what exactly should I do with my PHP code?

<?php

$localDomains = '/etc/localdomains'; // Remove from this file
$remoteDomains = '/etc/remotedomains'; // Add to this file

$opts = array();
$argv0 = array_shift($argv);

while(count($argv)) {
$key = array_shift($argv);
$value = array_shift($argv);
$opts[$key] = $value;
}

$newDomain = $opts['domain'];

$f=fopen($localDomains,"w");
fclose($f);

$file=file_get_contents($remoteDomains);
if (strpos($file, $newDomain)===false){
file_put_contents($remoteDomains, $file."\n".$newDomain);
}

?>

Keep in mind that /scripts/postwwwacct is essentially a shell script. What you intend to do is actually write a shell script in PHP. So, keep your <?php .. ?> tags (and everything between them of course), but above the first line put a hashbang (like you would with Perl) as follows:

#!/usr/bin/php -q

I use the -q parameter to suppress output from the PHP interpreter.

Throwing the code into that file without the hashbang will result in a non-functioning script.

If you're not familiar with shell scripting with PHP, do an Internet search for "Shell Scripting in PHP" for some tutorials and to learn the basics.

hknight
07-19-2007, 03:37 PM
Thank you both! I have run a few tests and is works perfectly!

Bluesplinter
08-03-2007, 08:53 AM
I didn't particularly want to restart an old thread, but I had the same need as the original poster... with a little tweak. For my box to properly email cron, logwatch and other notices, I have to keep the full hostname of the server in the localdomains file. Plus, I may have need in the future to keep the odd domain in localdomains. The postwwwacct script above empties everything from the localdomains file, so I made a coupla tweaks, and am posting it for the next guy who runs into this problem.

<?php

$localDomains = '/etc/localdomains'; // Remove from this file
$remoteDomains = '/etc/remotedomains'; // Add to this file

$opts = array();
$argv0 = array_shift($argv);

while(count($argv)) {
$key = array_shift($argv);
$value = array_shift($argv);
$opts[$key] = $value;
}

$newDomain = $opts['domain'];

//load file into $fc array
$fc=file($localDomains);

//open same file and use "w" to clear file
$f=fopen($localDomains,"w");

//loop through array, strip out $newDomain
foreach($fc as $line)
{
if (!strstr($line,$newDomain)) //look for $newDomain in each line
fputs($f,$line); //place $line back in file
}
fclose($f);

// open remotedomains, append new domain
$file = fopen($remoteDomains, 'a');
fwrite($file, $newDomain);
fclose($file);
?>
Testing shows this adds new domains to the remotedomains file, but leaves existing entries in the localdomains file (which is what I need). Any improvements will be VERY welcome :)