Forum Moderators: coopster

Message Too Old, No Replies

[PHP/MySQl] Piping email and MIME

Piping email and MIME

         

Lennerth

7:16 pm on Aug 10, 2010 (gmt 0)

10+ Year Member



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


$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

/**
* 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. :)

morehawes

10:33 am on Aug 11, 2010 (gmt 0)

10+ Year Member



Hi and welcome to Webmaster World!

The parser class you are using seems to be complicating things which makes debugging difficult. HTML emails in PHP are actually very simple so perhaps go back to basics by sending the email directly calling PHP's mail() function and don't worry about the parser for now. They have a HTML email example an loads of sanitising info on their docs page here : [uk.php.net...]

Once you have that working perhaps then you can use a parser to do more advanced things. I always find with this sort of debugging that going back to basics help - especially when copy & pasting lots of other peoples code.

Good luck and if you have any problems post here again.