A script (of sorts) to extract messages.

Help
2006-11-05
2013-04-03
  • Nobody/Anonymous

    Here's a script.  It's a terrible hack at the moment, but seems to work.  It extracts all the 'conversations' from the first page (50), and creates a rough html list page linked to one html page for each conversation, containing each message.  These html files are created in the current dir.

    I didn't need the proxy stuff and removed it, you may need it.

    There are many more attributes of the snapshot objects, uncomment the print_r lines to dump them all out. It's quite tricky to work out the nesting of attributes, many are inside numerical or associative arrays one or two levels down.

    <?php
        require_once("libgmailer.php");

        $gmail_acc = "gmail_account";
        $gmail_pwd = "the_password";

        $flist = fopen("convlist.html", "w");
        $ltext = "<html><head><title>List</title></head><body>";
        fwrite($flist, $ltext);

        $cnum = 0;
       
        $gmailer = new GMailer();
        if ($gmailer->created)
        {
           $gmailer->setLoginInfo($gmail_acc, $gmail_pwd, $my_timezone);
           // $gmailer->setProxy("proxy.company.com");
           if ($gmailer->connect())
           {
              // GMailer connected to Gmail successfully.
              // Do something with it.
              $gmailer->fetchBox(GM_STANDARD, "inbox", 0); // 0 is first page
              $snapshot = $gmailer->getSnapshot(GM_STANDARD);
              $box_total = $snapshot->box_total;
              $box_display = $snapshot->box_display;
              for ($i = 0; $i < $box_display; $i++)
              {
                  $conv_id = $snapshot->box[$i]["id"];
                  $gmailer->fetchBox(GM_CONVERSATION, $conv_id, 51); // 0 is default (inbox)
                  $snapshot2 = $gmailer->getSnapshot(GM_CONVERSATION);
                  // echo "dumping snapshot: ".print_r($snapshot2,true);
                  $conv_title = $snapshot2->conv_title;
                  $conv_total = $snapshot2->conv_total;
                  $dt = $snapshot2->conv[0]["dt"];

                  $cnum++;
                  $fconv = fopen("conv" . $cnum . ".html", "w");
                  $ctext = "<html><head><title>List</title></head><body>";
                  fwrite($fconv, $ctext);

                    $ltext = "<a href=\&quot;" . "conv" . $cnum . ".html" . "\&quot;>" . $conv_title . "</a> " . $dt . " (" . $conv_total . " messages)<br />";
                  fwrite($flist, $ltext);
                   
                  for ($j = 0; $j < $conv_total; $j++)
                  {
                      $msg_id = $snapshot2->conv[$j][id];
                 
                      $gmailer->fetchBox(GM_CONVERSATION, array($conv_id,$msg_id), 0);
                          $conv = $gmailer->getSnapshot(GM_CONVERSATION);
                          fwrite($fconv, $conv->conv[0]["sender"] . "<br />");
                          fwrite($fconv, $conv->conv[0]["body"] . "<br />");
                          // echo print_r($conv, true);
                          fwrite($fconv, "<br /><br />");
                  }
                  $ctext = "</body></html>";
                  fwrite($fconv, $ctext);
                  fclose($fconv);
              }
           }
           else
           {
              die("Fail to connect because: ".$gmailer->lastActionStatus());
           }
        }
        else
        {
           die("Failed to create GMailer because: ".$gmailer->lastActionStatus());
        }

        // echo $snapshot->gmail_data;
        if ($snapshot->created == 0)
            // error occurred
            echo "no snapshot\n";
    ?>
    <html><head></head>
    <body>that should have done something
    </body>
    </html>

     
    • JiveMasterT

      JiveMasterT - 2007-03-16

      This is a nice little script for learning how to use things.  Could you post your update!?

       

Log in to post a comment.