Stripping the message out and removing any HTML

2 replies
Hey all,

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

PHP Code:
#!/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($fd1024);
}
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------------------------------- 
?>
#html #message #removing #stripping
  • Profile picture of the author sbarrow
    For the tags why don't you just use the strip_tags() function?
    Signature

    Sam Barrow
    sambarrow.com

    {{ DiscussionBoard.errors[2313006].message }}
  • Profile picture of the author jaybaker
    I have used strip tags... I finally figured instead of just one forward I would do two as well. 1 to the pipe script and the second to my primary email address.
    Signature

    Do you want to make successes or excuses? Success? Alright then... See what's in store for you....
    - The AC Assassin

    {{ DiscussionBoard.errors[2324857].message }}

Trending Topics