Need help in email parsing PHP script

Discussion in 'PHP' started by tittbit, Nov 25, 2011.

  1. #1
    Hi all PHP experts

    i have code that i used to parse incoming mails and put it into database, but when it parse emails it does not parse headers successfully of multipart email, mime emails.

    The script is

    #!/usr/bin/php -q
    <?php
    include './database.php';
    
    // read from stdin
    $fd = fopen("php://stdin", "r"); //php://stdin
    $email = "";
    while (!feof($fd)&&!(strlen($email)>=5000)) {
        $email .= fread($fd, 1024);
    }
    fclose($fd);
    
    // handle email
    $lines = explode("\n", $email);
    
    // empty vars
    $recipient ="";
    $domain = "";
    $from = "";
    $subject = "";
    $headers = "";
    $msg = "";
    $splittingheaders = true;
    
    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];
            }
            if (preg_match("/^To:.*[<\s](.+)@.+/", $lines[$i], $matches)) {
                $recipient = strtolower($matches[1]);
            }
            if (preg_match("/^To:.+@(.+[^>])/", $lines[$i], $matches)) {
                $domain = $matches[1];
            }
        } else {
            // not a header, but message
            $msg .= $lines[$i]."\n";
        }
    
        if (trim($lines[$i])=="") {
            // empty line, header section has ended
            $splittingheaders = false;
        }
    }
    
    //echo htmlspecialchars($email);
    $recipient = substr($recipient, 0, 15);
    
    $recipient = mysql_real_escape_string($recipient);
    $domain = mysql_real_escape_string($domain);
    $from = mysql_real_escape_string($from);
    $subject = mysql_real_escape_string($subject);
    $headers = mysql_real_escape_string($headers);
    $msg = mysql_real_escape_string($msg);
    
    $query = "INSERT INTO emails (recipient, domain , sender, subject, header, msg, ts) VALUES ('$recipient','$domain' , '$from', '$subject', '$headers', '$msg', CURRENT_TIMESTAMP)";
    
    mysql_query($query) or die('Error, insert query failed');
    
    ?>
    Code (markup):
    following is the output of this script.

    In most cases, this works fine, however, whenever I send an email through the pipe from an gmail account this error occurs

    This is a multi-part message in MIME format.
    
    ------=_NextPart_000_0105_01C8D23F.DE1CA150
    Content-Type: text/plain;
    charset="iso-8859-1"
    Content-Transfer-Encoding: quoted-printable
    
    This is the actual message
    ------=_NextPart_000_0105_01C8D23F.DE1CA150
    Content-Type: text/html;
    charset="iso-8859-1"
    Content-Transfer-Encoding: quoted-printable
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD>
    <META http-equiv=3DContent-Type =
    content=3Dtext/html;charset=3Diso-8859-1>
    <STYLE></STYLE>
    
    <META content=3D"MSHTML 6.00.6000.16681" name=3DGENERATOR></HEAD>
    <BODY id=3DMailContainerBody=20
    style=3D"PADDING-LEFT: 10px; FONT-WEIGHT: normal; FONT-SIZE: 10pt; =
    COLOR: #000000; BORDER-TOP-STYLE: none; PADDING-TOP: 15px; FONT-STYLE: =
    normal; FONT-FAMILY: Verdana; BORDER-RIGHT-STYLE: none; =
    BORDER-LEFT-STYLE: none; TEXT-DECORATION: none; BORDER-BOTTOM-STYLE: =
    none"=20
    leftMargin=3D0 topMargin=3D0 acc_role=3D"text" CanvasTabStop=3D"true"=20
    name=3D"Compose message area"><!--[gte IE 5]><?xml:namespace =
    prefix=3D"v" /><?xml:namespace prefix=3D"o" /><![endif]-->
    <DIV>This is the actual message</DIV></BODY></HTML>
    
    ------=_NextPart_000_0105_01C8D23F.DE1CA150--
    Code (markup):
    Please i ask for help from all php experts
     
    tittbit, Nov 25, 2011 IP