EMAIL ME WHEN AVAILABLE We will inform you when the product arrives in stock. Just leave your valid email address below.
Email Quantity We won't share your address with anybody else.

Google – Php Parsing Gmail IMAP Email

Want to know something pretty slick?!?! With PHP you can parse emails in your inbox. Why would you want to do this? You can easily turn email into a means of collecting data from your users and trigger different actions. Examples: 1) Create a support email account that parses all recieved emails and creates help tickets automatically 2) Create a text message distribution list. If there is ever a problem shoot an email to the specific account, upon receipt it texts that message to your team. 3) Update a blog or webpage. Capture all emails in an inbox and create blog posts or webpages out of them. 4) Setup an email parsing script that looks for keywords and sends back auto-generated responses.

That is awesome! How do I get started?

What you need:
  1. You need at least PHP 5
  2. You will need an IMAP email account. For this example I will be using a gmail account.You will need to enable IMAP in gmail.To enable IMAP in your gmail settings:Settings->Forwarding and POP/IMAP->Enable IMAP->Save Changes
  3. Open your php.ini file and ensure the extension php_imap.dll is enabled
//Your gmail email address and password
$username = ''username@'';
$password = ''password'';

//Which folders or label do you want to access? - Example: INBOX, All Mail, Trash, labelname 
//Note: It is case sensitive
$imapmainbox = "INBOX";

//Select messagestatus as ALL or UNSEEN which is the unread email
$messagestatus = "UNSEEN";


//Gmail Connection String
$imapaddress = "{}";

//Gmail host with folder
$hostname = $imapaddress . $imapmainbox;

//Open the connection
$connection = imap_open($hostname,$username,$password) or die(''Cannot connect to Gmail: '' . imap_last_error());

//Grab all the emails inside the inbox
$emails = imap_search($connection,$messagestatus);

//number of emails in the inbox
$totalemails = imap_num_msg($connection);
echo "Total Emails: " . $totalemails . "<br>";
if($emails) {
  //sort emails by newest first
  //loop through every email int he inbox
  foreach($emails as $email_number) {
    //grab the overview and message
    $header = imap_fetch_overview($connection,$email_number,0);

    //Because attachments can be problematic this logic will default to skipping the attachments    
    $message = imap_fetchbody($connection,$email_number,1.1);
         if ($message == "") { // no attachments is the usual cause of this
          $message = imap_fetchbody($connection, $email_number, 1);
    //split the header array into variables
    $status = ($header[0]-&gt;seen ? ''read'' : ''unread'');
    $subject = $header[0]-&gt;subject;
    $from = $header[0]-&gt;from;
    $date = $header[0]-&gt;date;
    echo "status: " . $status . "<br>";
    echo "subject: " . $subject . "<br>";
    echo "from: " . $from . "<br>";
    echo "date: " . $date . "<br>";
    echo "date: " . $message . "<br><hr><br>";

//This is where you would want to start parsing your emails, send parts of the email into a database or trigger something fun to happen based on the emails.


// close the connection



Pin It

Trackback from your site.


This information box about the author only appears if the author has biographical information. Otherwise there is not author box shown. Follow YOOtheme on Twitter or read the blog.

Leave a comment