Question

I'm trying to create a PHP script to look through my Gmail account.

I have successfully logged in, but found a few queries which i am unsure about

login script: (i am connected)

     $hostname = '{imap.gmail.com:993/imap/ssl}';
     $username = "USERNAME@gmail.com";
     $password = "PASSWORD";
     /* try to connect */
     $hostname = '{imap.gmail.com:993/ssl}[Gmail]/All Mail';

     /* try to connect */
     $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

code to loop through my emails :-

     $emails = imap_search($inbox,'ALL');
     /* if emails are returned, cycle through each... */
     if($emails) {
     echo "FOUND EMAIlS<br />\n";
      $x=0;
      foreach ($emails as $ab => $email_number)
      {
      echo "'$ab '$email_number'<br />\n";
      $overview = imap_fetch_overview($inbox,$email_number,0);
      echo " &nbsp; Ov = '$overview'<br />\n";

(I have a $x counter to stop the script after 20 emails)

Issue 1:-

If i use print_r($overview); it works perfectly fine - but if i use a foreach loop on the $overview, i get an error :-

Catchable fatal error: Object of class stdClass could not be converted to string in (PATHNAME) line 37

Just curious - why can i use print_r on an array, but not a foreach ? any reason ?

issue 2 :-

the "from" result displays the NAME of the person - "john smith". is there a way to get the email address ?

(i do see some email addresses in the TO field, if i didn't have them in my contacts. but not in the FROM field.)

Issue 3:

i did find a google search a few hours ago (but forgot to bookmark it), where you can use regular MYSQLI commands to search emails, but I couldn't get the PHP script to connect. So i started searching for a workig connect script.

Is there a different way to connect, so we can use regular MYSQL commands ?

Ultimate aim: to retrieve a list of all the FROM "email addresses" (not the name of the sender), possibly a count of the emails too ?

I do have 30,000+ emails, & can do this process via a cron-job, over a period of time - building up a list.

EDIT: When I inserted the print_r command, the email address of the FROM name was not one of the resuts retrieved

EDIT: added imap_open line

EDIT: OUTPUT OF PRINT_R($output);

     Array
     (
         [0] => stdClass Object
             (
                 [subject] => subject line of email
                 [from] => From name
                 [to] => emailaddress@toperson.com
                 [date] => Sat, 10 Jul 2004 03:13:12 +1200
                 [message_id] => <9a3273a0040709081340933b3a@mail.gmail.com>
                 [size] => 1132
                 [uid] => 27
                 [msgno] => 21
                 [recent] => 0
                 [flagged] => 0
                 [answered] => 0
                 [deleted] => 0
                 [seen] => 1
                 [draft] => 0
             )
     )

This is my oldest message. it does include the email address i sent the message TO, but not the email address of the FROM (some eamils are To me - but still do not show an email address of the sender)

Was it helpful?

Solution

You should read it in this way:

foreach($overview as $entry) {
  $subject = $entry->subject;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top