Reading Gmail With PHP IMAP

Posted by Unknown On Tuesday 8 January 2013 13 comments


Hello dear friends .Our new php snippet is simple code for loading gmail messages using IMAP. Here using php imap extension to use inbox. Before you proceding this project make sure about your imap setting in Gmail. 






To start with we should have the following minimum requirements
  • PHP5
  • IMAP enabled in your Gmail settings.
  • PHP IMAP Extension is enabled
 
Configuration with mail.


               /* connect to gmail with your credentials */
                $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
                $username = 'YOUR_GMAIL_USERNAME'; # e.g somebody@gmail.com
                $password = 'YOUR_GMAIL_PASSWORD';
 


Connection using Gmail’s IMAP.


           /* try to connect */
               $inbox = imap_open($hostname,$username,$password) or 
               die('Cannot connect to Gmail: ' . imap_last_error());
 
 
 

Source code.


    
<?php
/**
 *	Uses PHP IMAP extension, so make sure it is enabled in your php.ini,
 *	extension=php_imap.dll
  */
 set_time_limit(3000); 
 /* connect to gmail with your credentials */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'YOUR_GMAIL_USERNAME'; # e.g somebody@gmail.com
$password = 'YOUR_GMAIL_PASSWORD';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
/* useful only if the above search is set to 'ALL' */
$max_emails = 16;
/* if any emails found, iterate through each email */
if($emails) {
    $count = 1;
        /* put the newest emails on top */
    rsort($emails);
        /* for every email... */
    foreach($emails as $email_number) 
    {
        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        /* get mail message */
        $message = imap_fetchbody($inbox,$email_number,2);
       echo $message; 
                
        if($count++ >= $max_emails) break;
    }
  } 
/* close the connection */
imap_close($inbox);
echo "Done";

?>
 
 

Download Source file

 
 
 
 
 


13 comments:

Unknown said...

how to get mail id by this right now its show message

Unknown said...

The script keeps asking for certificates

Passing strange said...

This is a nice section covering how to read emails from Gmail account with various filtering. Somehow most of the website uses this.

Here is anothe article about Read Gmail Emails using PHP & IMAP:
http://www.namasteui.com/read-gmail-emails-using-php-imap/

Hope this helps them too.

--
Regards,
Sourav Basak [Blogger, Entrepreneur, Thinker]
http://www.namasteui.com

nancy said...

Wonderful article, very useful and well explanation. Your post is extremely incredible.

nancy said...

Your blog is very useful for me, Thanks for your sharing.

nancy said...

very useful and well explained. Your post is extremely incredible.


RPA Training in Hyderabad

Sanvi said...

Nice post. It is very useful and informative post.

CEH Training In Hyderbad

Amrita Bansal said...

I am really happy to say it’s an interesting post to read. I learn new information from your article; you are doing a great job. Keep it up…
Data Analytics Training in Gurgaon
DATA ANALYTICS WITH R CERTIFICATION in Gurgaon
Power Bi Training in GUrgaon
SQL Training in Gurgaon
Advanced Excel /VBA training in Gurgaon
Tableau Training in Gurgaon

TRONIX said...

Nice information, thanks for sharing it with us.

Java training institution in Hyderabad

Anonymous said...

Your passion for learning is truly inspiring. Your thirst for knowledge is contagious. Buy google cloud free tier from google cloud free tier

Hyderabad Laser Dentistry said...

Nice Article

Thanks for sharing with us 🙂

Biological Dentist in Hyderabad

vv software said...

Nice blog...We value the devotion you have shown to this site. Anticipate further excellent material.
keep posting more

prudvi said...
This comment has been removed by the author.

Post a Comment

Fashion