By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,407 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,407 IT Pros & Developers. It's quick & easy.

imap_body()

P: n/a

I'm writing a php program to extract emails from a microsoft exchange
server and write them to files on my linux box. I'm having a problem
which looks like a memory leak in imap_body(). But I find it hard to
believe that nobody has noticed it before, so I am probably doing
something wrong.

If I watch the program using top, the SIZE and RSS rapidly increase
until the program hits the size limit. I can workaround the problem by
moving the calls to imap_open() and imap_close() inside the loop. The
imap close frees the memory that imap_body allocated, but it's very
slow and inefficient to do it that way.

If I comment out the call to imap_body() then the memory usage does not
increase, so it's definitely that line causing the problem.

I am currently using php as supplied with Fedora1. I will upgrade to a
newer version
and see if that helps.

Here's the code, expert advice please...

#!/usr/bin/php
<?php
{
$folder="{mailserver.domain.com:143}INBOX";
$mailbox="DOMAIN\\user\\user";
$authpass="password";
$mbox = imap_open($folder,$mailbox,$authpass,OP_READONLY);
if (!$mbox)
{
print "imap_open() failed.\n";
exit(1);
}
$message_count=imap_num_msg($mbox);
for ($i=1;$i<=$message_count;$i++)
{
$body = imap_body($mbox,$i,FT_PEEK);
$outfile=fopen($i.".mime","w");
if (!$outfile)
{
print "fopen() failed,\n";
exit(2);
}
fwrite($outfile,$body);
fclose($outfile);
}
imap_close($mbox);
}
?>

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ph********@vicorp.com wrote:
If I comment out the call to imap_body() then the memory usage does
not increase, so it's definitely that line causing the problem.

You are storing the output of imap_body() into a variable, which may be the
cause of your problem (with large emails).
$body = imap_body($mbox,$i,FT_PEEK);
$outfile=fopen($i.".mime","w");
if (!$outfile)
{
print "fopen() failed,\n";
exit(2);
}
fwrite($outfile,$body);
fclose($outfile);


Try to do it like this:

if (!$outfile = @fopen($i.".mime","w")) {
print "fopen() failed,\n";
exit(2);
}
fwrite($outfile, imap_body($mbox, $i, FT_PEEK));
fclose($outfile);
JW

Jul 17 '05 #2

P: n/a
Hi JW,
Thanks for the suggestions. I've followed you advice, but the problem
remains. The memory usage increases with every call to imap_body(). I
guess is really is a problem with PHP rather than with my code.

I've now upgraded PHP to 4.3.8-1.1, which is the latest rpms released
by RedHat for Fedora 1. The only option I can think of is to download
and compile the very latest version, but I'm not hopefull because I
don't see anything in the changelog relating to this problem.

Thanks again.

Phil.

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.