I'm developing a script that handles bounced emails from a html sending application. It is all working except when I use ereg to pull the email address out of the body of the email it is pulling extra characters. The extra characters were originally html tags so I used strip tags again and then it was pulling in trailing and leading characters from other words. Any help is greatly appreciated. Code included below.
<?
//script to connect to pop3 mailbox and retrieve bounced emails, find returned email address
//and do some database queries with that email address
$server = "mattwaters.dnsalias.net";
$user = "matt";
$pass = "7777777";
$conn = @imap_open("{$server/pop3}INBOX", $user, $pass)
or die("Connection to server failed");
$headers = @imap_headers($conn)
or die("Couldn't get emails");
$numEmails = sizeof($headers);
echo "You have $numEmails in your mailbox";
//individual emails
for($i = 1; $i < $numEmails+1; $i++)
{
$mailHeader = @imap_headerinfo($conn, $i);
$from = $mailHeader->fromaddress;
$subject = strip_tags($mailHeader->subject);
$date = $mailHeader->date;
$body = nl2br(strip_tags(imap_body($conn, $i)));
echo "<br><br>Email from $from, <br>subject $subject, <br>date $date<br>";
echo $body;
echo "<br>";
ereg('. (.@[^ ]* )',$body,$m);
//$m[1] should contain email address,
//this is working but it is bringing in extra characters beyond the email address
echo "email="; echo $m[1];
}
imap_close($conn);
?>