I am using cURL library in this code, if that is not avaailble on your server, you can use sockets.
Craete cPanel Email Forwarder
- Code: Select all
$sUsername = ""; // cPanel Account Username
$sPassword = ""; // cPanel Account Password
$sDomain = ""; // Domain name (without www)
$sSkin = "x"; // cPanel Skin Version (You can get this form the URL after logging into the cPanel - http://.../frontend/[skin-version]/...)
$sEmail = ""; // Email Address
$sHandle = curl_init("http://$sUsername:$sPassword@$sDomain:2082/frontend/$sSkin/mail/dosetdef.html");
@curl_setopt($sHandle, CURLOPT_HEADER, FALSE);
@curl_setopt($sHandle, CURLOPT_RETURNTRANSFER, TRUE);
@curl_setopt($sHandle, CURLOPT_POSTFIELDS, "domain=$sDomain&forward=$sEmail");
$sResponse = @curl_exec($sHandle);
@curl_close($sHandle);
if (@strpos(@strtolower($sResponse), "failure") !== FALSE)
print "Domain Email Forwarder Creation Failed";
else
print "Domain Email Forwarder Created Successfully";
You can also remove the forwarder with the help of following code.
Remove cPanel Email Forwarder
- Code: Select all
$sUsername = ""; // cPanel Account Username
$sPassword = ""; // cPanel Account Password
$sDomain = ""; // Domain name (without www)
$sSkin = "x"; // cPanel Skin Version (You can get this form the URL after logging into the cPanel - http://.../frontend/[skin-version]/...)
$sHandle = curl_init("http://$sUsername:$sPassword@$sDomain:2082/frontend/$sSkin/mail/dosetdef.html");
@curl_setopt($sHandle, CURLOPT_HEADER, FALSE);
@curl_setopt($sHandle, CURLOPT_RETURNTRANSFER, TRUE);
@curl_setopt($sHandle, CURLOPT_POSTFIELDS, "domain=$sDomain&forward=".urlencode(':fail: No Such User Here'));
$sResponse = @curl_exec($sHandle);
@curl_close($sHandle);
if (@strpos(@strtolower($sResponse), "failure") !== FALSE)
print "Domain Email Forwarder Removal Failed";
else
print "Domain Email Forwarder Removed Successfully";
If you are having any issue, don't hesitate to contact me for support.