Get Emails From Gmail Using PHP
Last Updated : Jul 1, 2023
In this tutorial we will show you how to get emails from gmail using PHP, Emails are now very essential for everyone people want to check there mails regularly to keep updated from various activities now.
This process will be more convenient for people who want everything at one place if you are a webmaster you can view emails right on your website. You may also like send emails using PHP.
To Get Emails From Gmail It Takes Only Two Steps:-
- Make a HTML file and define markup
- Make a PHP file to get emails from gmail
Step 1. Make a HTML file and define markup
We make a HTML file and save it with a name emails.html
<html> <body> <div id="wrapper"> <div class="form_div"> <form method="post" action="get_emails.php"> <input type="text" name="username" placeholder="Enter Username"> <input type="password" name="password" placeholder="*********"> <input type="submit" name="get_emails" value="GET EMAILS"> </form> </div> </div> </body> </html>
In this step we create a form to enter your username and password to get your emails you can also get emails for others using this form.
Always validate any data before and after submitting the form to validate this form view our validate email and password using jQuery tutorial.
Step 2. Make a PHP file to get emails from gmail
We make a PHP file and save it with a name get_emails.php
<?php if(isset($_POST['get_emails'])) { $username=$_POST['username']; $password=$_POST['password']; //Connect Gmail feed atom $url = "https://mail.google.com/mail/feed/atom"; // Send Request to read email $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_ENCODING, ""); $curlData = curl_exec($curl); curl_close($curl); $emails = new SimpleXmlElement($curlData); echo "<ul>"; foreach($emails->entry as $entry) { echo '<li><p>'. $entry->title.'<br>'; echo $entry->summary; echo '</p></li>'; } echo "</ul>"; }
In this step we get username and password entered by user and use google mail feed url to
get emails and after that we use curl to get emails in xml format.
Now to convert mails in readable form we use SimpleXmlElement() function and display in readable format.
You may also like send attachments in email using PHP.
That's all, this is how to get emails from gmail using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
I hope this tutorial on get emails from gmail using PHP helps you and the steps and method mentioned above are easy to follow and implement.