CoderZone.org

Category: >> PHP Code >> Transfer or migrate messages between two IMAP/POP e-mail accounts Bookmark and Share

<< lastnext >>

Snippet Name: Transfer or migrate messages between two IMAP/POP e-mail accounts

Description: Transfers messages between two IMAP/POP e-mail accounts.

Also see:
» Easy way to break apart an email a...
» Check for a Valid Email Address (V...
» Sending Email With Perl
» Check For A Valid Email Address
» Another Good Email Validator
» Send iCal Email Meeting Requests u...
» Check email address
» Send email with attachments from P...
» Is email deliverable?
» Email attachments with PHP mail()
» Email w/ attachments class
» Automatically process email
» Live validation of email address
» Send email from Oracle
» Fake email addresses
» Validate email address #3
» Validate email address #2
» Validate email address #1

Comment: (none)

Author: Kaspars Dambis
Language: PHP
Highlight Mode: PHP
Last Modified: April 03rd, 2011

/*
mig - a command line PHP tool for transfering emails from one host to another.
 
Created by Kaspars Dambis 
http://konstruktors.com
kaspars@konstruktors.com
*/
 
function m_connect() {
	global $msrc, $mdst;
 
	// FROM
	$msrc = imap_open(
		'{mail.example.lv:143/imap/novalidate-cert}', // host
		'user@example.lv', // username
		'pass123') // password
		or die("Can't connect: " . imap_last_error());
 
	// TO
	$mdst = imap_open(
		'{imap.gmail.com:993/imap/ssl/novalidate-cert}', // host
		'username@gmail.com', // username
		'pass123') // password
		or die("can't connect: " . imap_last_error());
}
 
// Go!
m_connect();
 
$start = 1;
$end = 1;
 
// Find the last message transfered
if ($dst_status = imap_check($mdst)) {
	$start = $dst_status->Nmsgs;
}
 
// Check how many messages are the source.
if ($src_status = imap_check($msrc)) {
	$end = $src_status->Nmsgs;
}
 
echo "Getting destination mailbox address...\n";
$to_box = imap_mailboxmsginfo($mdst)->Mailbox;
 
echo "Started! - start: $start / end: $end";
 
for ($i = $start; $i <= $end; $i++) {
	// Check if we are still connected
	if (($i - $start) % 5 == 0) {
		if (!imap_ping($msrc) || !imap_ping($mdst)) {
			echo "\n\n Connection lost! Trying to reconnect ...\n\n";
			m_connect();
		} else {
			echo "\n\n Connection OK! \n\n";
		}
	}
 
	// Check if message doesn't exist at the destination	
	if ($message = imap_fetchbody($msrc, $i, null)) {
		if ($m = imap_headerinfo($msrc, $i)) {
			echo "\n" . $i . " / $end" . "\n from: " . $m->fromaddress . "\n to: " . $m->toaddress . "\n subject: " . $m->subject . "\n size: " . format_size($m->Size) . "\n date: " . $m->date . "\n\n";
			imap_append($mdst, $to_box, $message);
		}
	}
}
 
echo "Ended!\n";
 
imap_close($msrc);
imap_close($mdst);
 
function format_size($size) {
      $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
      if ($size == 0) { return('n/a'); } else {
      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), $i > 1 ? 2 : 0) . $sizes[$i]); }
}
 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 17