This page allows you to backup your gmail account (IMAP) to a MBox mail format.

By Guilhem Martin, May 2011. To write the code, I used following sources: From sebsauvage (http://sebsauvage.net/wiki/doku.php?id=gmail_archiver) [mbox format + methods] and from David Walsh (http://davidwalsh.name/gmail-php-imap) [php code to deal with IMAP]

Please note:

Source code to backup your Gmail account (highlighted thanks to http://tohtml.com/php/)
Preview:
<?php
set_time_limit (0);  // Backup can last a long time => avoid PHP time out


    $sEmail    = "Email";
    $sPassword = "Password";

    $sFolder   = "Folder";

    $rMailbox  = imap_open ("{imap.gmail.com:993/imap/ssl}$sFolder", $sEmail, $sPassword);

    if($rMailbox === false) die('Cannot connect to the gmail account');

    $sFilePath = __DIR__. "/$sEmail";


    /* grab emails */
    $aEmails = imap_search($rMailbox,'ALL');

    /* if emails are returned, cycle through each... */

    if($aEmails) {
      
      $iTotalNumberOfEmails = count($aEmails);

      echo "Found " . count($aEmails) . "<BR>\n";

      
      /* begin output var */
      $output = '';
      
      /* put the newest emails on top */
      rsort($aEmails);

      
      $iCount = 1;
      
      file_put_contents($sFilePath,"");

      /* for every email... */
      foreach($aEmails as $email_number) {
      
        echo "Downloading email $iCount on $iTotalNumberOfEmails <br>\n";

        
        /* get information specific to this email */
        $overview   = imap_fetch_overview($rMailbox,$email_number,0);

        $sEmailBody = imap_fetchbody($rMailbox,$email_number,1);
        
        $sFromForMboxFormat = "From \"{$overview[0]->from}\"\n";    

        $sFromLine          = "From: \"{$overview[0]->from}\"\n";

        $sToLine            = "To: \"{$overview[0]->to}\"\n";    

        $sSubject           = "Subject: {$overview[0]->subject}\n";

        $sDate              = "Date: {$overview[0]->date}\n";

        $sRead              = "Seen: {$overview[0]->seen}\n";    

        if(isset($overview[0]->references)) {
          $sReferences        = "References: {$overview[0]->references}\n";

        }
        $sMessageId         = "Message-ID: {$overview[0]->message_id}\n";

        if(isset($overview[0]->in_reply_to)) {
          $sInReplyTo         = "In-Reply-To: {$overview[0]->in_reply_to}\n";

        }
        $sSize              = "Size: {$overview[0]->size}\n";

        $sUid               = "UID: {$overview[0]->uid}\n";    

        $sRecent            = "Recent: {$overview[0]->recent}\n";

        $sMsgNo             = "Msg-No: {$overview[0]->msgno}\n";    

        $sFlagged           = "Flagged: {$overview[0]->flagged}\n";        

        $sAnswered          = "Answered: {$overview[0]->answered}\n";            

        $sDeleted           = "Deleted: {$overview[0]->deleted}\n";                

        $sDraft             = "Draft: {$overview[0]->draft}\n";                    


        /* By Guilhem Martin, May 2011 */
        /* Based on: sebsauvage (http://sebsauvage.net/wiki/doku.php?id=gmail_archiver) [mbox format + methods] and on David Walsh (http://davidwalsh.name/gmail-php-imap) [php code to deal with IMAP] */
        $sOutput = $sFromForMboxFormat . $sFromLine . $sToLine . $sSubject . $sDate . $sRead . $sMessageId . $sReferences . $sInReplyTo . $sSize . $sUid . $sRecent . $sMsgNo . $sFlagged . $sAnswered . $sDeleted . $sDraft . $sEmailBody . "\n";

              
        $sOutput .= file_get_contents($sFilePath);
        file_put_contents($sFilePath,$sOutput);

        $iCount++;
      }
    } 
     
    imap_close($rMailbox);

?>