Solved

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

Posted on 2008-10-09
5
534 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:razorwoods
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

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

fwrite($output, implode("\n", $email));
0
 
LVL 3

Author Comment

by:razorwoods
ID: 22681918
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

0
 
LVL 4

Expert Comment

by:awessel
ID: 22682764
When you parse the message, that's a procedure call, right?  What's the line where you call the procedure?
0
 
LVL 3

Author Comment

by:razorwoods
ID: 22683258
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

0
 
LVL 4

Accepted Solution

by:
awessel earned 500 total points
ID: 22687663
This is why: $headers = array();

The $email['headers'] variable is an array, so imploding the $email variable is only part of the solution.  You also need to implode the $email['headers'] variable.

Unfortunately PHP doesn't have a built-in recursive implode.  We could build one, but that's a bit overkill for this.

See below:
$filestr = implode("\n", $email['headers']);

$filestr .= "\n\n" . $email['message'];
 

$output = fopen("test.txt", "a");

fwrite($output, $filestr);

fclose($output);

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calculate values in an array 18 32
Passing variables to stored procedure 3 34
Problem with Simple PHP/mySQL Query 3 50
Should I use subdomains or addon domains? 3 33
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now