Mercury##

Registered
Jan 31, 2013
1
0
1
cPanel Access Level
Root Administrator
Hi

I'm using the script below to create a email forwarder

It basically create a forwarder for [email protected] to anything entered in the form name="forward"

example : [email protected] --> [email protected]

But can anyone help me with coding that first checks if the the [email protected] maybe already exists, if so then it break;


Code:
<?php

include("./xmlapi.php");   //XMLAPI cpanel client class

$ip = "localhost";           
$account = "account_name";       
$passwd ="password";    
$port =2083;                 

$email_domain = 'domain.com'; 


function getVar($name, $def = '') {
  if (isset($_REQUEST[$name]))
    return $_REQUEST[$name];
  else
    return $def;
}

$dest_email = getVar('forward', '');
$email_user =getVar('new_email_forward', '');

if (!empty($email_user))
while(true) {

$xmlapi = new xmlapi($ip);

$xmlapi->set_port($port); 

$xmlapi->password_auth($account, $passwd);  


$call_f  = array(domain=>$email_domain, email=>$email_user, fwdopt=>"fwd", fwdemail=>$dest_email);
$xmlapi->set_debug(0);      

$result_forward = $xmlapi->api2_query($account, "Email", "addforward", $call_f);

?>

<html>
<head><title>Email Forward Creator</title></head>
<body>


<form name="frmEmail" method="post">
<table width="400" border="0">
<tr>
<td>Forwarder:</td>
<td><input name="forward" size="20" type="text" /></td>
</tr>
<tr>
<td>Forward to </td>
<td><input name="new_email_forward" size="20" type="text" /></td>
</tr>

<tr>
<td colspan="2" align="center"><hr /><input name="submit" type="submit" value="Create Forward" /></td></tr>
</table>
</form>
</body>
</html>
<html><body></body></html>
 

tomdchi

Well-Known Member
Feb 24, 2008
138
2
68
Atlanta, GA
cPanel Access Level
DataCenter Provider
I use this to return a list of email accounts. I then loop through them to see if there is a match.

Code:
    public function _account_list()
    {

        $api = $this->cpanel_xmlapi;
        $api->set_port('2087');
        $api->hash_auth('root', $this->serverhash);
        //$api->set_debug(1);//this setting will put output into the error log in the directory that you are calling script from
        $api->set_output('array');
        $response = $api->api2_query('cpanel_user_here', "Email", "listpopswithdisk", array(''));
        return $response;
    }
Note that you have to use the WHM port and root credentials to get this list. In the code above I am using the server hash instead of the root password.