Link to home
Start Free TrialLog in
Avatar of razorwoods
razorwoods

asked on

How to check pop3 email account and dump to a text file

I've been playing with some php code run from the command line on Debian Linux that checks a pop3 email box then dumps to the screen.  What I'm trying to do now is get the array output to dump to a text file.  I don't care if it still outputs to the screen I just also want it to dump to a text file.

The way this is now all I get is literally the word ' array ' in my test.txt file.

I'm using php5 and Debian stable


// Parse message
    $message = implode('', $message);
 
    // Return array
    $email = array();
    $email['message'] = $message;
    $email['headers'] = $headers;
 
    return $email;
 
}
 
$output = fopen("test.txt", "a");
fwrite($output, $email);
fclose($f);
 
 
?>

Open in new window

Avatar of awessel
awessel

That's because what you're passing to fwrite() is an array.  Try this:

fwrite($output, implode("\n", $email));
Avatar of razorwoods

ASKER

I made that change and now I am getting output to the text file, however it's just the ' message ' portion.  The ' headers ' portion is not being output to the text file, but the headers are being output to the screen on the command line.


    // Return array
    $email = array();
    $email['message'] = $message;
    $email['headers'] = $headers;
 
    return $email;
 
}
 
$output = fopen("test.txt", "a");
fwrite($output, implode("\n", $email));
fclose($output);

Open in new window

When you parse the message, that's a procedure call, right?  What's the line where you call the procedure?
Here is the entire parse section and output.



    $email = parse_email ($email);
 
    echo '<b>From: </b>' . htmlentities($email['headers']['From']) . '<br />';
    echo '<b>Subject: </b>' . htmlentities($email['headers']['Subject']) . '<br /><br />';
    echo '<b>Date: </b>' . htmlentities($email['headers']['Date']) . '<br /><br />';
    echo nl2br(htmlentities($email['message']));
    echo '<hr />';
 
 
    // Remove from mail server
    $do = $pop3->delete_mail ($i);
    if ($do == false) {
        echo $pop3->error;
    }
}
 
$pop3->close();
 
function parse_email ($email) {
    // Split header and message
    $header = array();
    $message = array();
 
    $is_header = true;
    foreach ($email as $line) {
        if ($line == '<HEADER> ' . "\r\n") continue;
        if ($line == '<MESSAGE> ' . "\r\n") continue;
        if ($line == '</MESSAGE> ' . "\r\n") continue;
        if ($line == '</HEADER> ' . "\r\n") { $is_header = false; continue; }
 
        if ($is_header == true) {
            $header[] = $line;
        } else {
            $message[] = $line;
        }
    }
 
    // Parse headers
    $headers = array();
    foreach ($header as $line) {
        $colon_pos = strpos($line, ':');
        $space_pos = strpos($line, ' ');
 
        if ($colon_pos === false OR $space_pos < $colon_pos) {
            // attach to previous
            $previous .= "\r\n" . $line;
            continue;
        }
 
        // Get key
        $key = substr($line, 0, $colon_pos);
 
        // Get value
        $value = substr($line, $colon_pos+2);
        $headers[$key] = $value;
 
        $previous =& $headers[$key];
    }
 
    // Parse message
    $message = implode('', $message);
 
    // Return array
    $email = array();
    $email['message'] = $message;
    $email['headers'] = $headers;
 
    return $email;
 
}
 
$output = fopen("test.txt", "a");
fwrite($output, implode("\n", $email));
fclose($output);
 
 
//print $email >> test.txt;
 
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of awessel
awessel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial