IMAP Copy to local file.

I had a mail server that only supported IMAP, but their webmail interface is horrible. So I decided to write a small script to grab the email from the imap server and save it locally. In my case the local directory is the mail directory in cpanel so I can use gmail to grab the emails via POP3 to use gmail. The mail flow is:

IMAP SERVER -> SCRIPT -> LOCAL POP3 -> GMAIL

imapscript

 Nmsgs}",0);
foreach ($result as $overview) 
{
	echo "$overview->subject\n";
	$mailfile = tempnam("/home/mth/mail/stephenjc.com/asa/cur/","EMAIL-");
	$message =  imap_fetchbody($srcstream,$overview->uid,"",FT_UID);
	$localmail = fopen($mailfile, "w");
	fwrite($localmail,$message);
	fclose($localmail);
	imap_mail_move($srcstream,$overview->msgno,'Forwarded'); 

}

imap_expunge($srcstream);
imap_close($srcstream);
echo "deleteing lock\n";
unlink($lockfile);

echo '     ]]> ';
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.