[PHP/MySQl] Piping email and MIME
08-10-2010, 01:55 PM
|
[PHP/MySQl] Piping email and MIME
|
Posts: 3
Name: Lennert
|
Hello
I have been working on a project, a ticket system.
I have created an e-mail adres in DirectAdmin, support@raskattenonline.be .
I have created also a forwarder from support@raskattenonline.be to the PHP-script.
Now I want to parse the message (MIME) to a clean email, and save it into the database.
I have found a parser class on the internet en integrated in my script, but it doesn't work.
When i send an e-mail, it saves the email in the database but the message in the email doesn't save, the message-field is empty.
This are my scripts.
to_db.php
PHP Code:
<?php
$fd = fopen("php://stdin", "r");
$email = ""; while (!feof($fd)) { $email .= fread($fd, 1024); } fclose($fd);
require_once('parser.class.php');
$lines = explode("\n", $email);
// empty vars $from = ""; $date = ""; $subject = ""; $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]; }
} else { // not a header, but message $message .= $lines[$i]."\n"; }
if (trim($lines[$i])=="") { // empty line, header section has ended $splittingheaders = false; } }
//*** make a connection to the database: *** $handle = mysql_connect("localhost","***********","*********") or die(mysql_error()); mysql_select_db("**********") or die(mysql_error()); //********************************** $when = date("Y-m-d G:i:s"); $data = explode('@',$from); $username = $data[0]; mysql_query("INSERT INTO `support_email` (`subject`,`date`,`content`,`author`) VALUES ('".$subject." ".$subject2."', '".$when."', '".$parser->message['plain']."', '".$from."')") or die(mysql_error()); mysql_close($handle);
?>
parser.class.php
PHP Code:
<?php
/** * Class for parsing raw emails * * This class is used to parse raw emails into * logical easy to use parts. * * @author Joshua Gilman * @package Parser */ class Parser { /** * Determines how a newline is parsed * * @var String */ var $P_NEWLINE = "\r\n";
/** * Contains the raw header of the email * * @var String */ var $header = NULL;
/** * Contains the boundary used to parse the raw email * * @var String */ var $boundary = NULL;
/** * Contains everything below the header of the raw email * * @var String */ var $content = NULL;
/** * Contains who the email is addressed to * * @var String */ var $to = NULL;
/** * Contains who the email is from * * @var String */ var $from = NULL;
/** * Contains the subject of the email * * @var String */ var $subject = NULL;
/** * Contains all the types of messages sent * * @example * <code> * $parser->message['plain'] // Returns the plain text message * $parser->message['htmk'] // Returns the html formatted message * </code> * * @var Mixed */ var $message = array();
/** * Contains all the parsed attachments in the raw email * * @var Mixed */ var $files = array();
/** * The constructor of the class * * This function loads and parses the raw email given ($mail) * and prepares it for usage * * @param String $mail * @return void */ function __construct($email) { if (empty($email)) { throw new Exception("Invalid email argument; Email cannot be empty"); }
// Load everything up // $this->load_parts($email); $this->load_contents(); $this->load_message(); $this->load_files(); }
/** * Sets up the class for other functions * * This function parses the boundary of the raw email * then preceeds to parse the header and content of * the raw email for other functions to use * * @param String $content * @return void */ function load_parts($content) { if ($istart = strpos($content, "Content-Type:")) { if ($istart = strpos($content, "boundary=\"", $istart)) { $istart += strlen("boundary=\""); $iend = strpos($content, "\"", $istart); $this->boundary = substr($content, $istart, $iend - $istart); } }
if (!$this->boundary) { $this->boundary = $this->P_NEWLINE; }
$parts = split($this->boundary, $content);
$header1 = array_shift($parts); $header2 = array_shift($parts);
$this->header = $header1 . $header2; $this->content = implode($parts, $this->boundary); }
/** * Parses the basic content of the email * * This function parses the to, from, and subject * from the raw email's header * * @return void */ function load_contents() { if (preg_match("/To: (.*)/", $this->header, $match)) { $this->to = $match[1]; if (preg_match("/.*<(.*)>/", $this->to, $match)) { $this->to = $match[1]; } }
if (preg_match("/From: (.*)/", $this->header, $match)) { $this->from = $match[1]; }
if (preg_match("/Subject: (.*)/", $this->header, $match)) { $this->subject = $match[1]; } }
/** * Parses the message from the email * * This function parses the two common formats of * a raw message, plain text, and html formatted. * It loads both (if either one exists) into an * associative array based on their names * * @return void */ function load_message() { if ($istart = strpos($this->content, "Content-Type: text/plain;")) { $istart = strpos($this->content, $this->P_NEWLINE . $this->P_NEWLINE, $istart); $istart += strlen($this->P_NEWLINE . $this->P_NEWLINE); $iend = strpos($this->content, $this->boundary); $this->message['plain'] = substr($this->content, $istart, $iend - $istart); }
if ($istart = strpos($this->content, "Content-Type: text/html;")) { $istart = strpos($this->content, $this->P_NEWLINE . $this->P_NEWLINE, $istart); $istart += strlen($this->P_NEWLINE . $this->P_NEWLINE); $iend = strpos($this->content, $this->boundary); $this->message['html'] = substr($this->content, $istart, $iend - $istart); } } }
?>
Who can i help me ?
Thank you. 
|
|
|
|
08-10-2010, 03:59 PM
|
Re: [PHP/MySQl] Piping email and MIME
|
Posts: 149
|
I can say it's a hard task parsing email. Try CPAN PHP/Perl classes.
|
|
|
|
08-10-2010, 04:53 PM
|
Re: [PHP/MySQl] Piping email and MIME
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
The problem is here:
PHP Code:
mysql_query("INSERT INTO `support_email` (`subject`,`date`,`content`,`author`) VALUES ('".$subject." ".$subject2."', '".$when."', '".$parser->message['plain']."', '".$from."')") or die(mysql_error());
PHP Code:
$parser->message['plain']
should be:
You include the parser, but you never actually use it.
Quote:
Originally Posted by mimamo
I can say it's a hard task parsing email. Try CPAN PHP/Perl classes.
|
Did you even bother reading the thread?
Last edited by NullPointer; 08-10-2010 at 04:55 PM..
|
|
|
|
08-10-2010, 07:08 PM
|
Re: [PHP/MySQl] Piping email and MIME
|
Posts: 3
Name: Lennert
|
But I want to use the parser, the $message gives the headers, i don't want the headers :s
|
|
|
|
08-10-2010, 07:25 PM
|
Re: [PHP/MySQl] Piping email and MIME
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
Quote:
Originally Posted by Lennert
But I want to use the parser
|
The do something like this:
PHP Code:
<?php
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
require_once('parser.class.php');
$parser = new Parser($email);
//*** make a connection to the database: ***
$handle = mysql_connect("localhost","***********","*********") or die(mysql_error());
mysql_select_db("**********") or die(mysql_error());
//**********************************
$when = date("Y-m-d G:i:s");
mysql_query("INSERT INTO `support_email` (`subject`,`date`,`content`,`author`) VALUES ('".$parser->subject."', '".$when."', '".$parser->message['plain']."', '".$parser->from."')") or die(mysql_error());
mysql_close($handle);
?>
|
|
|
|
08-11-2010, 02:57 AM
|
Re: [PHP/MySQl] Piping email and MIME
|
Posts: 3
Name: Lennert
|
I noticed now that the parser doest remove a part of the header, he works now but i want that he remove that piece.
This is what I get in the database.
Code:
tent-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
ERH=A3
QETH
QTH
=
--
And if I change in the INSERT-query the $parser->message['plain'], to $parser->message['html']. He doesn't give me the HTML-message, also the plain, not the HTML-version.
Now he change the comma into =A3 , that's not good for reading the message.
|
|
|
|
|
« Reply to [PHP/MySQl] Piping email and MIME
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|