Trying to parse email, eh? Tricky business... I think you will be best served be using a class that already deals with this, because there are about 10 more cases you haven't thought of yet. For example, you might try the mailparse object from the PEAR distribution. It is not part of the standard Pear distribution, so you will have to get it.
I assume you have PEAR installed and you have root access to your server. Try this:
-
cd /an/include/dir/for/php
pear install mailparse
This created a file, mailparse.so. You need a copy
in an include dir for you php scripts. If you are unsure
about this step, just copy mailparse.so to the dir your
php script is going to run.
-
If pear install mailparse doesn't install, it will probably be because of this:
'mbstring' PHP extension is not installed
mailparse: dependencies failed
If so, you need to recompile php with mbstring:
cd /path/to/php/source
make clean
./configure --enable-mbstring
make
make install
#Now try to install mailparse.
OK, now that you have the benefit of the mailparse class, here is how to use it:
First, suppose I have an email from Yahoo that looks like this:
Message-ID: <20030504161636.55348.qmail@web21208.mail.yahoo.com>
Date: Sun, 4 May 2003 09:16:36 -0700 (PDT)
From: David Koopman <zzzzzzz@yahoo.com>
Subject: From Yahoo
To: dave@zzzzzzz.com
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="0-233061671-1052064996=:54937"
--0-233061671-1052064996=:54937
Content-Type: text/plain; charset=us-ascii
This message is a test from Yahoo
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
--0-233061671-1052064996=:54937
Content-Type: text/html; charset=us-ascii
<DIV><STRONG>This message is a test from Yahoo</STRONG></DIV><p><hr SIZE=1>
Do you Yahoo!?<br>
<a href="http://us.rd.yahoo.com/search/mailsig/*[url]http://search.yahoo.com[/url]">The New Yahoo! Search</a> - Faster. Easier. Bingo.
--0-233061671-1052064996=:54937--
<?php
/*
* This is a simple email viewer.
* make sure that $filename points to a file containing an email message and
* load this page in your browser.
* You will be able to choose a part to view.
* */
dl("mailparse.so");
#$filename = "/home/CLIENTWEB/worlddo/mimetests/namib.rfc822";
$filename = "email2";
#$filename = "/home/CLIENTWEB/worlddo/mimetests/segblob.txt";
#$filename = "yourmessage.txt";
/* parse the message and return a mime message resource */
$mime = mailparse_msg_parse_file($filename);
/* return an array of message parts - this contsists of the names of the parts
* only */
$struct = mailparse_msg_get_structure($mime);
/* print all sections */
foreach($struct as $st) {
echo "------------\n";
echo "Part-num: $st\n";
/* get a handle on the message resource for a subsection */
$section = mailparse_msg_get_part($mime, $st);
/* get content-type, encoding and header information for that section */
$info = mailparse_msg_get_part_data($section);
/* dump data contained in the $info object */
print_r($info);
echo "\n";
/* print the data in this part */
$sec = mailparse_msg_get_part($mime, $st);
ob_start();
/* extract the part from the message file and dump it to the output buffer */
mailparse_msg_extract_part_file($sec, $filename);
$contents = ob_get_contents();
ob_end_clean();
/* print the contents of this part: */
echo "\n".$contents;
}
echo "\n\n";
?>
The above code, using the above email as the $filename, will produce these results:
Part-num: 1
Array
(
[headers] => Array
(
[message-id] => <20030504161636.55348.qmail@web21208.mail.yahoo.com>
[date] => Sun, 4 May 2003 09:16:36 -0700 (PDT)
[from] => David Koopman <zzzzzzz@yahoo.com>
[subject] => From Yahoo
[to] => dave@zzzzzzz.com
[mime-version] => 1.0
[content-type] => multipart/alternative; boundary="0-233061671-1052064996=:54937"
)
[starting-pos] => 0
[starting-pos-body] => 286
[ending-pos] => 821
[ending-pos-body] => 821
[line-count] => 23
[body-line-count] => 15
[charset] => us-ascii
[transfer-encoding] => 8bit
[content-boundary] => 0-233061671-1052064996=:54937
[content-type] => multipart/alternative
[content-base] => /
)
--0-233061671-1052064996=:54937
Content-Type: text/plain; charset=us-ascii
This message is a test from Yahoo
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
--0-233061671-1052064996=:54937
Content-Type: text/html; charset=us-ascii
<DIV><STRONG>This message is a test from Yahoo</STRONG></DIV><p><hr SIZE=1>
Do you Yahoo!?<br>
<a href="http://us.rd.yahoo.com/search/mailsig/*[url]http://search.yahoo.com[/url]">The New Yahoo! Search</a> - Faster. Easier. Bingo.
--0-233061671-1052064996=:54937--
Part-num: 1.1
Array
(
[headers] => Array
(
[content-type] => text/plain; charset=us-ascii
)
[starting-pos] => 318
[starting-pos-body] => 362
[ending-pos] => 492
[ending-pos-body] => 492
[line-count] => 6
[body-line-count] => 4
[charset] => us-ascii
[transfer-encoding] => 8bit
[content-charset] => us-ascii
[content-type] => text/plain
[content-base] => /
)
This message is a test from Yahoo
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.------------
Part-num: 1.2
Array
(
[headers] => Array
(
[content-type] => text/html; charset=us-ascii
)
[starting-pos] => 525
[starting-pos-body] => 568
[ending-pos] => 786
[ending-pos-body] => 786
[line-count] => 4
[body-line-count] => 2
[charset] => us-ascii
[transfer-encoding] => 8bit
[content-charset] => us-ascii
[content-type] => text/html
[content-base] => /
)
<DIV><STRONG>This message is a test from Yahoo</STRONG></DIV><p><hr SIZE=1>
Do you Yahoo!?<br>
<a href="http://us.rd.yahoo.com/search/mailsig/*[url]http://search.yahoo.com[/url]">The New Yahoo! Search</a> - Faster. Easier. Bingo.