Actually its a email coming to me in different formats and I have piped that email to my script which then filters the required data and adds it to database. Several people are sending me emails in several different formats and hence I am creating different filters for different formats. I will post my code:
<?php
if (isset($_POST["btnTest"]))
{
//$fields = array ("Your Name:", "Your Address:", "Telephone:", "Telephone:", "Email:", "Friend's Name:", "Friend's Address:", "Post Code:", chr(13), chr(13));
$fields = array("TITLE:", "NAME:", "EMAIL:", "DESIRED_LOCATION:", "CAPITAL_TO_INVEST:", "TELEPHONE_NUMBER:", "ALT_TELEPHONE_NUMBER:", "BEST_TIME_TO_CALL:", "PREFERRED_METHOD_OF_CONTACT:", "ADDRESS_LINE_1:", "ADDRESS_LINE_2:", "ADDRESS_CITY:", "ADDRESS_REGION:", "ADDRESS_POSTCODE:", "ADDRESS_COUNTRY:", "WHEN_WANTS_TO_START_NEW_BUSINESS:", "COMMENTS:", "SOURCE:", "DATE:", Chr(13), Chr(13));
if(!empty($_FILES["filemail"][name]))
{
$email = file_get_contents($_FILES["filemail"][tmp_name]);
}
else { $error[] = "Invalid E-Mail Message File"; }
if (empty($error))
{
for ($i = 0; $i <= count($fields) - 1; $i++)
{
$find_pos = strpos($email, $fields[$i], ($find_pos + strlen($fields[$i])));
if (empty($find_pos)) { $pos[] = "0"; } else { $pos[] = $find_pos; }
}
$find = array (chr(13), "<br />", "<br>", "<br/>");
$rplc = array ("", "", "", "");
for ($i = 0; $i <= count($pos); $i++)
{
$data[] = str_replace ($find, $rplc, substr ($email, $pos[$i], $pos[$i + 1] - $pos[$i]));
}
for ($i = 0; $i <= count($data) - 1; $i++)
{
$exp2 = explode (": ", $data[$i]);
if (trim($exp2[0]) <> "") { echo "Field: " . str_replace ($find, $rplc, strtolower($exp2[0])) . " - " . "Value: " . $exp2[1] . "<br>\
"; }
}
}
}
?>
<h3 align="center">E-Mail Filter System</h3>
<?php if (!empty($error)) { require_once ("error.php"); } ?>
<form method="post" enctype="multipart/form-data">
<table border="0" cellpadding="5" cellspacing="0" align="center">
<tr>
<td align="right">E-Mail:</td>
<td><input type="file" name="filemail" /></td>
</tr>
<tr>
<td align="right">Debug Mode:</td>
<td>
<input type="radio" value="0" checked name="debug" /> No
<input type="radio" value="1" name="debug" /> Yes
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btnTest" value="Test" /></td>
</tr>
</table>
</form>
UPDATE: I have modified the code a bit, it now gets the address fine, but the last comments are still an issue…
What I have done now is I have specified the list of fields that I need which it finds the positions of then using those positions I extract the data and process the thing. Seems to be fine but I still think its not achieving the goal.
Please check.
Thanks.