imap_body()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • phil.skuse@vicorp.com

    imap_body()


    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="{mails erver.domain.co m:143}INBOX";
    $mailbox="DOMAI N\\user\\user";
    $authpass="pass word";
    $mbox = imap_open($fold er,$mailbox,$au thpass,OP_READO NLY);
    if (!$mbox)
    {
    print "imap_open( ) failed.\n";
    exit(1);
    }
    $message_count= imap_num_msg($m box);
    for ($i=1;$i<=$mess age_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($mbo x);
    }
    ?>

  • Janwillem Borleffs

    #2
    Re: imap_body()

    phil.skuse@vico rp.com wrote:[color=blue]
    > 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.
    >[/color]

    You are storing the output of imap_body() into a variable, which may be the
    cause of your problem (with large emails).
    [color=blue]
    > $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 );[/color]

    Try to do it like this:

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


    JW



    Comment

    • phil.skuse@vicorp.com

      #3
      Re: imap_body()

      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.

      Comment

      Working...