Results 1 to 8 of 8

Thread: Webmail login through CURL

  1. #1
    Registered Member
    Join Date
    Mar 2008
    Posts
    7

    Default Webmail login through CURL

    Hello,

    I am trying to autologin to webmail through CURL. I have the following code written, but get a login failed everytime. Have I misconfigured a setting? I cannot find any documentation out there to help me out. Any help would be great.

    <?php
    // INIT CURL
    $ch = curl_init();

    // SET URL FOR THE POST FORM LOGIN
    curl_setopt($ch, CURLOPT_URL, 'http://domain.com:2095/login');

    // ENABLE HTTP POST
    curl_setopt ($ch, CURLOPT_POST, 1);

    // SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
    curl_setopt ($ch, CURLOPT_POSTFIELDS, 'user=mailuser@domain.com&pass=userpassword');

    // IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
    curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie);

    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

    // EXECUTE 1st REQUEST (FORM LOGIN)
    $store = curl_exec ($ch);

    // CLOSE CURL
    curl_close ($ch);

    print $store;

    ?>

  2. #2
    Registered Member mctDarren's Avatar
    Join Date
    Jan 2004
    Location
    New Jersey
    Posts
    667
    cPanel Access Level

    Root Administrator

    Default

    Quote Originally Posted by boy143878 View Post
    curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie);
    Your code is missing the final single quote mark at the end of the line. Typo or error?

    EDIT: Also try changing your post fields to:
    Code:
    curl_setopt ($ch, CURLOPT_POSTFIELDS, 'user=mailuser&#37;40domain.com&pass=userpassword');
    Last edited by mctDarren; 04-03-2008 at 06:52 AM.

  3. #3
    Registered Member LiNUxG0d's Avatar
    Join Date
    Jun 2003
    Location
    Gatineau, Quebec, Canada
    Posts
    206

    Default

    Hey there,

    There's a bit more to it. Let me show you an example that works for me. I just whipped it up for fun...

    Code:
    <?
            $htusername = urlencode("email@yourdomain.com");
            $htpassword = urlencode("password");
            $server = "www.yourdomain.com";
    
            $ch = null;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_TIMEOUT,3);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_URL,"http://$htusername:$htpassword@$server:2095/");
    
            if ($buffer = curl_exec($ch)) {
    
                    print $buffer;
            }
            else {
    
                    print $server.": Not responding.\n";
            }
    
            curl_close($ch);
    ?>
    The above worked for me. I checked and saw the "You are logged in as email@yourdomain.com" in my output, so it does indeed work. Now for you to tweak it.

    Instead of using the /login URI, I use the :2095 straight up, since it's an htpasswd protected area. The benefits? If Apache is down, :2095 should work as it's a socket/port and not a valias or redirect in the httpd.conf. Also, manipulating the htaccess popups are usually easier. I used urlencode() here for the username and password because there might be a few... odd characters which could pose a surprise.

    Let me know how it comes along. I really hope this helps you.

    Regards,
    http://www.okteck.com/
    The best web hosting, reseller hosting and dedicated server packages!

  4. #4
    Registered Member
    Join Date
    Mar 2008
    Posts
    7

    Default

    I've copied your code into a login.php file, uploaded to domain.com/test and tried running it. I get a redirect to this URL http://domain.com/webmail/x3/?login=1

    I also see this php error flash before redirecting the the above url
    Warning: curl_setopt() [function curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an
    open_basedir is set in /home/mhost/public_html/cplogin/login.php on line 20
    I commented out the CURLOPT_FOLLOWLOCATION TO RUN THE SCRIPT.

    Is it worth mentioning that I am on a reseller hosting plan.
    Last edited by boy143878; 04-06-2008 at 07:49 AM.

  5. #5
    Registered Member LiNUxG0d's Avatar
    Join Date
    Jun 2003
    Location
    Gatineau, Quebec, Canada
    Posts
    206

    Default

    Hi there,

    If this is what you're returned, then the login was most likely successful. Try to print the contents of the $buffer variable to see if you're logged in. Check the HTML output. The URL you provided indicates "login=1" so you are indeed logged in.

    Beyond this, however, you are a bit on your own. This isn't really a webmail client we've made here, it's a simple login using cURL. What you do with it from here on, however, depends on you. If you have any more precise questions, let me know.

    You might need to disable safe_mode in your php.ini to fix those errors. I'm not sure about open_basedir, I don't believe I use it all that much, or if so, I don't think it's gotten in my way at all.

    Warmest regards,
    http://www.okteck.com/
    The best web hosting, reseller hosting and dedicated server packages!

  6. #6
    Registered Member
    Join Date
    Mar 2008
    Posts
    7

    Default

    thanks for your help, I am indeed logged. I think the problem lies within the fact that I am unable to use followlocation function for curl. I played with the returned buffer and I get a logged in successfully screen, with some broken links, but for the most part this is what I wanted to accomplish; to get a successful login.

  7. #7
    Registered Member
    Join Date
    Jun 2008
    Posts
    28

    Default cPanel Login

    I am trying the example code and it works sometimes, but most of the time it gives me a not responding error. Is there a way to fix this? Also, when I actually do get logged in, I add "https://$htusername:$htpassword@$server:2083" to all images so it does not prompt for a password. Is this a security issue and/or is there a better way of doing this? Thanks.

    Code:
    <?
    $htusername = urlencode("USERNAME");
    $htpassword = urlencode("PASSWORD");
    $server = "WWW.MYDOMAIN.COM";
    
    $ch = null;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT,3);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_URL,"https://$htusername:$htpassword@$server:2083/");
    
    if ($buffer = curl_exec($ch)) {
    	$test = str_replace("/cPanel_magic_revision_","https://$htusername:$htpassword@$server:2083/cPanel_magic_revision_",$buffer);
    	print $test;
    } else {
    	print $server.": Not responding.\n";
    }
    
    curl_close($ch);
    ?>

  8. #8
    Registered Member
    Join Date
    Jun 2008
    Posts
    28

    Default

    Turns out it was an error with the shared certificate. Still don't know why, but at least I figured out what is happening.

Similar Threads

  1. security tokens and curl login
    By madbob in forum cPanel Developers
    Replies: 3
    Last Post: 11-21-2012, 03:54 AM
  2. [php] problems with cURL login to cPanel
    By mathiflip in forum cPanel Developers
    Replies: 3
    Last Post: 03-26-2012, 01:41 PM
  3. roundcube login using curl
    By Anupam Baruah in forum General Discussion
    Replies: 0
    Last Post: 10-19-2011, 12:07 AM
  4. Replies: 0
    Last Post: 10-10-2010, 02:55 AM
  5. webmail login using cURL
    By larwilliams in forum cPanel Developers
    Replies: 3
    Last Post: 06-16-2009, 01:09 PM
bargain