Question

First Let me start by saying im a total noob at email and email servers and how they work. what i would like to do is retrieve my emails from my hosting server (hostgator) and display inside my sites admin panel via php. I'm building a site for a couple partners of mines whom want the ui as simple as possible and for that reason i don't want them to have to log into the cpanel to check there emails i would like there emails to be displayed inside the sites administrative panel i have been working on any suggestions ?

Was it helpful?

Solution

You already have a complete email viewer built into cpanel, so why not take advantage of it? Use the following code to determine how many unread emails you have in your inbox as well as generate a link that will open your webmail without having to type your username/password:

    $emailAddress = 'email@domain.com'; // Full email address
    $emailPassword = 'yourpassword';        // Email password
    $domainURL = 'domain.com';              // Your websites domain
    $useHTTPS = true;                       // Depending on how your cpanel is set up, you may be using a secure connection and you may not be. Change this from true to false as needed for your situation

    /* BEGIN MESSAGE COUNT CODE */

    $inbox = imap_open('{'.$domainURL.':143/notls}INBOX',$emailAddress,$emailPassword) or die('Cannot connect to domain:' . imap_last_error());
    $oResult = imap_search($inbox, 'UNSEEN');

    if(empty($oResult))
        $nMsgCount = 0;
    else
        $nMsgCount = count($oResult);

    imap_close($inbox);

    echo('<p>You have '.$nMsgCount.' unread messages.</p>');

    /* END MESSAGE COUNT CODE */

    echo('<a href="http'.($useHTTPS ? 's' : '').'://'.$domainURL.':'.($useHTTPS ? '2096' : '2095').'/login/?user='.$emailAddress.'&pass='.$emailPassword.'&failurl=http://'.$domainURL.'" target="_blank">Click here to open your inbox.</a>');

Of course, it is not the best practice to store your passwords in plaintext so you may want to come up with a solution for encrypting these, but you can use this example to get started.

Also, you need to have php_imap.dll enabled in php for the count of messages code to work. If you do not want to or cannot enable this, then you can remove the code between the MESSAGE COUNT CODE. The direct link to your webmail should work for any cpanel instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top