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
- Find IMAP details of Gmail
- Enable less secure apps access in Gmail
- Creating a PHP script for fetching the emails
- 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.
Share with your friends:
Drag and Drop with Swapy and PHP
Hello , You might had issues with drag and drop options in your dashboard. here i found a new javascript […]
September 17, 2024
How To Generate A PDF from HTML in Laravel 11
Hello , I was trying to generate a PDF payment receipt for my SAAS application and when i search for […]
June 22, 2024
How to get the next value of an array and loop it in the array in PHP
Do you know How to get the next value of an array and loop it in the array in PHP […]
July 28, 2023
New Open Source CRM for project Management and Invoicing
I’m excited to announce the launch of my new open source project: Gmax CRM. an invoicing and project management tool […]
December 31, 2022
Digital Marketing Toolkit
Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits
Get Free Access Now