Stripping the message out and removing any HTML
I have a script that reads in emails via php. I can remove most of the headers but am still finding that there are about 10% remaining. I want to remove all headers that are before the part that says:
Content-Type-Encoding: 7 bit
I also want to remove any headers for html emails, strip the html and any other codes out and then return the email in plain text.
Here is the script that I have below. If someone could recommend a solution it would be greatly appreciated.
Cheers!
Jay
#!/ramdisk/bin/php5 -q
<?php
// CUSTOMIZE ALL BELOW
$youremail="jason@bitsonline.us"; // Put your email address here.
$aremail="bakeritservices@listwire.com"; // Put your autoresponders email address here.
$subj="Stay tuned for..."; // Put your subject here that you want the person to get as soon as their email is received by the script.
$msg="more information on the workshop! --- I am working to secure a place to do this two day workshop. I will have things set here in about a week or two. ~Jason Baker"; // Put your message here that you want them to get as soon as the email is received by the script.
$yourname="Jason Baker"; // Put your name or business name here for the people to see who the email is from.
// END CUSTOMIZE AREA
// REMEMBER to change permissions of this script to 755 instead of 644 as you dont want browsers to trigger this script.
//------------------------------------DO NOT CUSTOMIZE ANYTHING IN THIS SECTION!----------------------------------------------------
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
$email .= fgets($fd, 1024);
}
fclose($fd);
$splittingheaders = true;
$lines = explode("\n",$email);
$parts = preg_split('/\n/',$email);
$element = "header";
foreach($parts as $key=>$val) {
// once we find an empty line, we'll switch to the body
if(preg_match("/[^\"\"](.*)[^\"\"]/", $val)==0){$element = "body";}
$sections[$element] .= $val;
$message=$sections[body];
//if (preg_match('/^[\s\t\n]$/',$val)) { $element = "body"; }
//here we compact into it's array section
//$sections[$element] .= $val;
//}
}
for($i = 0; $i < count($lines); $i++){
if ($splittingheaders) {
$headers .= $lines[$i]."\n";
if (preg_match("/^Subject: ([A-Za-z0-9]+(.*))/", $lines[$i], $matches)) {
$subject = substr($matches[0],9);
}
if(preg_match("/^From: (.*)/", $lines[$i], $matches)){
//if (preg_match("/^From: \._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $exploded[$i], $matches)) {
$from = substr($matches[0],6);
}
}
}
mail($youremail,$subject,strip_tags($message),"From: $from");
//mail($aremail,$subject,$message,"From: $from");
//mail($from,$subj,$msg,"From: $yourname <$youremail>");
//-------------------------------END EMAIL SEBDUBG SNIPPET-------------------------------
?>
Sam Barrow
sambarrow.com
Do you want to make successes or excuses? Success? Alright then... See what's in store for you....
- The AC Assassin