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.

[Resolved] PHP Script creates email account but fails to create forwarder

Discussion in 'cPanel Developers' started by baldarab, Mar 17, 2014.

  1. baldarab

    baldarab Member

    Joined:
    Mar 17, 2014
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Hi.

    I have a script I am using to create an email account and forwarder using a POST from an campaign. It is succeeding in creating the email account, but does not create the forwarder. I'd appreciate any help on why that might be. I've scoured the forums, and can't see that the script is non-compliant. Anyway, here it is:

    PHP:
    <?php
    include 'xmlapi.php' ;

    $ip 'nnn.nnn.nnn.nnn';
    $root_pass 'pppppppp';

    $account 'aaaaaa';

    $email_user $_POST['Username'];
    $email_password $_POST['EmailPassword'];
    $dest_email $_POST['ParentEmail'];
    $email_domain 'domain.com';
    $email_quota '10';

    $xmlapi = new xmlapi($ip);
    $xmlapi->set_port(2083);
    $xmlapi->password_auth($account,$root_pass);
    $xmlapi->set_output('xml');

    $result $xmlapi->api1_query($account'Email''addpop', array($email_user$email_password$email_quota$email_domain) );

    $result_forward $xmlapi->api2_query($account'Email''addforward', array($email_domain$email_user'fwd'$dest_email) );
    ?>
     
  2. vanessa

    vanessa Well-Known Member
    PartnerNOC

    Joined:
    Sep 26, 2006
    Messages:
    817
    Likes Received:
    22
    Trophy Points:
    18
    Location:
    Virginia Beach, VA
    cPanel Access Level:
    DataCenter Provider
    The API2 call looks syntactically correct. Have you tried 1) Checking the cPanel error logs, or 2) setting $xmlapi->debug to 1 so you can see what the raw API output is? (this is an option provided with the public API you appear to be using)
     
  3. baldarab

    baldarab Member

    Joined:
    Mar 17, 2014
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Thanks Vanessa. I don't see anything in the ePanel error logs. And apologies, as I'm not a developer, but how would I set the debug to 1? I tried adding
    PHP:
    $xmlapi->set_debug(1);      
    error_log.
    But that stopped the script from working at all.
    I appreciate your taking the time - thank you.
     
  4. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    With API2, you're expected to pass an associative array of arguments to the api call.

    This:

    PHP:
    array($email_domain$email_user'fwd'$dest_email)
    should be this:

    PHP:
    array('domain' => $email_domain'email' => $email_user'fwdopt' => 'fwd''fwdemail' => $dest_email)
     
  5. baldarab

    baldarab Member

    Joined:
    Mar 17, 2014
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Dave - awesome - you fixed it! Thank you also Vanessa for your time.

    Here's the final code in case another rank amateur like me is looking to use the same.
    PHP:
    include 'xmlapi.php' ;
    $ip 'nnn.nnn.nnn';   //the ip address of your email hosting
    $root_pass 'pppppppp';  //password for cPanel account
    $account 'aaaaaaaa'//account name for cPanel

    // I'm using $_POST as I'm posting the details for the new account using an http: post from Infusionsoft.
    // All 3 $_POST parameters need to be changed accordingly if you're using an HTML form.

    $email_user $_POST['Username'];   //the email account to be created, portion before @
    $email_password $_POST['EmailPassword'];  //the password to be used for the new email account
    $dest_email $_POST['ParentEmail'];   //the full email account to be forwarded to
    $email_domain 'domain.com';  //the email domain (part of email after @)
    $email_quota '10';  //the mailbox quota for the new account in MB

    // don't change anything from here forward

    $xmlapi = new xmlapi($ip);
    $xmlapi->set_port(2083);
    $xmlapi->password_auth($account,$root_pass);
    $xmlapi->set_output('xml');

    $result $xmlapi->api1_query($account'Email''addpop', array($email_user$email_password$email_quota$email_domain) );

    $result_forward $xmlapi->api2_query($account'Email''addforward', array(domain => $email_domainemail => $email_userfwdopt => 'fwd'fwdemail => $dest_email) );
    For a bit of security, my email account domain is different from my website domain, and is hosted separately. That means that if the file is compromised and someone gets password and username details for this cPanel, "only" my email accounts are compromised, not my website. I'm sure a security guy could make it infinitely more secure, but for my purposes, that's probably enough as the contents of the email accounts are not that important.
     
  6. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    675
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  7. followersexpert

    followersexpert Registered

    Joined:
    Apr 1, 2014
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    thanks it will help
     
  8. baldarab

    baldarab Member

    Joined:
    Mar 17, 2014
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Hi Michael. I just noticed that the first part of the PHP code in my reply is sitting within the body of the message rather than the PHP code itself. Any chance of getting it in there in case anyone wants to copy the code?
     
  9. cPanelJared

    cPanelJared Technical Analyst
    Staff Member

    Joined:
    Feb 25, 2010
    Messages:
    1,842
    Likes Received:
    18
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    I fixed it for you by moving the code tag up one line. :)
     
  10. baldarab

    baldarab Member

    Joined:
    Mar 17, 2014
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    thank you!
     
  11. cPanelJared

    cPanelJared Technical Analyst
    Staff Member

    Joined:
    Feb 25, 2010
    Messages:
    1,842
    Likes Received:
    18
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    You're most welcome. :)
     
  12. Thembinkosi

    Thembinkosi Member

    Joined:
    May 9, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    South Africa
    cPanel Access Level:
    Root Administrator
    in which directory should i put the script and what should i call it... please help
     
  13. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    675
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello,

    Could you provide some more details about your purpose for using the script, and how you plan to implement it? Generally there are no restrictions on the name and location.

    Thanks!
     
  14. Thembinkosi

    Thembinkosi Member

    Joined:
    May 9, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    South Africa
    cPanel Access Level:
    Root Administrator
    i have found a solution thanks... now i am looking for a script that can allow my clients to make domain redirects in whmcs client area without having to login to cpanel
     
  15. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    675
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    I'm happy to see you were able to find a solution. Feel free to open a new thread on the WHMCS support forums for additional feedback on the WHMCS question:

    WHMCS Forums

    Thank you.
     
Loading...

Share This Page