How to fetch emails from Gmail using IMAP and PHP

Do you know How to fetch emails from Gmail using IMAP and PHP?  I will tell you how.

I had worked with so many CRM projects so one of the features all the CRM needs is fetching the emails from their mailbox. so today I will show you how to get emails using IMAP in PHP.

Steps to do

  1. Find IMAP details of Gmail
  2. Enable less secure apps access in Gmail
  3. Creating a PHP script for fetching the emails
  4. Listing out email contents

So moving to the first step

1. Find IMAP details of Gmail

You can find the Gmail IMAP information in the google documentations. but don’t worry I collected that for you.

Host : imap.gmail.com
Requires SSL: Yes
Port  : 993

2. Enable less secure apps access in Gmail

In order to collect emails from the Gmail server, we have to enable the less secure app access from the Gmail control panel. otherwise, Gmail only allows its official plugins or applications to access the data.

So go to this Link : https://www.google.com/settings/security/lesssecureapps  to enable less secure app access.

 

3. Creating a PHP script for fetching the emails

Now we have to create a PHP script to fetch the emails from Gmail. if you are testing from your Localhost you have to enable imap in your PHP.ini file



Remove the “;” from extension=imap  to enable IMAP Connection in your localhost.

Then we have to create our PHP code for testing the IMAP Connection

<?php
 //Establishing connection
 $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
 $id = "[email protected]";
 $pwd = "Yourpassword";
 $imap = imap_open($url, $id, $pwd);
 print("Connection established...."."<br>");

 //Closing the connection
 imap_close($imap);   
?>

Run this code and it will test Your IMAP connection.

4. Listing out email contents

Finally, if you succeed while testing the connection now is the time to display all the emails we collected from Gmail.

<?php
 //Establishing connection
 $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
 $id = "[email protected]";
 $pwd = "Yourpassword";
 $imap = imap_open($url, $id, $pwd);
 print("Connection established...."."<br>");
 //Searching emails
 $emailData = imap_search($imap, '');

 if (! empty($emailData)) {  
    foreach ($emailData as $msg) {
       $msg = imap_fetchbody($imap, $msg, "1");
       print(quoted_printable_decode($msg)."<br>");                
    }    
 } 
 //Closing the connection
 imap_close($imap);   
?>

Run this code to List out all the emails from Gmail server.

Hope this helps you.

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to get data from a model on all pages in Laravel – Simple Way

Do you know How to get data from a model on all pages in Laravel ? In this blog post, […]

August 5, 2022

Export html table to excel, pdf, csv format using Datatable

Do you know how to export html table to excel, pdf, CSV, or excel format using Datatable? If you don’t […]

August 4, 2022

How to create a Laravel Collection From JSON

Today i have to create a laravel collection from a JSON object. I search on the internet and I cant […]

August 3, 2022

Instagram Hashtag API PHP and JavaScript Integration with RapidAPI

Do you know how to integrate Instagram Hashtag API with your website? In this tutorial, I will show you how […]

August 2, 2022

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now