I am currently piping mail to a script that does a bit of parsing as per this article:

http://www.devarticles.com/c/a/PHP/Incoming-Mail-and-PHP/1/

Email piped to the script gets recorded as $email. The script then parses it like this:

http://www.filefarmer.com/2/bitt3n/example.html

I want to parse $email more completely. I want to isolate fully the message body and email addresses, and ideally also isolate the message from any text that comprises a message to which the message body is a reply. (Ultimately I hope to handle attachments.)

In a perfect world, some function would take $email as its argument and returns variables $to, $from, $subject, $message, $num_attachments, and $attachments_array with the array indicating the name, type, size and filepath of each attachment saved by the function (which saves each attachment to a specified directory if this attachment meets the size, type and number criteria).

Is there some library I can install that has such a function? Searching around I found some software called ripMIME that looks interesting (http://www.pldaniels.com/ripmime/), but I am not sure that is what I want and it doesn’t appear to come with any documentation. Also I’ve never needed to install a library before, and I believe I would have to get a ripMIME binary to use it, since it's written in C and needs to be compiled.

I also found http://pear.php.net/manual/en/package.mail.mail-mime.php, so that is another option I am considering.

Thanks for your help.

    well, the message body in proper emails is always separeted from the header by a blank line (\n\n or \r\n\r\n). Your HTML page just verifies that, so does the article:

    An empty line marks the end of the header part. After the first empty line, follows the body of the email.

    SO you can do thsi:

    <?php
    $email; // The var holding your email info
    
    $h_pos = strpos($email, "\n\n");
    $headers = substr($email, 0, $h_pos);
    $body = substr($email, $h_pos);
    
    echo 'Headers:<br>'.$headers.'<hr>'.$body;
    ?>

    That should give you a separated body and header strings. Then, each line should be terminated by \n or \r\n. It's system dependant, but most likely it's "\n".

      11 days later

      I have never been able to get that script to work. Is your server running Qmail?

        4bidden wrote:

        I have never been able to get that script to work. Is your server running Qmail?

        yep. it took a few tries though. do you know what is going wrong exactly?

        thanks for the help bpat1434, I did figure out the parsing.

          Well now I get no bounce email, but the script also does not run. The script currently just sends an email to me so I know it worked, but I never get the email and I've tried various addresses to make sure a spam filter wasn't blocking it.

          Can you copy/paste here EXACTLY what you have in your qmail file?

          I have mine setup as a real email, "test". So there is a file .qmail-test in the root directory which I have been adding the pipe to. Did you setup your pipe in the same user file?

            hm.. basically what I did to get it to work was put the path to the script in a .forward file, change the first line of the script to the path to PHP (finding the correct path using the shell command "which php"), and then chmodded the script to 755. One problem I had was that editing the first line of the script in Dreamweaver caused problems, so I had to do that part using PICO and then it worked. Also, my first host didn't allow piping, and it took me a long time to realize that was the problem. When I switched hosts it worked fine.

            To test the script I used the shell command:

            cat file-which-contains-a-valid-mail-address | /pathtomailscript/incomingmailscript.php

            which lets you see whether it works without sending email and waiting.

            here is my exact script if it helps (change the email address at the bottom, obviously -- I think this is verbatim from the article, aside from the top line). good luck and let me know if I can add anything.

            #!/usr/local/bin/php
            <?php
            
            //The email is sent to the script through stdin. This is a special 'file' that can be reached by opening php://stdin. We will do that now and read the email.
            
            // read from stdin
            $fd = fopen("php://stdin", "r");
            $email = "";
            while (!feof($fd)) {
            $email .= fread($fd, 1024);
            }
            fclose($fd);
            
            //Now we have the full text of the email in the $email variable, we can start splitting headers from body. We will do that line by line, so the first step would be splitting the email. We also empty the variables that we will fill with the From header, the Subject header, and save other information in.
            
            // handle email
            $lines = explode("\n", $email);
            // empty vars
            $from = "";
            $subject = "";
            $headers = "";
            $message = "";
            $splittingheaders = true;
            
            //We have just set the $splittingheaders variable to true. As long as this variable is true, and we have not yet seen an empty line, the text should be added to $headers. If we find a Subject or a From header, we will save it in the appropriate variable.
            
            //After we have seen the first empty line, we have processed the headers and can start adding the lines to $message.
            
            for ($i=0; $i<count($lines); $i++) {
            if ($splittingheaders) {
            // this is a header
            $headers .= $lines[$i]."\n";
            // look out for special headers
            if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
            $subject = $matches[1];
            }
            if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
            $from = $matches[1];
            }
            } else {
            // not a header, but message
            $message .= $lines[$i]."\n";
            }
            if (trim($lines[$i])=="") {
            // empty line, header section has ended
            $splittingheaders = false;
            }
            }
                                $body = "Success.";
                                mail('myemail@gmail.com', 'Success', $body, 'From: noreply@nourl.com');
            ?>

              My script I don't think is the problem, its getting it to run. Few questions.

              What is the full contents and exact file name of your .forward file?
              You said this is qmail, not sendmail, right?
              Where in your directory structure is the .forward file located?

                2 years later

                Sorry to revive a dead thread, but I'm hoping someone can help me.

                I have it up and running. It works great for plain text emails, but for HTML e-mails, I'm finding it a pain.

                Is there a set of simple instructions to extra just the e-mail text?

                This is what I get: (after from, subject) have been removed.

                ------=Part_19278_9286714.1194157132468 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline This is testing the HTML email ------=Part_19278_9286714.1194157132468 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline This is testing the HTML email ------=_Part_19278_9286714.1194157132468--

                  Write a Reply...