I am writing a PHP script to get email piped to the script and save it to the database.. I have succeeded in get coming emails.. But i am still struggling with how to get rid of raw contents that comes with the email.. What technique can i use to get only the message either the text version or the html version This is the example of email i receive currently
Content preview: Mine Mine [...]
Content analysis details: (-0.1 points, 5.0 required)
pts rule name description
---- ---------------------- --------------------------------------------------
-0.0 SPF_PASS SPF: sender matches SPF record
0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
(techservice4real[at]gmail.com)
0.0 HTML_MESSAGE BODY: HTML included in message
-0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's
domain
0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid
-0.1 DKIM_VALID Message has at least one valid DKIM or DK signature
-0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2)
[209.85.222.182 listed in wl.mailspike.net]
X-Spam-Flag: NO
--000000000000c1a52105822cd389
Content-Type: text/plain; charset='UTF-8'
Mine
--000000000000c1a52105822cd389
Content-Type: text/html; charset='UTF-8'
<div dir='ltr'>Mine<br></div>
--000000000000c1a52105822cd389--
This is currently the code i am using to get the email
#!/usr/local/bin/php -q
<?php
require_once('inc/init.php');
//chdir(dirname(__FILE__));
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
// if(strlen($email)<1) {
// die();
// }
// handle email
$lines = explode("
", $email);
// empty vars
$from = "";
$to="";
$subject = "";
$headers = "";
$message = "";
$bodyContent = "";
$myBody = "";
$splittingheaders = true;
for ($i=0; $i < count($lines); $i++) {
if ($splittingheaders) {
// this is a header
$headers .= $lines[$i]."
";
// 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];
}
if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
$to = $matches[1];
}
} else {
// not a header, but message
$message .= $lines[$i]."
";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$splittingheaders = false;
}
}
$data = explode("
", $message);
mail("boomztechs@gmail.com", "You Have One new Email Piped", "A New Mail Message just Piped to you) ".$message);
Your response is highly appreciated thanks..
</div>