<?php



// cpanel user (the one you login to cpanel)
define(&#39;CPANELUSER&#39;,&#39;cpanel-username-here&#39;

// cpanel password (the one you login to cpanel)
define(&#39;CPANELPASS&#39;,&#39;cpanel-password-here&#39;

// your cpanel domain (localhost, or domain name)
define(&#39;CPANELDOMAIN&#39;,&#39;localhost&#39;

// cPanel skin (usually "x")
define(&#39;CPANEL_SKIN&#39;,&#39;x&#39;

// Allow multiple forwarders for the same email?
// true - allow, false - disallow
define(&#39;ALLOW_MULTIPLE&#39;, false);

////////////////////////////////////////////////////////////////
/////////////////////// END OF SETTINGS ////////////////////////
////////////////////////////////////////////////////////////////

function getVar($name, $def = &#39;&#39 {
if (isset($_REQUEST[$name]) && ($_REQUEST[$name] != &#39;&#39)
return $_REQUEST[$name];
else
return $def;
}

$cpuser = CPANELUSER;
$cppass = CPANELPASS;
$cpdomain = CPANELDOMAIN;
$cpskin = CPANEL_SKIN;

$euser=getVar(&#39;euser&#39;
$edomain=getVar(&#39;edomain&#39;
$eforward = getVar(&#39;eforward&#39;

if (empty($euser) || empty($edomain) || empty($eforward)) {
echo "All fields are required to create email forwarding:

<form method=&#39;post&#39;>
Username:<input name=&#39;euser&#39; value=&#39;$euser&#39;> (sample: john)

Domain:<input name=&#39;edomain&#39; value=&#39;$edomain&#39;> (sample: mysite.com)

Redirect to:<input name=&#39;eforward&#39; value=&#39;$eforward&#39;> (sample: jimm@site2.com)

<input type=&#39;submit&#39; value=&#39;Create forwarder&#39; style=&#39;border:1px solid black&#39;>
</form>";
die();
}

if (!ALLOW_MULTIPLE) {
// get list of existing forwarders for this email
$forwarders = array();
preg_match_all(&#39;/\?email=&#39; . $euser. &#39;@&#39; . $edomain . &#39;=([^"]*)/&#39;, file_get_contents("http://$cpuser:$cppass@$cpdomain:2082/frontend/$cpskin/mail/fwds.html"), $forwarders);

if (count($forwarders[1]) > 0) {
die("Email forwarder for this account already exists.");
}
} // ALLOW_MULTIPLE

// Create email forwarder
$f = fopen
("http://$cpuser:$cppass@$cpdomain:2082/frontend/$cpskin/mail/doaddfwd.html?email=$euser&domain=$edomain&forward =$eforward",
"r");
if (!$f) {
die(&#39;Cannot create forwarding. Possible reasons: "fopen" function disallowed on your server, or PHP is running in SAFE mode&#39;
}

$text = "";

// Check result
while (!feof ($f)) {
$text = $text . fgets ($f, 1024);;
}
fclose($f);

if (ereg ("failure", $text, $out) || !strpos($text,"redirected")) {
die(&#39;Cannot create email forwarding.&#39; . $text);
}

echo "Email Forwarder created: $euser@$edomain =&gt; $eforward";

?>