Skip to content


cPanel Email Forwarder

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

cPanel Email Forwarder

Postby Web Guru » February 7th, 2009, 6:59 am

If you need to place a forwarder for all your email accounts to a specific email address using cPanel, you can also do this with the help of a script in PHP. Just pass the cpanel login info and the forwarder email address and this scripts with setup the forwarder without going into the cpanel.

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.
User avatar
Web Guru
 
Posts: 94
Joined: March 24th, 2008, 7:59 am
Location: Lahore, Pakistan

Return to Board index

Return to PHP / MySQL / XML

Who is online

Users browsing this forum: No registered users and 0 guests