How will you read your Inbox messages using PHP script ?


In my previous post i talked about “How to create your own helper file to generate PDF in PHP(CodeIgniter)?”. In some application may require you to code to read your email. If you are wondering “How to read your inbox messages using PHP script?”, then you are at right place. In this short article we will discuss about how to read messages from Gmail inbox using php script. We will make use of IMAP to read GMail inbox messages.

We will create a php file naming GmailReader.php and paste below code.

Below compact code to read email from your gmail account.

function readMail() {

    $dns = "{}INBOX";
    $email = "Your Mail";
    $password = "Password";

    $openmail = imap_open($dns,$email,$password ) or die("Cannot Connect ".imap_last_error());
    if ($openmail) {

        echo  "You have ".imap_num_msg($openmail). " messages in your inbox";

        for($i=1; $i <= 100; $i++) {

            $header = imap_header($openmail,$i);
            echo "
"; echo $header->Subject." (".$header->Date.")"; } $msg = imap_fetchbody($openmail,1,"","FT_PEEK"); /* $msgBody = imap_fetchbody ($openmail, $i, "2.1"); if ($msgBody == "") { $portNo = "2.1"; $msgBody = imap_fetchbody ($openmail, $i, $portNo); } $msgBody = trim(substr(quoted_printable_decode($msgBody), 0, 200)); */ echo $msg; imap_close($openmail); } else { echo "Failed reading messages!!"; } }

That's all. We are done. Now run your GmailReader.php file from the browser and can see the results.

I am able to read gmail inbox using above code. Hope this post helps. Please consider sharing with friends, like the post, that way it will be helpful for others too. Keep visiting.

1 comment:

  1. Hello Sir,

    I am developing a site and i want to show the images from my mail attachment to my site. so i tried this code. But there are some problem this code is showing the number of messages in my inbox and their regarding dates. please guide me how to show images.

    Thanks and Regards

    Manish Gupta

Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 All rights reserved. Powered By- Sanjoy Dey Productions