Reading Gmail With PHP IMAP

By Clain Dsilva November 21, 2011

Reading Gmail With PHP IMAPHello my dear friends, Now this is quite interesting topic for all the PHP newbies and off course OLD bees..!! You might have seen many large PHP libraries that is designed to access emails and send emails. Well this one is going to be a lot more simpler than a HUGE MAIL Library.

To start with we should have the following minimum requirements

  • PHP5
  • IMAP enabled in your Gmail settings.
  • PHP IMAP Extension is enabled [Its a default package with PHP]

Lets start with connection configuration.

 

Replace the values with the Gmail account details that you need to access. The host name need not be altered, its the standard gmail IMAP address.

Now lets connect to Gmail using IMAP.

 

Once connected you can access the inbox using imap_search() function

 

 

imap_search returns all the email ID’s which are unread and we need to iterate through the ID’s to read the emails

 

 

Download Source file

Hope you have enjoyed this Post.!! Leave your comments.

VN:F [1.9.22_1171]
Rating: 4.5/5 (4 votes cast)
Reading Gmail With PHP IMAP, 4.5 out of 5 based on 4 ratings

22 thoughts on “Reading Gmail With PHP IMAP

    • Hi Sophia. I’m glad that you found this useful.

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  • I have recently found a class on this site [link removed by moderator] , it’s working and seems promising

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  • Dear Clain, I can’t even log:
    Warning: imap_open() [function.imap-open]: Couldn’t open stream {imap.gmail.com:993/imap/ssl}INBOX on line 10
    Cannot connect to Gmail:
    Can’t connect to gmail-imap.l.google.com,993:
    Connection timed out
    Windows Vista Pro, FireFox & Chrome
    Thank you for code Max

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • Hi Max,

      A firewall is preventing the local server in your system from accessing the Gmail server. You need to give permission for for the HTTP server to access the internet. Alternatively you can add exception to the port 993. This should fix the issue. You can try this code on an external production server and this should work well. There is no relation with the browsers and the PHP code, browsers just display the final outcome of a PHP code that runs on the server. Only HTML,CSS and JavaScript issues are related to browsers.

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
    • Hi Priyanka,

      I guess your server PHP configuration do not have PHP IMAP enabled. You can easily enable this by editing the php.ini file. Also you can install PHP IMAP Extension if its not installed by default. This will solve the internal server error.

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  • I have some problems when try to receive the emails. When I run the script on server first i had receive error of type: to many login try or this one:
    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please help.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • Igor,

      Did you made any modifications on the code? I guess you have the login option included in the code. Which is why you get “too many login” error.

      Can you copy paste you code here so that I can review your code and get it fixed

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
      • Ok the problem is solved find the problem was with functio imap_search. I have one more question: If I send email let say with gmail, is possible after sending the email to append some new text inside the message which i have send?

        VA:F [1.9.22_1171]
        Rating: 0.0/5 (0 votes cast)
        • An email send is send forever, you can never append any text at a later stage. It will be a security issue if this feature ever existed.

          VN:F [1.9.22_1171]
          Rating: 5.0/5 (1 vote cast)
  • While trying this code i got the error like Couldn’t open stream {imap.gmail.com:993/imap/ssl}INBOX and Can’t connect to gmail-imap.l.google.com,993: Connection timed out. Please anybody help me to fix this.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • You are not connected to the internet, or a firewall is preventing PHP [Apache] server from connecting to gmail server. Its a connectivity problem. Please check the firewall and connection settings.

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  • I try switch to pop mode. But it is not working.
    Can you help me check ??

    subject.”\n”;
    $message = imap_fetchbody($inbox,$email_number,10);

    echo $message;
    }

    }

    /* close the connection */
    imap_close($inbox);
    ?>

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • What do you mean by pop mode?

      This library supports only IMAP. Can you give me more details of what you are trying to do? Perhaps I could help.

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  • Thanks for sharing such usefull information.I got emails but i want to make it more readable.Can you help me in that.I got email with attachment and my output like this :
    –089e01183a18bbefed04e254197b Content-Type: text/plain; charset=ISO-8859-1 Please see attachment –089e01183a18bbefed04e254197b Content-Type: text/html; charset=ISO-8859-1
    Please see attachment
    –089e01183a18bbefed04e254197b–

    ==============================
    In the above message i want to get only message not attachment, and also email address of the person who sent.Thanks in advance

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • Mahmood,
      Can you send me the full code so that I can guide you where to make changes.

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  • Thanks for sharing such usefull information.I got emails but i want to make it more readable.Can you help me in that.I got email with attachment and my output like this :
    –089e01183a18bbefed04e254197b Content-Type: text/plain; charset=ISO-8859-1 Please see attachment –089e01183a18bbefed04e254197b Content-Type: text/html; charset=ISO-8859-1
    Please see attachment
    –089e01183a18bbefed04e254197b–

    ==============================
    In the above message i want to get only message not attachment, and also email address of the person who sent.Thanks in advance

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • The text that you have mentioned is not an attachment, but the email header in text format that says you have an attachment with and Id XXXXXXXXXXXX.

      An email body in text format will have all the information like attachment info, sender information, server info, email message etc. The core imap function fetches the body as whole, now you can use your own logic to parse only the text and sender email. Use pattern matching or string functions to extract the necessary text.

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)

Leave a Reply

Your email address will not be published. Required fields are marked *