Benutzer-Werkzeuge

Webseiten-Werkzeuge


programmieren:php:programme:imap_copy

IMAP Copy

kopiert alle Mails von einem IMAP Postfach in ein anderes.

Habe das Script noch gefunden - bitte unbedingt vorher testen, da ich nicht mehr weiss ob es funktioniert hat.

<?php
// alle Fehler anzeigen
error_reporting(E_ALL);
// Fehler in der Webseite anzeigen (nicht in Produktion verwenden)
ini_set('display_errors', 'On');
ini_set('memory_limit', '512M'); 
 
$sourceHost = "<hostname/ip:<port>";
$sourceUser = "<user>";
$sourcePass = "<password>";
 
$destHost = "<hostname/ip:<port>";
$destUser = "<user>";
$destPass = "<password>";
 
 
// ---- ab hier nicht mehr ändern ---- //
echo "IMAP Copy<hr>";
 
/* 
// TEST
$mbox = imap_open($sourceHost, $sourceUser, $sourcePass, OP_READONLY);
 
echo "<h1>Postfächer</h1>\n";
$folders = imap_listmailbox($mbox, $sourceHost, "*")
 or die("can't connect: ".imap_last_error()); 
 
if ($folders == false) {
    echo "Abruf fehlgeschlagen<br />\n";
} else {
    foreach ($folders as $val) {
        echo $val . "<br />\n";
    }
}
 
echo "<h1>Nachrichten in INBOX</h1>\n";
$headers = imap_headers($mbox);
 
if ($headers == false) {
    echo "Abruf fehlgeschlagen<br />\n";
} else {
    foreach ($headers as $val) {
        echo $val . "<br />\n";
    }
}
 
imap_close($mbox);
*/
 
// ----------------------------------------------------------------------------
 
// $mailbox = "INBOX";
 
$sourcestream = imap_open("{".$sourceHost."}", $sourceUser, $sourcePass, OP_READONLY) 
				or die(" can't connect: ".imap_last_error());
$deststream = imap_open("{".$destHost."}", $destUser, $destPass)
			  or die(" can't connect: ".imap_last_error());
 
if ($sourcestream && $deststream) {
 
	$sourcelist = imap_list($sourcestream, "{".$sourceHost."}", "*");
	$destlist = imap_list($deststream, "{".$destHost."}", "*");
 
	echo "Quelle:<br>";
	print_r($sourcelist);
	echo "<hr>Ziel:<br>";
	print_r($destlist);
 
	//   if (0) {
	if (is_array($sourcelist)) {
		foreach($sourcelist as $mailbox) {
			$pos = strpos($mailbox,"}");
			$mailbox = substr($mailbox,$pos+1);
			if (! stristr($mailbox,"Shared")) {
				// if ($mailbox == "INBOX") {
				echo "<hr>".$mailbox."<br>";
 
				$sourcembox = imap_open("{".$sourceHost."}".$mailbox, $sourceUser, $sourcePass)
							or die("Quelle can't connect: ".imap_last_error());
				// $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<br>";
						} else {
							echo "NOT done<br>";
						}
					}
 
					$destmbox = imap_open("{".$destHost."}".$mailbox, $destUser, $destPass);
//----------
					if ($destmbox)  {                    
                        $headers = imap_headers($sourcembox);
                        $total = count($headers);
                        $n = 1;
                        echo "$total items in $mailbox<br>";
                            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);
								$body = "test";
								flush();/*
                                    if (imap_append($destmbox,"{".$destHost."}".$mailbox,$messageHeader."\r\n".$body))
										{	
                                    echo "done\n";
                                    } else {
                                    echo "NOT done\n";
                                    }
								echo imap_last_error();
								*/
                                $n++;
                                }
                            }
					}
//----------
 
					imap_close($destmbox);
				}
				imap_close($sourcembox);
			}
		}
	}
	imap_close($sourcestream);
	imap_close($deststream);
}
Diese Website verwendet nur für den Betrieb notwendige Cookies. Durch die Nutzung der Website stimmen Sie dem Speichern von Cookies auf Ihrem Computer sowie den Datenschutzbestimmungen zu. Wenn Sie nicht einverstanden sind, verlassen Sie die Website. Weitere Information
programmieren:php:programme (122 views) · Zuletzt geändert: 13/01/2021 12:06 von conny