简体   繁体   中英

How to remove email headers from message body

i am working on an email parsing script and is having some issues. when the emails is being forwarded it inserts the email header into the body of the message such as From, To, Sent, Subject. i am trying to strip it out but it is not stripping. i tried a few methods but no luck. any suggestions?

//php

$message = str_replace("/^From: (.*)/", "", $message);
$message = str_replace("/^To: (.*)/", "", $message);
$message = str_replace("/^Subject: (.*)/", "", $message);

i have also tried $message = str_replace("From: $from", "", $message); //$from get from header.

I'm not sure if you are still having an issue with this, but you should use the preg_replace function (or preg_replace_all if you are trying to locate multiple occurrences). Your code should look like this:

    preg_replace("/^From: (.*)/", "", $message, $from);
    preg_replace("/^To: (.*)/", "", $message, $to);
    preg_replace("/^Subject: (.*)/", "", $message, $subject);

When you are trying to output the variable use the print(r) function:

    print_r($from, true);
    print_r($to, true);
    print_r($subject, true);

It will probably come out as a multi-dimensional array, and you can echo the response from the array.

    echo $from[1][0]; // It may be in a different array location.       
    echo $to[1][0]; // It may be in a different array location.       
    echo $subject[1][0]; // It may be in a different array location.       

You may need to adjust the array locations ([1][0]) to output the correct data. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM