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.

php code to change email password

Discussion in 'cPanel Developers' started by davidvanrensbur, May 26, 2010.

  1. davidvanrensbur

    Joined:
    May 26, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    anyone manage to work this out.

    I want to be able to list email accounts via api for a particular domain as well as change email(pop3) passwords.

    Can anyone point me in the right direction?

    ive read through the api documentation and cant find anything in this regard.
     
  2. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
  3. davidvanrensbur

    Joined:
    May 26, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Hi Thanks for that, but it seems like im going to be needing more help.

    We are a hosting reselling company that host about 200 domains.
    We have a reseller account on a cpanel box in south africa.
    All the latest software on the box.

    i cant get the authentication to work properly in php.
    can i authenticate as my main reseller user/pass in cpanel in php and then access all domains.

    The functionality that i want to build needs to:
    pull all our domains out of our local mysql database (that i can do).
    be able to click on any domain (for our support staff) - and then see a list of the pop3 accounts.
    Want to be able to click on a pop3 account to change the password.

    I really need a bit more help to get this sorted out.
    would i have to authenticate as each cpanel user to do this or just as my main cpanel account?
    the php authentication on your side doesnt work proerly (well i cant get it to)

    Please let me know if you have any sample code - im pretty medium with php worked with other api's but cant get anywhere with this.

    We pretty desperate to get this working with in a day or 2.
    Im willing to pay for help if i need to.
     
  4. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    I'm sorry, but I can't write the application. However, what you desire could be created by any PHP developer.

    Barring the UI interface that you desire (which sounds like it needs multiple pages), you can obtain the information and preform the necessary procedures by making these API calls

    list all accounts:
    Code:
                   
    # my reseller is 'finalne' and has all 'Standard Privs'
    
    URL_REQUEST: http://server.com:2086/xml-api/listaccts      
    
    RESPONSE:
    <listaccts>
    	<acct>
    		<disklimit>unlimited</disklimit>
    		<diskused>0M</diskused>
    		<domain>blah.com</domain>
    		<email>mr_blah@blah.com</email>
    		<ip>10.10.10.1</ip>
    		<maxaddons>*unknown*</maxaddons>
    		<maxftp>unlimited</maxftp>
    		<maxlst>unlimited</maxlst>
    		<maxparked>*unknown*</maxparked>
    		<maxpop>unlimited</maxpop>
    		<maxsql>unlimited</maxsql>
    		<maxsub>unlimited</maxsub>
    		<owner>finalne</owner>
    		<partition>home</partition>
    		<plan>default</plan>
    		<shell>/bin/bash</shell>
    		<startdate>09 Dec 16 09:57</startdate>
    		<suspended>0</suspended>
    		<suspendreason>not suspended</suspendreason>
    		<suspendtime/>
    		<theme>x3</theme>
    		<unix_startdate>1260979066</unix_startdate>
    		<user>blahcom</user>
    	</acct>
    </listaccts> 
    
    fetch pops
    Code:
    # Foreach acct that the reseller has ownership of, an <acct/> node will be generated.
    #  you can parse these <acct/> nodes for the domain and username of the domain.
     
    #  use each username to perform a subsequent query for listing the pops for that user
    
    URL_REQUEST: http://server.com:2086/xml-api/cpanel?cpanel_xmlapi_version=2&cpanel_xmlapi_module=Email&cpanel_xmlapi_func=listpopswithdisk&user=blahcom 
    
    RESPONSE:
    <cpanelresult>
        <apiversion>2</apiversion>
        <data>
          <_diskquota>262144000</_diskquota>
          <_diskused>0</_diskused>
          <diskquota>250</diskquota>
          <diskused>0</diskused>
          <diskusedpercent>0</diskusedpercent>
          <diskusedpercent20>0</diskusedpercent20>
          <domain>blah.com</domain>
          <email>mr_blah@blah.com</email>
          <humandiskquota>250 MB</humandiskquota>
          <humandiskused>None</humandiskused>
          <login>mr_blah@blah.com</login>
          <txtdiskquota>250</txtdiskquota>
          <user>mr_blah</user>
        </data>
        <data>
          <_diskquota>262144000</_diskquota>
          <_diskused>0</_diskused>
          <diskquota>250</diskquota>
          <diskused>0</diskused>
          <diskusedpercent>0</diskusedpercent>
          <diskusedpercent20>0</diskusedpercent20>
          <domain>blah.com</domain>
          <email>mrs_blah@blah.com</email>
          <humandiskquota>250 MB</humandiskquota>
          <humandiskused>None</humandiskused>
          <login>mrs_blah@blah.com</login>
          <txtdiskquota>250</txtdiskquota>
          <user>mrs_blah</user>
        </data>
        <event>
          <result>1</result>
        </event>
        <func>listpopswithdisk</func>
        <module>Email</module>
      </cpanelresult>
    
    mod the password
    Code:
             
    # Now you know which email names need to change (the <user/> node).
    #  since the reseller has ownership of that account, you should be able to perform action against it     
    #  simply iterate through them
    
    URL_REQUEST:http://server.com:2086/xml-api/cpanel?cpanel_xmlapi_version=2&cpanel_xmlapi_module=Email&cpanel_xmlapi_func=passwdpop&user=blahcom&email=mr_blah&domain=blah.com&password=AnewS3cretpassworD
    
    RESPONSE:
    <cpanelresult>
        <apiversion>2</apiversion>
        <data>
          <reason></reason>
          <result>1</result>
        </data>
        <event>
          <result>1</result>
        </event>
        <func>passwdpop</func>
        <module>Email</module>
      </cpanelresult>
    
    
    If you use the xmlapi.php PHP client class found here:
    http://sdk.cpanel.net/lib/xmlapi/php/cp_xmlapi_php_v1.0.5.tar.gz

    you can automate the calls something similar to this
    Code:
    
    // instantiate client 
    $xmlapi = new xmlapi('10.10.10.1');
    
    // set credentials, these will be your reseller creds
    $resellername = "finalne";
    $resellerpasswd = 'cp1';
    $xmlapi->password_auth($resellername, $resellerpasswd);
            
    //get all accounts owned by reseller
    $accounts = $xmlapi->listaccts();
    foreach ($accounts->acct as $acct ) {
    	$u = (string) $acct->user;
    	if($u == $resellername){ continue; }
    	$cpuser = new stdClass();
    	$cpuser->name = $u;
    	$users[] = $cpuser;	
    }     
    	    
    //go thru each account and get the pops
    foreach( $users as $user) {
    	$pops = $xmlapi->api2_query($user->name, 'Email', 'listpopswithdisk');
    	
    	foreach( $pops->data as $pop) {
    		$popstomod[$user->name][] = array('email' => (string) $pop->user, 'domain' => (string) $pop->domain); 
    	}	
    }
            
    //for all valid accounts with pops, change password
    foreach( $popstomod as $name => $poparray) {
    	foreach($poparray as $modme){
    		$modme['password']	= 'genericpoppasswd'; //this is really, really bad, but I'm sure you can thing of something better ;)		
    		$response = $xmlapi->api2_query($name, 'Email', 'passwdpop', $modme);
    		if( (int)$response->event->result != 1 || (int)$response->data->result != 1) {
    			echo "Failed to change password for account ".$name." on pop ".$modme['email']."@".$modme['domain'];
    		}else{
    			echo "Password changed for account ".$name." on pop ".$modme['email']."@".$modme['domain'];
    		}
    	}
    }
    
    
    this is just a sample bit of code; please reference it as an example ONLY. You need to take many things into consideration with automating modification to user accounts.

    Like I said above, I'm only describing how to make the calls. A UI would be nice, especially considering your automating password changes.

    Cheers,
    -DavidN
     
  5. davidvanrensbur

    Joined:
    May 26, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    thanks

    thank you so much
     
  6. Harilgx

    Harilgx Registered

    Joined:
    May 13, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    need help to change e-mail passwords of a domain using script

    Dear David Neimeyer,

    I have 300 e-mail accounts in my domain. I would like to change the password of all these accounts to a common one ( for example "test123" ) . Is there any way to do that

    Please advice me on this.

    Awaiting your reply.

    Sincerely,
    Hari
     
  7. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi Hari,

    The easiest way to change all 300 would be a script, similar to what I've posted previously.

    I'm made some slight modifiations to that script so that only one account is affected. you'll need to set the right IP and the right loginname/loginpassword for your account. This script will change ALL email account passwords for your enitre account. If you need it to only affect one particular domain (ie, not parked domains, etc.) it will need to be modified.

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

    // instantiate client
    $xmlapi = new xmlapi('10.1.5.164');

    // set credentials, these will be your reseller creds
    $loginname "mycnf";
    $loginpasswd 'cp1';
    $xmlapi->password_auth($loginname$loginpasswd);
    $xmlapi->set_port('2083');

    // put only my account listing into the $users array
            
    $cpuser->name $loginname;
            
    $users[] = $cpuser;

    //go thru each account and get the pops
    foreach( $users as $user) {
            
    $pops $xmlapi->api2_query($user->name'Email''listpopswithdisk');

            foreach( 
    $pops->data as $pop) {
                    
    $popstomod[$user->name][] = array('email' => (string) $pop->user'domain' => (string) $pop->domain);
            }
    }

    //for all valid accounts with pops, change password
    foreach( $popstomod as $name => $poparray) {
            foreach(
    $poparray as $modme){
                    
    $modme['password']      = 'genericpoppasswd'//this is really, really bad, but I'm sure you can thing of something better ;)
                    
    $response $xmlapi->api2_query($name'Email''passwdpop'$modme);
                    if( (int)
    $response->event->result != || (int)$response->data->result != 1) {
                            echo 
    "Failed to change password for account ".$name." on pop ".$modme['email']."@".$modme['domain'] ."\n";
                    }else{
                            echo 
    "Password changed for account ".$name." on pop ".$modme['email']."@".$modme['domain'] ."\n";
                    }
            }
    }
    Regards,
    -DavidN
     
Loading...

Share This Page