RMail 2 Mbox

The below code will convert eh old Emacs RMAIL folder to the mbox format (as used by services like dovecot). It will convert from one folder location, and recreate the folder structure in a new location. It will also set the permissions on folders/directories in the new location

It requires the b2m tool (emacs-common rhel package)

<?php
$username = "pfowler";
$b2m = "/usr/bin/b2m";
 
$MAIL_PATH = "/mnt/suphys/home1/$username/mail";
$DOVECOT_MAILPATH = "/storage/mailm/mailboxes/$username";
 
 
// Check that the b2m tool exists
if (!file_exists($b2m)) {
	print("**> Set a proper b2m location: $b2m\n");
	exit(1);
}
 
 
if (!file_exists($DOVECOT_MAILPATH)) {
	if (!mkdir($DOVECOT_MAILPATH, 0700)) {
		print "*-> Could not make $DOVECOT_MAILPATH\n";
		exit(1);
	}
	chown($DOVECOT_MAILPATH, $username);
}
 
// The folders in the mailbox
$folders = array();
$foldernew = 0;
$foldererr = 0;
 
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($MAIL_PATH));
foreach($iterator as $file => $cur) {
	$curpath = $cur->getPath();
	if ($curpath == "$MAIL_PATH") continue;
	if ($curpath == "$MAIL_PATH/.tmp") continue;
	if ($file == "$MAIL_PATH/.") continue;
	if ($file == "$MAIL_PATH/..") continue;
 
	// Create all the target folder for this mail
	$newpath = str_replace($MAIL_PATH, $DOVECOT_MAILPATH, $curpath);
	if (!in_array($newpath, $folders, true)) {
		$folders[] = $newpath;
 
		if (!file_exists($newpath)) {
			if (!mkdir($newpath, 0700, true)) {
				print "*-> Could not make folder $newpath\n";
				$foldererr++;
				continue;
			} else {
				chown($newpath, $username);
				$foldernew++;
			}
		}			
	}
	$mailbox = 	str_replace($MAIL_PATH . "/", "", $file);
	$cmd = "(file $file | awk -F: '{print $2}' | awk '{print $2}') 2>&1";
	$mailtype = exec($cmd);
	if ($mailtype != "RMAIL") {
		print "*-> Not RMAIL: $mailbox\n";
		continue;
	}
 
	$newfile = 	str_replace($MAIL_PATH, $DOVECOT_MAILPATH, $file);
	print "--> Convert $mailbox\n";
	$cmd = $b2m . " < $file > $newfile";
	system($cmd);
	chown($newfile, $username);
	chmod($newfile, 0600);
}
 
print "--> " . count($folders) . " folders found (Created:$foldernew, Error:$foldererr)\n";
 
?>
Print/export
QR Code
QR Code sysadmin:rmail2mbox (generated for current page)