Posted on Updated on - Arjun

Reading emails from gmail using PHP & IMAP

Recently i have worked on language translation project for that i need to read emails using PHP From the gamil inbox based on the email content i had created projects in the database…fortunately reading email from the gmail was easier than i think thanks to IMAP extension.

Here is the simple example for retrieving emails from the gmail:

NOTE: This will work only if you meet the fallowing requirements

  • PHP version should be PHP5 or latest
  • PHP IMAP Extension should be enabled in your PHP installation
  • IMAP should be enabled in your Gmail settings.

How to enable IMAP in PHP

How to Enable IMAP in XAMPP

IMAP is not enabled by default in Xampp distribution, so to enable it go to the file "\xampp\php\php.ini" and search for ";extension=php_imap.dll" and by removing the beginning semicolon at the line ,it’s get enabled ,it should be: extension=php_imap.dll.

How to enable IMAP in Linux

You can install the PHP5 IMAP module with this command :

apt-get install php5-imap

However it’s not enabled by default so enable it with:

php5enmod imap

To see the changes ,restart Apache

service apache2 restart

Here is the PHP script for retrieve Your Gmail Emails Using IMAP.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
  • Parthan Hugelview

    thanks its work for me.. 🙂

    • MD Wasim

      I need help pls help me

      On server showing

      Internal Server Error

      The server encountered an internal error or
      misconfiguration and was unable to complete
      your request.

      Please contact the server administrator at
      [email protected] to inform them of the time this error occurred,
      and the actions you performed just before this error.

      More information about this error may be available
      in the server error log.

      Additionally, a 500 Internal Server Error
      error was encountered while trying to use an ErrorDocument to handle the request.

  • Kannu Priya

    get an error :
    Warning: imap_open(): Couldn’t open stream {}INBOX in C:xampphtdocskanuemails.php on line 10

    Cannot connect to Gmail: Too many login failures

    Notice: Unknown: Invalid credentials 36mb47183581iot (errflg=1) in Unknown on line 0

    Notice: Unknown: Invalid credentials 36mb47183581iot (errflg=1) in Unknown on line 0

    Notice: Unknown: Invalid credentials 36mb47183581iot (errflg=1) in Unknown on line 0

    Notice: Unknown: Too many login failures (errflg=2) in Unknown on line 0

    not working , could you please help me

    • Check your user name and password once . If possible share your email.php file

  • Ankit Shah

    Hi arjun, your code works nice. However I want to retrieve entire body text. Currently it just retrieves a small part of the message text. What should I do to get the entire message body of the email?

    • can you share you code ? because imap_body() function will return entire message body.

  • MD Wasim

    Hey Friends
    I need help pls anybody help me….
    i am suffering last 3 days but not found any solution
    I am using Codeigniter i need to store data from email reply.
    Patient can question us and it send to doctor email and when doctor answer this question thn its also show our dashboard and store mysql database.

    Please help me any body..

    • what is the issue here.

  • Sujan Malla

    It says:

    A PHP Error was encountered

    Severity: Warning

    Message: imap_open(): Couldn’t open stream {}INBOX

    Filename: controllers/Mail.php

    Line Number: 64


    File: /home/rupinalanepal/email/application/controllers/Mail.php

    Line: 64

    Function: imap_open

    File: /home/rupinalanepal/email/index.php

    Line: 292

    Function: require_once

    Array ( [0] => Can not authenticate to IMAP server: [ALERT] Please log in via your web browser: ) Cannot connect to Gmail: 1
    what might be the solution?

  • Thank you!!!

  • Manish rana

    Hi Arjun
    I Enabled IMAP in my gmail account setting and tried your above code but not getting any output please help me out. I am new in PHP

  • Patrick BOHN

    Hello Arjun,
    Thank you for the script.

    I can’t sort the mails by order of date desc

    Can you me help?

  • Ida Bagus Mahendra Permana

    always getting maximum execution time of 30

  • Ida Bagus Mahendra Permana

    please help me to fix this , iam using code igniter , and i want to store all emails in gmail to the view that i created ,

  • Ida Bagus Mahendra Permana

    but thanks for your kindness to share it all

  • Ajay

    Hello guys,

    I have implemented Gmail Imap thing and working fine but I have 1 question, is there any option to use datetime in imap_search function. I mean currently I am using this like

    imap_search($kImap->mailbox,’SINCE 03 August 2017′);

    but i want to use this like

    imap_search($kImap->mailbox,’SINCE 03 August 2017 10:00′);

    So basically i want to import all the email that was there in Gmail since 03 August 2017 10:00


    • I guess you can only pass date, not date time

      • Ajay

        Hi Arjun.

        Thanks for your reply, well is there any other way by which we can fetch email received as per datetime? Actually we are receiving too many emails in our Gmail inbox and i learn that if we fetch email for entire day then sometimes scripts breaks in between. So to fix this we are planing to schedule a cron script which will run in every 15 min and will fetch all the emails received in last 15 Min. Please suggest

        Ajay Protection Status