Random Sequence

This PHP script will duplicate folders and messages from one IMAP server to another.

Sponsored Links:

Copying Between IMAP Servers With PHP

It doesn’t check for messages or mailboxes which already exist on the target server

The script requires PHP compiled with IMAP support. On Mac OS X I use the PHP module installed using Marc Liyanage’s page. On other systems you may need to change the path to PHP on the first line.

Usage

./copy.php username password

Script

!/usr/local/php/bin/php -q

<?php

$sourceHost = "10.0.1.1:143";    // Change these as appropriate
$destHost = "10.0.1.2:143";

// $mailbox = "INBOX";

$user = $argv[1];
$pass = $argv[2];

$sourcestream = imap_open("{".$sourceHost."}", $user, $pass);
$deststream = imap_open("{".$destHost."}", $user, $pass);
    if ($sourcestream && $deststream) {

    $list = imap_list($sourcestream, "{".$sourceHost."}", "*");
    $destlist = imap_list($deststream, "{".$destHost."}", "*");
    print_r($destlist);
     //   if (0) {
        if (is_array($list)) {
            foreach($list as $mailbox) {
            $pos = strpos($mailbox,"}");
            $mailbox = substr($mailbox,$pos+1);
                if (! stristr($mailbox,"Shared")) {
                // if ($mailbox == "INBOX") {
                echo $mailbox."\n";
                $sourcembox = imap_open("{".$sourceHost."}".$mailbox, $user, $pass);
                // $sourcembox = false; // debug
                    if ($sourcembox) {

                        if (! array_search("{".$destHost."}".$mailbox,$destlist)) {
                        echo "Creating mailbox $mailbox on $destHost ";
                            if (imap_createmailbox($deststream, imap_utf7_encode("{".$destHost."}".$mailbox))) {
                            echo "done\n";
                            } else {
                            echo "NOT done\n";
                            }
                        }

                    $destmbox = imap_open("{".$destHost."}".$mailbox, $user, $pass);

                        if ($destmbox)  {                    
                        $headers = imap_headers($sourcembox);
                        $total = count($headers);
                        $n = 1;
                        echo "$total items in $mailbox\n";
                            if ($headers) {
                                foreach ($headers as $key => $thisHeader) {
                                echo "copying $n of $total... ";
                                $header = imap_headerinfo($sourcembox, $key+1);
                                $is_unseen = $header->Unseen;
                                echo "is_unseen = $is_unseen";
                                // $is_recent = $header->Recent;
                                // echo "is_recent = $is_recent";
                                $messageHeader = imap_fetchheader($sourcembox, $key+1);
                                $body = imap_body($sourcembox, $key+1);
                                    if (imap_append($destmbox,"{".$destHost."}".$mailbox,$messageHeader."\r\n".$body)) {
                                        if ($is_unseen != "U") {
                                            if (! imap_setflag_full($destmbox,$key+1,'\\SEEN')) {
                                            echo "couldn't set \\SEEN flag";
                                            }
                                        }
                                    echo "done\n";
                                    } else {
                                    echo "NOT done\n";
                                    }
                                $n++;
                                }
                            }
                        imap_close($destmbox);
                        }
                    imap_close($sourcembox);
                    }
                }
            }
        }
    imap_close($sourcestream);
    imap_close($deststream);
    }

?>

References:

http://www.entropy.ch/software/macosx/welcome.html
http://uk.php.net/manual/en/ref.imap.php

Sponsored Links: