parsing raw email in php

前端 未结 14 460
猫巷女王i
猫巷女王i 2020-12-23 20:50

I\'m looking for good/working/simple to use php code for parsing raw email into parts.

I\'ve written a couple of brute force solutions, but every time, one small cha

相关标签:
14条回答
  • 2020-12-23 21:18

    There are Mailparse Functions you could try: http://php.net/manual/en/book.mailparse.php, not in default php conf, however.

    0 讨论(0)
  • 2020-12-23 21:19

    Try the Plancake PHP Email parser: https://github.com/plancake/official-library-php-email-parser

    I have used it for my projects. It works great, it is just one class and it is open source.

    0 讨论(0)
  • 2020-12-23 21:21

    Simple PhpMimeParser https://github.com/breakermind/PhpMimeParser Yuo can cut mime messages from files, string. Get files, html and inline images.

    $str = file_get_contents('mime-mixed-related-alternative.eml');
    
    // MimeParser
    $m = new PhpMimeParser($str);
    
    // Emails
    print_r($m->mTo);
    print_r($m->mFrom);
    
    // Message
    echo $m->mSubject;
    echo $m->mHtml;
    echo $m->mText;
    
    // Attachments and inline images
    print_r($m->mFiles);
    print_r($m->mInlineList);
    
    0 讨论(0)
  • 2020-12-23 21:22

    I cobbled this together, some code isn't mine but I don't know where it came from... I later adopted the more robust "MimeMailParser" but this works fine, I pipe my default email to it using cPanel and it works great.

    #!/usr/bin/php -q
    <?php
    // Config
    $dbuser = 'emlusr';
    $dbpass = 'pass';
    $dbname = 'email';
    $dbhost = 'localhost';
    $notify= 'services@.com'; // an email address required in case of errors
    function mailRead($iKlimit = "") 
        { 
            // Purpose: 
            //   Reads piped mail from STDIN 
            // 
            // Arguements: 
            //   $iKlimit (integer, optional): specifies after how many kilobytes reading of mail should stop 
            //   Defaults to 1024k if no value is specified 
            //     A value of -1 will cause reading to continue until the entire message has been read 
            // 
            // Return value: 
            //   A string containing the entire email, headers, body and all. 
    
            // Variable perparation         
                // Set default limit of 1024k if no limit has been specified 
                if ($iKlimit == "") { 
                    $iKlimit = 1024; 
                } 
    
                // Error strings 
                $sErrorSTDINFail = "Error - failed to read mail from STDIN!"; 
    
            // Attempt to connect to STDIN 
            $fp = fopen("php://stdin", "r"); 
    
            // Failed to connect to STDIN? (shouldn't really happen) 
            if (!$fp) { 
                echo $sErrorSTDINFail; 
                exit(); 
            } 
    
            // Create empty string for storing message 
            $sEmail = ""; 
    
            // Read message up until limit (if any) 
            if ($iKlimit == -1) { 
                while (!feof($fp)) { 
                    $sEmail .= fread($fp, 1024); 
                }                     
            } else { 
                while (!feof($fp) && $i_limit < $iKlimit) { 
                    $sEmail .= fread($fp, 1024); 
                    $i_limit++; 
                }         
            } 
    
            // Close connection to STDIN 
            fclose($fp); 
    
            // Return message 
            return $sEmail; 
        }  
    $email = mailRead();
    
    // handle email
    $lines = explode("\n", $email);
    
    // empty vars
    $from = "";
    $subject = "";
    $headers = "";
    $message = "";
    $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: (.*)/", $lines[$i], $matches)) {
                $to = $matches[1];
            }
        } else {
            // not a header, but message
            $message .= $lines[$i]."\n";
        }
    
        if (trim($lines[$i])=="") {
            // empty line, header section has ended
            $splittingheaders = false;
        }
    }
    
    if ($conn = @mysql_connect($dbhost,$dbuser,$dbpass)) {
      if(!@mysql_select_db($dbname,$conn))
        mail($email,'Email Logger Error',"There was an error selecting the email logger database.\n\n".mysql_error());
      $from    = mysql_real_escape_string($from);
      $to    = mysql_real_escape_string($to);
      $subject = mysql_real_escape_string($subject);
      $headers = mysql_real_escape_string($headers);
      $message = mysql_real_escape_string($message);
      $email   = mysql_real_escape_string($email);
      $result = @mysql_query("INSERT INTO email_log (`to`,`from`,`subject`,`headers`,`message`,`source`) VALUES('$to','$from','$subject','$headers','$message','$email')");
      if (mysql_affected_rows() == 0)
        mail($notify,'Email Logger Error',"There was an error inserting into the email logger database.\n\n".mysql_error());
    } else {
      mail($notify,'Email Logger Error',"There was an error connecting the email logger database.\n\n".mysql_error());
    }
    ?>
    
    0 讨论(0)
  • 2020-12-23 21:22

    The Pear lib Mail_mimeDecode is written in plain PHP that you can see here: Mail_mimeDecode source

    0 讨论(0)
  • 2020-12-23 21:23

    This library work very well:

    http://www.phpclasses.org/package/3169-PHP-Decode-MIME-e-mail-messages.html

    0 讨论(0)
提交回复
热议问题