Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Using cPanel API for creating email account and forwarders in your own PHP scripts

  1. #11
    jcrimmel is offline Newbie
    Join Date
    Sep 2013
    Posts
    2

    Default

    Quote Originally Posted by Alexander View Post
    Well, maybe I missed something, but they can change passwords for their email accounts in Webmail. There is an option for it on the very first page when accessing Webmail. Thus, API is not needed.

    Wow... I've been using the webmail auto load for so long that I completely forgot about that link. Now, please excuse me while I shamefully make my exit....

  2. #12
    coderjack9 is offline Newbie
    Join Date
    Mar 2016
    Posts
    1

    Default

    hello
    When I set all the necessary details i an getting this error "Access denied".
    please tell me what is this error

  3. #13
    David I is offline Newbie
    Join Date
    Jun 2010
    Posts
    1,245

    Default

    Hello,

    We just provided example of code and probably you will need to update XML API client class, you can find the latest one here:
    https://github.com/CpanelInc/xmlapi-php

    If you need our assistance, you may open a ticket with additional information of the problem, so we can test it out.

  4. #14
    daarkm is offline Newbie
    Join Date
    Oct 2016
    Posts
    1

    Default

    thank you very much it's work very good but i want to Chang password email script can you help me plz?

  5. #15
    GlowHost-Daniel's Avatar
    GlowHost-Daniel is offline Customer Care
    Join Date
    Feb 2014
    Posts
    1,847

    Default

    Hello daarkm,

    I'm sorry but I'm not sure I follow. You want to change an email address password for a script or by a script? You can see more about cPanel's API here.

    Please clarify your question and we'll see how we can assist further.

  6. #16
    WHK
    WHK is offline Newbie
    Join Date
    Jan 2011
    Posts
    8

    Default

    Fast forward to 2020 and the XML-API was eliminated from cPanel years ago. If you have a dedicated server or VPS server, you have access to the cPanel object which creates a CPANEL Class allowing the use of UAPI functions. Some of us are on shared servers and can't use those without the CPANEL Class.

    I played around with a command line script I found and was able to modify it so it works. The code below provides the framework to develop a custom PHP application that will add the email forwarding address to cPanel. There is also a URL to delete a forwarded email address. There isn't much error checking in this, so those who use it in their own scripts may want to add that.

    <?php

    ################################################## #############
    # cPanel Email Forwarder Creator Version 1.0
    ################################################## #############
    #
    ################################################## #############
    #
    # Can be used in 3 ways.
    #
    # 1) Sample run from browser:
    # cpanel_email_forwarder.php?euser=john&edomain=site 1.com&eforward=johny@gmail.com
    #
    # 2) When run from browser without parameters will show entry form
    #
    # 3) Sample run from your script (by including):
    # $REQUEST['euser'] = 'john';
    #
    # $REQUEST['edomain'] = 'your_domain.com';
    #
    # $REQUEST['eforward'] = 'johny@gmail.com';
    # include("cpanel_email_forwarder.php");
    #
    ################################################## #############


    ////////////////////////////////////////////////////////////////
    /////////////////////// SETTINGS START ////////////////////////
    ////////////////////////////////////////////////////////////////


    // cpanel user (the one you login to cpanel)
    define('CPANELUSER','cpanel-username-here');


    // cpanel password (the one you login to cpanel)
    define('CPANELPASS','cpanel-password-here');


    // your cpanel domain (domain name)
    define('CPANELDOMAIN','your_domain.com');


    // Hard Coded cPanel Session Token;
    define('CPANEL_SESSION','cpsessXXXXXXX');


    ////////////////////////////////////////////////////////////////
    /////////////////////// END OF SETTINGS ////////////////////////
    ////////////////////////////////////////////////////////////////


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


    $cpuser = CPANELUSER;
    $cppass = CPANELPASS;
    $cpdomain = CPANELDOMAIN;
    $cpsess = CPANEL_SESSION;


    $euser=getVar('euser');
    $edomain=getVar('edomain');
    $eforward = getVar('eforward');
    if (empty($edomain)) $edomain = $_SERVER['SERVER_NAME']; // populate curent domain


    if (empty($euser) || empty($edomain) || empty($eforward)) {
    echo "All fields are required to create email forwarding:<br>
    <form method='post'>
    Username:<input name='euser' value='$euser'> (sample: john)<br>
    Domain:<input name='edomain' value='$edomain'> (sample: mysite.com)<br>
    Redirect to:<input name='eforward' value='$eforward'> (sample: jimm@site2.com)<br>
    <input type='submit' value='Create forwarder' style='border:1px solid black'>
    </form>";
    die();
    }


    // Create email forwarder
    /*******email forward below***************/
    // This Works! forward an email address
    // url with variables


    $url_add = "https://" . $cpuser . ":" . $cppass . "@" . $cpdomain . ":2083/" . $cpsess . "/execute/Email/add_forwarder?domain=" . $edomain . "&email=" . $euser . "@" . $edomain . "&fwdopt=fwd&fwdemail=" . $eforward;
    /*******************************************/


    /*******delete a forwarded email below***************/
    // This Works! To delete a forwarded email
    // url with variables
    $url_delete = "https://" . $cpuser . ":" . $cppass . "@" . $cpdomain . ":2083/" . $cpsess . "/execute/Email/delete_forwarder?address=" . $euser . "@" . $edomain . "&forwarder=" . $eforward;
    /************/


    $url = $url_add;
    // $url = $url_delete; // debug - can be used to delete a forwarded email




    /********************/
    $contents = file_get_contents($url); // execute the command and get results
    echo "<h3>Contents: " . $contents . "</h3>";
    /***********/










    echo "<h3>URL: " . $url . "</h3>"; // this displays the URL executed




    echo "Email Forwarder created: $euser@$edomain =&gt; $eforward";
    Last edited by Matt; 08-14-2020 at 09:08 PM.

  7. #17
    Larry's Avatar
    Larry is offline Customer Care
    Join Date
    May 2018
    Posts
    3,075

    Default

    Brilliant share WHK, thank you!

    For those of us who are less PHP savvy, is there a specific PHP version you've chosen while testing your script?

  8. #18
    WHK
    WHK is offline Newbie
    Join Date
    Jan 2011
    Posts
    8

    Default

    Thanks Larry - I used PHP version 7.3

Page 2 of 2 FirstFirst 12

LinkBacks (?)

  1. 08-03-2012, 07:09 AM

Similar Threads

  1. default timezone
    By Scott in forum Programming Talk
    Replies: 14
    Last Post: 03-24-2008, 06:48 PM
  2. Single Account, Multiple Domains to WHM Account
    By rickpugh in forum WHM, Resellers, VPS and Dedicated Hosting Topics
    Replies: 3
    Last Post: 08-15-2005, 10:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14