Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6832793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:54:06+00:00 2026-05-26T22:54:06+00:00

( First time programming in PHP. Had some help. Need a bit more. )

  • 0

(First time programming in PHP. Had some help. Need a bit more.)

Goal:

Pull the lastContactDate from a given email address from my gmail account. Hoping to answer the question, “When was the last time I contacted [Person]”

What I’ve done so far:

  • Connected to gmail using imap (inbox only)
  • Grabbed the date and time
  • Printed the person’s name and timestamp.

What I can’t do:

  • Scour emails for lastContactDate that have been archived (I’m an inbox=0 guy)

Notes:

  • The code is rough, but functional. The php should really be separated onto different pages, but this is first attempt. Thanks in advance for any help!
  • Loving programming, btw. I did a little @edw519 dance more than once the last two days.

Research:

  • I think that messing with the params for imap_open and imap_search is probably my best bet, but not sure.
  • Been using these two pages heavily:
  • http://php.net/manual/en/function.imap-open.php
  • http://php.net/manual/en/function.imap-search.php

Code used thus far:

    /* connect to gmail */
$gmailhostname = '{imap.gmail.com:993/imap/ssl}';
$gmailusername = "___@gmail.com";
$gmailpassword = "___";

    /* try to connect */
$conn = imap_open($gmailhostname,$gmailusername,$gmailpassword) or die('Cannot connect to Gmail: ' . imap_last_error());

$query = mysql_query("SELECT * FROM users");    
    while($row = mysql_fetch_array($query))
    {
        $findemail = $row["email"];

        /* grab emails */
        $emails = imap_search($conn,'FROM "'.$findemail.'"');

        /* if emails are returned, cycle through each... */
        if ($emails) { 
            /* begin output var */
            $output = '';             
            /* put the newest emails on top */
            rsort($emails);

            /* for 5 emails... */
            $emails = array_slice($emails,0,1);

            foreach ($emails as $email_number) {    
                /* get information specific to this email */
                $overview = imap_fetch_overview($conn,$email_number,0);
                $message = imap_fetchbody($conn,$email_number,2);

                /* output the email header information */
                /*
            $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
                $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
                $output.= '<span class="from">'.$overview[0]->from.'</span>';
            */
                $output.= '<span class="from">'.$overview[0]->from.'</span> ';
                $output.= '<span class="date">on '.$overview[0]->date.'</span> <br /><br />';
                mysql_query("UPDATE users SET lastContactDate = '".$overview[0]->date."' WHERE email = '".$findemail."'") or die(mysql_error());

                /* output the email body */
                /* $output.= '<div class="body">'.$message.'</div>'; */
            }
            echo $output;
        }
    } 
/* close the connection */
imap_close($conn);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:54:07+00:00Added an answer on May 26, 2026 at 10:54 pm

    Problem solved!

    Here’s the solution. Using the original code above, we only modified the location in which our program searches. Instead of INBOX, it’s:

        /* connect to gmail */
    $gmailhostname = '{imap.gmail.com:993/imap/ssl}[Gmail]/All Mail';
    

    Specifically

    [Gmail]/All Mail
    

    Found the syntax here: http://php.net/manual/en/function.imap-delete.php

    But would not have been possible without Ben’s epic solution below.. In large part for this bit:

        //You can find out what folders are available with this command:
    print_r(imap_list($conn, $gmailhostname, '*'));
    

    print_r listed all the folders in my account by name. We spotted “All Mail”, in my case – 22,000+, found a sample piece of code on php.net with the syntax, plugged it in and viola!

    Thanks to mmmshuddup for cleaning my code and especially Ben for the enormous researching effort and leading solutions.

    This is fun as hell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am taking my first foray into PHP programming and need to configure the
While using Aptana and Eclipse for the first time in my programming life for
a colleague and I are trying pair programming for the first time. We both
first time poster and TDD adopter. :-) I'll be a bit verbose so please
first time posting in StackOverflow. :D I need my software to add a couple
first time use JTree. Just wondering is it possible to have more than one
First time trying to deal with users logging in with ASP.net and haven't had
I am programming my first real PHP website and am wondering how to make
PROBLEM I have a nested PHP array that I need to populate from flat
I've just started programming a online highscore for the first time, and it's for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.