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.
Post Your Questions on our forum
Post a question on ForumShare with your friends:
How to integrate Paypal API in Laravel
Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]
April 3, 2024
How to integrate Razorpay API in Laravel
Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]
April 3, 2024
Laravel 11 Ajax CRUD Operation Tutorial Example
**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]
April 3, 2024
Login as Client in Laravel – Login with user id
**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]
April 3, 2024
Digital Marketing Toolkit
Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits
Get Free Access Now