Hi,

I have a email piping code show below.
It works fine, except that when i print the $message, it also shows me some headers.
How do i strip the headers from the message itself?

email_piping.php file

#!/usr/bin/php -q
<?php
//header('Content-Type: text/html; charset=utf-8');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  // always modified 
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache");                          // HTTP/1.0  
date_default_timezone_set('America/New_York');
ob_start();
require_once ('includes/config.inc.php');						  
require_once ('includes/functions.inc.php');
require_once ('includes/mysql_connect.php');
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);

// handle email
$lines = explode("\n", $email);

// empty vars
$from = "";
$subject = "";
$headers = "";
$message = "";
$sendmail = "";
$splittingheaders = true;

//we have processed the headers and can start adding the lines to $message. 
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;
    }
	
	//$a = "RE: [119-24] Work Order Request - test - dsfsd";
	preg_match("/\[[0-9]*-[0-9]*\]/",$subject,$matches);
	list($jobid, $custid) = split('[-]', $matches[0]);
	$jobid = substr($jobid, 1);	//removes first string
	$custid = substr($custid, 0, (strlen($custid)-1));	//removes last string
}

$cur_date = returnLocalDate();
$cur_time = returnLocalTime();
$sendmail .= 'job id = '.$jobid.'<br/><br/>';
$sendmail .= 'customer id = '.$custid.'<br/><br/>';
$sendmail .= 'reply date = '.$cur_date.' '.$cur_time.'<br/><br/>';
$sendmail .= 'message = '.$message.'<br/><br/>';
//$sendmail .= 'from = '.$from.'\n';
//$sendmail .= 'subject = '.$subject.'\n';
//$sendmail .= 'headers = '.$headers.'\n';
//$sendmail .= 'message = '.$message.'\n';
mymailer('admin@milano4you.com','test pipe id ', $sendmail);
/*
$insertQ = mysql_query("INSERT INTO test_pipe(job_id, cust_id, description, reply_date, reply_time, displayed) 
						VALUES($jobid, $custid, '$message', '$cur_date', '$cur_time', 1)") or trigger_error("Query: $insertQ\n<br />MySQL Error: " .mysql_error());
if (mysql_affected_rows() > 0) {
	$new_reply_id = mysql_insert_id();
	echo $new_reply_id;
	$sendmail .= 'job id = '.$jobid.'\n';
	$sendmail .= 'customer id = '.$custid.'\n';
	$sendmail .= 'reply date = '.$cur_date.' '.$cur_time.'\n';
	$sendmail .= 'message = '.$message.'\n';
	$sendmail .= 'from = '.$from.'\n';
	$sendmail .= 'subject = '.$subject.'\n';
	$sendmail .= 'headers = '.$headers.'\n';
	$sendmail .= 'message = '.$message.'\n';
	mymailer('admin@milano4you.com','test pipe id '.$new_reply_id.'', $sendmail);
}
*/
return true;
if (isset($_SESSION['custfn'])){mysql_close();}
ob_end_flush();
?>

The email showed:

job id = 119

customer id = 24

reply date = 2009-03-25 07:28

message = This is a multipart message in MIME format. ------=_NextPart_000_006F_01C9AD4D.7445AC60 Content-Type: multipart/alternative; boundary="----=_NextPart_001_0070_01C9AD4D.7445AC60" ------=_NextPart_001_0070_01C9AD4D.7445AC60 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 222Meyta fsdfsaasdsada email_logo E-mail: meytal@dachooch.com IL Phone: +972-(0)54-740-7505 IL Mobile: +972-(0)4-652-2704 USA Phone: +718-404-9369 Skype: dachooch2 www.dachooch.com ------=_NextPart_001_0070_01C9AD4D.7445AC60 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
222Meyta fsdfsaasdsada

Hi,

I have a email piping code show below.
It works fine, except that when i print the $message, it also shows me some headers.
How do i strip the headers from the message itself?

email_piping.php file

#!/usr/bin/php -q
<?php
//header('Content-Type: text/html; charset=utf-8');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  // always modified 
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache");                          // HTTP/1.0  
date_default_timezone_set('America/New_York');
ob_start();
require_once ('includes/config.inc.php');						  
require_once ('includes/functions.inc.php');
require_once ('includes/mysql_connect.php');
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);

// handle email
$lines = explode("\n", $email);

// empty vars
$from = "";
$subject = "";
$headers = "";
$message = "";
$sendmail = "";
$splittingheaders = true;

//we have processed the headers and can start adding the lines to $message. 
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;
    }
	
	//$a = "RE: [119-24] Work Order Request - test - dsfsd";
	preg_match("/\[[0-9]*-[0-9]*\]/",$subject,$matches);
	list($jobid, $custid) = split('[-]', $matches[0]);
	$jobid = substr($jobid, 1);	//removes first string
	$custid = substr($custid, 0, (strlen($custid)-1));	//removes last string
}

$cur_date = returnLocalDate();
$cur_time = returnLocalTime();
$sendmail .= 'job id = '.$jobid.'<br/><br/>';
$sendmail .= 'customer id = '.$custid.'<br/><br/>';
$sendmail .= 'reply date = '.$cur_date.' '.$cur_time.'<br/><br/>';
$sendmail .= 'message = '.$message.'<br/><br/>';
//$sendmail .= 'from = '.$from.'\n';
//$sendmail .= 'subject = '.$subject.'\n';
//$sendmail .= 'headers = '.$headers.'\n';
//$sendmail .= 'message = '.$message.'\n';
mymailer('admin@milano4you.com','test pipe id ', $sendmail);
/*
$insertQ = mysql_query("INSERT INTO test_pipe(job_id, cust_id, description, reply_date, reply_time, displayed) 
						VALUES($jobid, $custid, '$message', '$cur_date', '$cur_time', 1)") or trigger_error("Query: $insertQ\n<br />MySQL Error: " .mysql_error());
if (mysql_affected_rows() > 0) {
	$new_reply_id = mysql_insert_id();
	echo $new_reply_id;
	$sendmail .= 'job id = '.$jobid.'\n';
	$sendmail .= 'customer id = '.$custid.'\n';
	$sendmail .= 'reply date = '.$cur_date.' '.$cur_time.'\n';
	$sendmail .= 'message = '.$message.'\n';
	$sendmail .= 'from = '.$from.'\n';
	$sendmail .= 'subject = '.$subject.'\n';
	$sendmail .= 'headers = '.$headers.'\n';
	$sendmail .= 'message = '.$message.'\n';
	mymailer('admin@milano4you.com','test pipe id '.$new_reply_id.'', $sendmail);
}
*/
return true;
if (isset($_SESSION['custfn'])){mysql_close();}
ob_end_flush();
?>

The email showed:

I is actually the email body that is showing up. The body is composed of multipart-mime formatting.

It is used to separate different mime (formats) in the messages, eg: plaintext, html, and file attachments etc.

You need to be able to parse the mime types, or you can use an existing mime parser.

http://pear.php.net/package/Mail_Mimedecode
http://freshmeat.net/projects/phpmimemaildecoder
http://www.phpclasses.org/browse/package/3169.html

hi again, i didnt quite understand how to use these.
could you explain please.

hi again, i didnt quite understand how to use these.
could you explain please.

You have to choose one of those libraries, depending on what your PHP build supports and which you understand better.

If you are able to install extensions for PHP or have the "MailParse" extension, then you can use PHP's build in Mime-Mail parser.

http://www.php.net/manual/en/ref.mailparse.php

This would be by far the fastest and most efficient of the choices.

If you have a specific library of choice, I can help you out with it.

Here I've written a PHP5 class for parsing mime mail messages using the PHP MailParse Extension.

<?php

/**
 * Fast Mime Mail parser Class using PHP's MailParse Extension
 * @author gabe@fijiwebdesign.com
 * @url http://www.fijiwebdesign.com/
 * @license http://creativecommons.org/licenses/by-sa/3.0/us/
 */
class MimeMailParser {
	
	/**
	 * PHP MimeParser Resource ID
	 */
	public $resource;
	
	/**
	 * A file pointer to email
	 */
	public $stream;
	
	/**
	 * A text of an email
	 */
	public $data;
	
	/**
	 * Free the held resouces
	 * @return void
	 */
	public function __destruct() {
		if (is_resource($this->stream)) {
			fclose($this->stream);
		}
		if (is_resource($this->resource)) {
			mailparse_msg_free($this->resource);
		}
	}
	
	/**
	 * Set the file path we use to get the email text
	 * @return Object MimeMailParser Instance
	 * @param $mail_path Object
	 */
	public function setPath($path) {
		// should parse message incrementally from file
		$this->resource = mailparse_msg_parse_file($path);
		$this->stream = fopen($path, 'r');
		$this->parse();
		return $this;
	}
	
	/**
	 * Set the Stream resource we use to get the email text
	 * @return Object MimeMailParser Instance
	 * @param $stream Resource
	 */
	public function setStream($stream) {
		$this->resource = mailparse_msg_create();
		$this->stream = $stream;
		// parses the message incrementally low memory usage but slower
		while(!feof($this->stream)) {
			mailparse_msg_parse($this->resource, fread($this->stream, 2082));
		}
		$this->parse();
		return $this;
	}
	
	/**
	 * Set the email text
	 * @return Object MimeMailParser Instance 
	 * @param $data String
	 */
	public function setText($data) {
		$this->resource = mailparse_msg_create();
		// does not parse incrementally, fast memory hog might explode
		mailparse_msg_parse($this->resource, $data);
		$this->data = $data;
		$this->parse();
		return $this;
	}
	
	/**
	 * Parse the Message into parts
	 * @return void
	 * @private
	 */
	private function parse() {
		$structure = mailparse_msg_get_structure($this->resource);
		$this->parts = array();
		foreach($structure as $part_id) {
			$part = mailparse_msg_get_part($this->resource, $part_id);
			$this->parts[$part_id] = mailparse_msg_get_part_data($part);
		}
	}
	
	/**
	 * Retrieve the Email Headers
	 * @return Array
	 */
	public function getHeaders() {
		if (isset($this->parts[1])) {
			return $this->getPartHeaders($this->parts[1]);
		} else {
			throw new Exception('MimeMailParser::setPath() or MimeMailParser::setText() must be called before retrieving email headers.');
		}
		return false;
	}
	
	/**
	 * Retrieve a specific Email Header
	 * @return String
	 * @param $name String Header name
	 */
	public function getHeader($name) {
		if (isset($this->parts[1])) {
			$headers = $this->getPartHeaders($this->parts[1]);
			if (isset($headers[$name])) {
				return $headers[$name];
			}
		} else {
			throw new Exception('MimeMailParser::setPath() or MimeMailParser::setText() must be called before retrieving email headers.');
		}
		return false;
	}
	
	/**
	 * Returns the email message body in the specified format
	 * @return Mixed String Body or False if not found
	 * @param $type Object[optional]
	 */
	public function getMessageBody($type = 'text') {
		$body = false;
		$mime_types = array(
			'text'=> 'text/plain',
			'html'=> 'text/html'
		);
		if (in_array($type, array_keys($mime_types))) {
			foreach($this->parts as $part) {
				if ($this->getPartContentType($part) == $mime_types[$type]) {
					$body = $this->getPartBody($part);
				}
			}
		} else {
			throw new Exception('Invalid type specified for MimeMailParser::getMessageBody. "type" can either be text or html.');
		}
		return $body;
	}
	
	/**
	 * Returns the attachments
	 * @return Array
	 * @param $type Object[optional]
	 */
	public function getAttachments() {
		$attachments = array();
		$disposition = 'attachment';
		foreach($this->parts as $part) {
				if ($this->getPartContentDisposition($part) == $disposition) {
					$attachments[] = base64_decode($this->getPartBody($part));
				}
			}
		return $attachments;
	}
	
	/**
	 * Return the Headers for a MIME part
	 * @return Array
	 * @param $part Array
	 */
	private function getPartHeaders($part) {
		if (isset($part['headers'])) {
			return $part['headers'];
		}
		return false;
	}
	
	/**
	 * Return a Specific Header for a MIME part
	 * @return Array
	 * @param $part Array
	 * @param $header String Header Name
	 */
	private function getPartHeader($part, $header) {
		if (isset($part['headers'][$header])) {
			return $part['headers'][$header];
		}
		return false;
	}
	
	/**
	 * Return the ContentType of the MIME part
	 * @return String
	 * @param $part Array
	 */
	private function getPartContentType($part) {
		if (isset($part['content-type'])) {
			return $part['content-type'];
		}
		return false;
	}
	
	/**
	 * Return the Content Disposition
	 * @return String
	 * @param $part Array
	 */
	private function getPartContentDisposition($part) {
		if (isset($part['content-disposition'])) {
			return $part['content-disposition'];
		}
		return false;
	}
	
	/**
	 * Retrieve the Body of a MIME part
	 * @return String
	 * @param $part Object
	 */
	private function getPartBody(&$part) {
		$body = '';
		if ($this->stream) {
			$body = $this->getPartBodyFromFile($part);
		} else if ($this->data) {
			$body = $this->getPartBodyFromText($part);
		} else {
			throw new Exception('MimeMailParser::setPath() or MimeMailParser::setText() must be called before retrieving email parts.');
		}
		return $body;
	}
	
	/**
	 * Retrieve the Body from a MIME part from file
	 * @return String Mime Body Part
	 * @param $part Array
	 */
	private function getPartBodyFromFile(&$part) {
		$start = $part['starting-pos-body'];
		$end = $part['ending-pos-body'];
		fseek($this->stream, $start, SEEK_SET);
		$body = fread($this->stream, $end-$start);
		return $body;
	}
	
	/**
	 * Retrieve the Body from a MIME part from text
	 * @return String Mime Body Part
	 * @param $part Array
	 */
	private function getPartBodyFromText(&$part) {
		$start = $part['starting-pos-body'];
		$end = $part['ending-pos-body'];
		$body = substr($this->data, $start, $end-$start);
		return $body;
	}
}


?>

I haven't even tested this much. Just with 3 emails, one plain-text, an HTML email and an email with attachments.

Example Usage:

// include the class file
require_once('MimeMailParser.class.php');

$MailParser = new MimeMailParser();

// set the stream resource to read the email from
$MailParser->setStream(STDIN);

// retrieve useful info
$to = $MailParser->getHeader('to');
$from = $MailParser->getHeader('from');
$subject = $MailParser->getHeader('subject');

// the text email message
$text = $MailParser->getMessageBody('text');

// the html email message, if it exists
$html = $MailParser->getMessageBody('html');

// etc...

The class is set to read from STDIN. So you don't have to do the mail parsing you have currently. It will do it all for you.

You can also specify a file to read from, or text to read from.

eg:

$MailParser->setPath('php://stdin');

or:

$text = file_get_contents('php://stdin');
$MailParser->setText($text);

Using setStream() and setPath is would ensure low memory consumption however.

I haven't added stream reading for the attachments however, so if you get attachments, know that it will give you the full attachment data. So if the attachments exceed memory limitations it will die.

forget what i just wrote above.
how do i know if i have support for "PHP MailParse Extension" ?
where in phpinfo() can i see that?

So i now updated my email_pipe.php page to:

#!/usr/bin/php -q
<?php
header('Content-Type: text/html; charset=utf-8');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  // always modified 
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache");                          // HTTP/1.0  
date_default_timezone_set('America/New_York');
ob_start();
require_once ('includes/config.inc.php');						  
require_once ('includes/functions.inc.php');
require_once ('includes/mysql_connect.php');

// include the class file
require_once('mime_parse/mime_mail_parser.php');

// empty vars
$from = "";
$subject = "";
$headers = "";
$message = "";
$text = "";
$html = "";
$sendmail = "";
$jobid = 0;
$custid = 0;
$splittingheaders = true;
$MailParser = new MimeMailParser();

// read from stdin // set the stream resource to read the email from
$MailParser->setPath('php://stdin');

// set the stream resource to read the email from
//$MailParser->setStream(STDIN);

// retrieve useful info
$to = $MailParser->getHeader('to');
$from = $MailParser->getHeader('from');
$subject = $MailParser->getHeader('subject');

// the text email message
$text = $MailParser->getMessageBody('text');

// the html email message, if it exists
$html = $MailParser->getMessageBody('html');


$sendmail .= 'to = '.$to.'<br/><br/>';
$sendmail .= 'subject = '.$subject.'<br/><br/>';
$sendmail .= 'from = '.$from.'<br/><br/>';
$sendmail .= 'text = '.$text.'<br/><br/>';
$sendmail .= 'html = '.$html.'<br/><br/>';
mymailer('admin@milano4you.com','forwarding non-reply email ', $sendmail);

return true;
if (isset($_SESSION['custfn'])){mysql_close();}
ob_end_flush();
?>

The i obvously require the page mime_mail_parser.php, which is the code you provided.

When i send an email test, i get the following error:

<b>Fatal error</b>: Call to undefined function mailparse_msg_parse_file() in <b>/home/milano4y/public_html/mime_parse/mime_mail_parser.php</b> on line <b>45</b><br />

This error points to line in setPath function:

$this->resource = mailparse_msg_parse_file($path);

Should i be setting headers or such? what am i doing wrong?

IF you don't see a reference to the extension in phpinfo() then it is not installed.

The error also indicates that it isn't installed.

You actually need shell access to install PHP extensions. So you could ask your host to install it for you.

Or you could use one of the other libraries.

hi,
ok so my host installed mailparse for me. i see it in my phpinfo.
i do get an email, but i also get mail delivery error:

<div id="Error">An error occurred in script '/home/milano4y/public_html/mime_parse/mime_mail_parser.php' on line 238:
<br /><br />
fseek() [<a href='function.fseek'>function.fseek</a>]: stream does not support seeking

this points to function "getPartBodyFromFile" and the line is "fseek($this->stream, $start, SEEK_SET);"

what shall i do?

hi,
ok so my host installed mailparse for me. i see it in my phpinfo.
i do get an email, but i also get mail delivery error:


this points to function "getPartBodyFromFile" and the line is "fseek($this->stream, $start, SEEK_SET);"

what shall i do?

Looks like STDIN does not support fseek.

You'll have to use:

$text = file_get_contents('php://stdin');
$MailParser->setText($text);

or

$text = stream_get_contents(STDIN);
$MailParser->setText($text);

etc.

This will make the whole email load into the variable $text. This however means if you have large attachments, they will be loaded into PHP's memory allocation. The max is 8mb usually so if it goes over that the script will die.

hello????

Looks like STDIN does not support fseek.

You'll have to use:

$text = file_get_contents('php://stdin');
$MailParser->setText($text);

or

$text = stream_get_contents(STDIN);
$MailParser->setText($text);

etc.

This will make the whole email load into the variable $text. This however means if you have large attachments, they will be loaded into PHP's memory allocation. The max is 8mb usually so if it goes over that the script will die.

Did you try one of these suggestions?

Hi,
Now part of my main email piping code looks like this:

// empty vars
$from = "";
$subject = "";
$headers = "";
$message = "";
$text = "";
$html = "";
$sendmail = "";
$jobid = 0;
$custid = 0;
$splittingheaders = true;
$MailParser = new MimeMailParser();

// read from stdin // set the stream resource to read the email from
$MailParser->setPath('php://stdin');

// retrieve useful info
$to = $MailParser->getHeader('to');
$from = $MailParser->getHeader('from');
$subject = $MailParser->getHeader('subject');

// the text email message
//$text = $MailParser->getMessageBody('text');
$text = file_get_contents('php://stdin');
$MailParser->setText($text);

// the html email message, if it exists
$html = $MailParser->getMessageBody('html');


$sendmail .= 'to = '.$to.'<br/><br/>';
$sendmail .= 'subject = '.$subject.'<br/><br/>';
$sendmail .= 'from = '.$from.'<br/><br/>';
$sendmail .= 'text = '.$text.'<br/><br/>';
$sendmail .= 'html = '.$html.'<br/><br/>';
mymailer('admin@milano4you.com','forwarding non-reply email ', $sendmail);

I didnt get an error email, but the regular email i am sending, which is:

to =

subject = this is a test

from = "Meytal Misrahi"

text =

html =

so from the above code the "to" and "text" part are missing.

what am i doing wrong?

Hi Digital-Ether,

I nearly forgot about this post, did you get my last reply?
Let me know...
Cheers

Hi Digital-Ether,

I nearly forgot about this post, did you get my last reply?
Let me know...
Cheers

Could you post the raw email.

As I mentioned, I only tested the 'MimeMailParser' class on 2 or 3 emails. It may not be parsing correctly.

Give the raw email I can try debugging.

Could you post the raw email.

As I mentioned, I only tested the 'MimeMailParser' class on 2 or 3 emails. It may not be parsing correctly.

Give the raw email I can try debugging.

Actually, try:

$MailParser = new MimeMailParser();

$text = file_get_contents('php://stdin');
$MailParser->setText($text);

// retrieve useful info
$to = $MailParser->getHeader('to');
$from = $MailParser->getHeader('from');
$subject = $MailParser->getHeader('subject');

// the text email message
$text = $MailParser->getMessageBody('text');


// the html email message, if it exists
$html = $MailParser->getMessageBody('html');


$sendmail .= 'to = '.$to.'<br/><br/>';
$sendmail .= 'subject = '.$subject.'<br/><br/>';
$sendmail .= 'from = '.$from.'<br/><br/>';
$sendmail .= 'text = '.$text.'<br/><br/>';
$sendmail .= 'html = '.$html.'<br/><br/>';
mymailer('admin@milano4you.com','forwarding non-reply email ', $sendmail);

what do you mean by the "raw email"?

I think after making the changes you sent me, it is starting to work:
This is what i get in my email body:

to =

subject = testhing this

from = "Meytal Misrahi"

text = Helsfsd fdsf Sd fsdf dsfs

html =
Helsfsd fdsf
Sd fsdf dsfs

1. Why is the "to" not showing though?
2. Can i also post which email sent it and which email address received the email?
3. also, can i change the "reply-to" email address so that when someone replies to the email, they reply to the correct one?

thanks so much!

I think after making the changes you sent me, it is starting to work:
This is what i get in my email body:

1. Why is the "to" not showing though?
2. Can i also post which email sent it and which email address received the email?
3. also, can i change the "reply-to" email address so that when someone replies to the email, they reply to the correct one?

thanks so much!

The email addresses derived from the MIME headers are in the format:

Full Name <email@example.com>

It may be that you received an email address such as:

<email@example.com>

When you print this out, you won't see it since the browser will interpret it as an HTML tag.

Try using htmlentities() or viewing the HTML source.

2. Can i also post which email sent it and which email address received the email?

Off course. The address that sent the email would be the "from" header. The receiver would be "delivered-to" header.

eg:

$from = $Parser->getHeader('from');
$delivered_to = $Parser->getHeader('delivered-to');

The "to" header, can contain multiple addresses. The mail server sends the email to each address in the "to" header. Each of these addresses is then added to the "delivered-to" header.

I'm purely guessing here based on the 3 emails I'm looking at - which I copied from gmail. You'd have to look at the SMTP specs to make sure.

3. also, can i change the "reply-to" email address so that when someone replies to the email, they reply to the correct one?

Don't think you can change the "reply-to" of the email you just received. The MTA is sending you a "forward" of the email, not the email itself when you pipe the email to your PHP script. You'd have to be sitting in front of the MTA, and modify the email in transit to do that.

You can however add a reply-to header to the email you are about to send through your "mymailer" function.

what do you mean by the "raw email"?

Here is an example of a raw email. (actual address/ip/hosts are replaced with xxxxx)

Delivered-To: xxxxx@xxxx.com
Received: by xxxxxxxx with SMTP id xxxxx;
        Fri, 27 Mar 2009 01:11:xxx-0700 (PDT)
Received: by xxxxxxx with SMTP id xxxx.xxx.xxxx3;
        Fri, 27 Mar 2009 01:11:38 -0700 (PDT)
Return-Path: <xxx@xxx.xxxxxxxx.com>
Received: from xxxx.xxxxx.com (host2.xxxx.com [xxxxx])
        by mx.xxxx.com with ESMTP id xxxxxx.xxxxxx;
        Fri, 27 Mar 2009 01:11:38 -0700 (PDT)
Received-SPF: pass (xxxx.com: best guess record for domain of xxx@xxx.xxx.com designates xxxx as permitted sender) client-ip=xxxxx;
Authentication-Results: mx.xxxx.com; spf=pass (xxx.com: best guess record for domain of xxx@xxx.xxx.com designates xxxxxx as permitted sender) smtp.mail=xxx@xxx.xxxx.com
Received: from xxx by xxx.xxxx.com with local (Exim 4.69)
	(envelope-from <xxx@xxx.xxxx.com>)
	id 1Ln77j-0002a5-1M
	for info@xxxxxx.com; Fri, 27 Mar 2009 04:08:43 -0400
To: info@xxxxxxx.com
Subject: buy fioricet
X-PHP-Script: www.xxxxx.com/index.php for xxxxxx
Date: Fri, 27 Mar 2009 04:09:06 -0400
From: HsvsRsvsesv <xxxx@xxxx.net>
Message-ID: <xxxxx@www.xxxxx.com>
X-Priority: 3
X-Mailer: PHPMailer [version 1.73]
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset="iso-8859-1"
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname - xxxxxx
X-AntiAbuse: Original Domain - xxxxx
X-AntiAbuse: Originator/Caller UID/GID - [xxxx] / [xxxx]
X-AntiAbuse: Sender Address Domain - xxxxx
X-Source: /usr/bin/php
X-Source-Args: /usr/bin/php 
X-Source-Dir: xxxxxx.com:/public_html

This is an enquiry e-mail via http://www.xxxxxxx.com from:
HsvsRsvsesv <sdfxvf3@xxxx.net>

buy fioricet

This is what your are receiving in your PHP script, and parsing.

This is a mime email with text only. Usually you'd have multiple parts: text, html, embedded media, attachments etc.

In your setup, this would be the value of $text in:

$text = file_get_contents('php://stdin');

which is the raw email fed to your PHP script through Standard Input ('php://stdin') by the mail server.

wanted to let you know that your snippet works wonders and i have completed my email piping, including inserting to DB, etc.

thanks so much!!!

wanted to let you know that your snippet works wonders and i have completed my email piping, including inserting to DB, etc.

thanks so much!!!

Awesome. You're most welcome.

I've added the mime mail parsing class to google code. If anyone is interested here is the project home:

http://code.google.com/p/php-mime-mail-parser/

Docs:
http://code.google.com/p/php-mime-mail-parser/wiki/RequirementsAndInstallation

Source:
http://code.google.com/p/php-mime-mail-parser/source/browse/trunk/MimeMailParser.class.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.