EDIT: My problem is now SOLVED. See the second post for my working script
Hi guys,
First up- Hi, this is my first post here

Second- Sorry this is such a long post- please bear with me!
Third- Any help you can offer will be much appreciated
Scenario:
I'm currently trying to finish a perl script that I'm writing to migrate my users from openwebmail to zimbra.
The script, when working properly, will do the following:
-Read through a /mail folder, which contains a folder for each user (these folders are named in the format first_initial.surname)
-Once in a user folder, decend another level to the user's mail folder
-Once in the user's mail folder, process the mbox files (processing involves confirming that a file IS a mbox file, creating a zimbra folder with the same name as the mbox file, outputting the messages contained in a mbox file to a local location, and importing the messages from the mbox file into the newly created zimbra folder)
In other words, on my zimbra server there is a folder:
/tmp/mail
within which there are all my user's folders from openwebmail:
/tmp/mail/t.brown
/tmp/mail/a.user
...
within each user folder, there is a mail folder:
/tmp/mail/a.user/mail
within each user's mail folder are the mbox files (along with some other junk).
Within the script is basic checking to confirm that a folder is a user folder (looks for a '.' as the second character in the folder name) and checking to confirm that a file is an mbox file (read the first line of the file, and look for the 'F' in 'From', which is always the first line of an mbox file).
And yes, I am very new to perl, and yes I used the mbox migration example as a starting point.
My Problem:
Anyhow, the script in its current form works in every way EXCEPT that for some reason it seems to 'skip' some messages when processing mbox files. For example, I have an mbox file that I know has 10 messages contained in it, and it is processed without error by my script. When I look in zimbra the appropriate folder has been created in the right user account, and emails are there, just not ALL of them. From what I can see, sometimes it reaches a particular email and then proceeds to 'lump' all remaining emails into a single message.
This doesn't happen with all mbox files though- there are some mbox files where ALL of the emails are properly processed and appear in the new zimbra folder. I have noticed that the mbox files that are processed correctly are the ones where all of the emails contained within are of the same MIME type- so my issue my be related to that.
My script Code:
#!/usr/bin/perl
# mbox to Zimbra mailfile conversion
# last updated 24.8.07
# Variable declaration
use strict;
use Email::Folder;
use Mail::Mailer;
use MIME::Parser;
use Net::SMTP;
use Fcntl;
use Getopt::Std;
use File::Basename;
use Email::FolderType qw(folder_type);
my $email;
my $user;
my $next = "";
my $where = "";
my $next2 = "";
my $where2 = "";
my $dirname;
my $dirname2;
my $tempword;
my $flagset = 0;
my $message_hold;
my @messages;
my $count = 0;
my $text2;
my $test_pos2;
system ("mkdir /tmp/mail_output");
# Begin reading through the user email subfolders
my @maildir2 = glob("/tmp/mail/$user/mail/*.*");
if (-d "/tmp/mail") {
$where2 = "/tmp/mail/";
} else {
$where2 = "/tmp/mail/";
}
while (defined($next2 = <$where2/*>)) {
$user = basename($next2);
$text2 = $user;
print "$text2\n";
$test_pos2 = index $text2,".";
print "$test_pos2\n";
if ($test_pos2 == "1") {
$user = basename($next2);
$email = $user."\@my.domain.goes.here";
print "Username: $user\n";
print "Email: $email\n";
#---------------------------------------------------------------------
# Process files in current directory, replace spaces with underscores
opendir(DIR, "/tmp/mail/$user/mail") or die $!;
my @files = readdir(DIR);
close(DIR);
for (@files) {
next if -d;
next if /^\./;
my $new_name = $_;
$new_name =~ s/ /_/g;
rename("/tmp/mail/$user/mail/$_" , "/tmp/mail/$user/mail/$new_name") or die $!;
}
#--------------------------------------------------------------------
# Setup directory to store converted mail, begin loop to read through mail
# in user's folder
system ("mkdir /tmp/$user");
my @maildir = glob("/tmp/mail/$user/mail/*.*");
if (-d "/tmp/mail/$user/mail/") {
$where = "/tmp/mail/$user/mail/";
} else {
$where = "/tmp/mail/$user/mail/";
}
while (defined($next = <$where/*>)) {
$dirname = basename ($next);
my $folder = Email::Folder->new($next ||
die "Usage: $0 mbox dest_address [smtp server]
Forward all mail found in mail file mbox to address."
);
print folder_type "$next";
print "\n";
#-------------------------------------------------------------------------------
#Tests to see if the file is a valid mail file, sets a flag if it is
my $i=0;
my $text;
my $test_pos;
$flagset = 0;
print "$dirname\n";
open (mailfile, "$next")|| die ("Could not open file <br> $!");
$text = <mailfile>;
print "Text on first line: $text\n";
$test_pos = index $text,"F";
if ($test_pos == "0") {
@messages=$folder->messages;
print "message: @messages\n";
my $total=@messages;
$message_hold = @messages;
$flagset = 1;
close mailfile;
}
if ($test_pos != "0"){
print("Not a mail file\n");
close mailfile;
}
#---------------------------------------------------------------------------------
# If the file has been flagged a mail file, process it
if ($flagset == "1") {
system ("mkdir /tmp/$user/$dirname");
system ("/opt/zimbra/bin/zmprov -z selectMailbox $user cf /$dirname");
$count = 0;
foreach (@messages){
$count++;
my $parser = new MIME::Parser;
# Parser options-----------------------------------------------------------------
# $parser->output_under("/tmp/mail_output/");
# $parser->decode_headers(0);
# $parser->ignore_errors(0);
# $parser->extract_uuencode(0);
# $parser->extract_nested_messages(0);
#-------------------------------------------------------------------------------
my $entity = $parser->parse_data($_->as_string);
print "entity: $entity\n";
my $header = $entity->head;
print "header: $header\n";
my $sender = $entity->head->get('From');
next if $header->get("subject") =~ m/FOLDER INTERNAL/;
$header->replace('To', $email);
$header->delete('Received');
$header->delete('MIME-Version');
$header->delete('Return-Path');
$header->delete('User-Agent');
$header->delete('Message-ID');
$header->delete('X-Mailer');
$header->delete('X-Security');
$header->delete('X-Spam-Checker-Version');
$entity->head($header);
$entity->sync_headers;
my $temp = $entity->as_string();
open MAILDUMP, ">/tmp/$user/$dirname/mail_$dirname$count.txt";
print MAILDUMP $temp;
close MAILDUMP;
system ("/opt/zimbra/bin/zmmailbox -z -m $user addmessage /$dirname /tmp/$user/$dirname/mail_$dirname$count.txt");
print "Done\n\n";
}
}
print "Done processing emails for user $user\n";
}
if ($test_pos2 != "1"){
print("Not a mail folder\n");
}
}
} Many thanks for any help you can give
