Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
[PHP/MySQl] Piping email and MIME
Old 08-10-2010, 01:55 PM [PHP/MySQl] Piping email and MIME
Junior Talker

Posts: 3
Name: Lennert
Trades: 0
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($fd1024);
  }
  
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.
Lennert is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-10-2010, 03:59 PM Re: [PHP/MySQl] Piping email and MIME
Extreme Talker

Posts: 149
Trades: 0
I can say it's a hard task parsing email. Try CPAN PHP/Perl classes.
__________________
Free
Please login or register to view this content. Registration is FREE

Visit our
Please login or register to view this content. Registration is FREE
and
Please login or register to view this content. Registration is FREE
mimamo is offline
Reply With Quote
View Public Profile
 
Old 08-10-2010, 04:53 PM Re: [PHP/MySQl] Piping email and MIME
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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:
PHP Code:
$message 
You include the parser, but you never actually use it.

Quote:
Originally Posted by mimamo View Post
I can say it's a hard task parsing email. Try CPAN PHP/Perl classes.
Did you even bother reading the thread?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 08-10-2010 at 04:55 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-10-2010, 07:08 PM Re: [PHP/MySQl] Piping email and MIME
Junior Talker

Posts: 3
Name: Lennert
Trades: 0
But I want to use the parser, the $message gives the headers, i don't want the headers :s
Lennert is offline
Reply With Quote
View Public Profile
 
Old 08-10-2010, 07:25 PM Re: [PHP/MySQl] Piping email and MIME
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Lennert View Post
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($fd1024);
}
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);
?>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-11-2010, 02:57 AM Re: [PHP/MySQl] Piping email and MIME
Junior Talker

Posts: 3
Name: Lennert
Trades: 0
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.
Lennert is offline
Reply With Quote
View Public Profile
 
Old 10-03-2010, 07:52 AM Re: [PHP/MySQl] Piping email and MIME
Junior Talker

Posts: 1
Name: Kerry James
Trades: 0
I haven't seen this parser class before, but I have had good luck with Pear's Mail_MimeDecode
http://pear.php.net/package/Mail_mimeDecode/redirected
The parsing is exceptionally good and predictable.

If you can't install it with Pear I would recommend looking at a remotely hosted service to forward your emails. This would parse them and return the parts to your script. Some good ones for this are:
http://smtp2web.com
http://mailhooks.com
http://www.mailnuggets.com
KerryJames is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to [PHP/MySQl] Piping email and MIME
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25678 seconds with 12 queries