The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XMLAPI setup forwarder with PHP

Discussion in 'cPanel Developers' started by Mercury##, Jan 31, 2013.

  1. Mercury##

    Mercury## Registered

    Joined:
    Jan 31, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    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_name_to_be_forwarded@domain.com to anything entered in the form name="forward"

    example : email_name_to_be_forwarded@domain.com --> anything@something.com

    But can anyone help me with coding that first checks if the the new_email_forward@domain.com 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>
     
  2. tomdchi

    tomdchi Well-Known Member

    Joined:
    Feb 24, 2008
    Messages:
    116
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    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.
     
Loading...

Share This Page