How will you read your Inbox messages using PHP script ?


Introduction

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 = "{imap.gmail.com:993/imap/ssl/novalidate-cert}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.

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 appsntech.com. All rights reserved. Powered By- Sanjoy Dey Productions