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 Forum
Share with your friends:
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
Post View Counts WordPress plugin (Documentation)
The Post View Counts plugin is a simple tool for tracking and displaying the number of views for each post […]
December 30, 2022
How to create Laravel Flash Messages
Laravel flash messages are a convenient way to display one-time notifications to the user after a form submission or other […]
December 22, 2022
How to make Custom Artisan Command in Laravel
Custom Artisan commands are a useful feature of Laravel that allow you to define your own command-line commands for tasks […]
December 22, 2022
Digital Marketing Toolkit
Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits
Get Free Access Now