Search code examples
email-parsing

Working with PHP mime mail parser


I'm using this library to parse my incoming emails: http://code.google.com/p/php-mime-mail-parser/

I have Mailparse extension installed and everything is fine, but when I do:

echo $from = $Parser->getHeader('from');

It echos out the name of the email sender, for example: John Smith, but I need the email address of the email sender, for example [email protected].

This also happens for:

echo $to = $Parser->getHeader('to');

and I can't seem to find any solution to get these, any helps here? Thanks in adnva


Solution

  • You need to apply htmlspecialchars() function on to/from:

    // You need to apply htmlspecialchars() function on to/from:
    $from = htmlspecialchars($Parser->getHeader('from'));
    $to = htmlspecialchars($Parser->getHeader('to'));
    
    // the above code will give you something like:
    // John Smith <[email protected]>
    // so to get the email address we need to use explode() function:
    
    function get_email_address($input){
            $input = explode('&lt;', $input);
            $output = str_replace('&gt;', '', $input);
            $name = $output[0]; // THE NAME
            $email = $output[1]; // THE EMAIL ADDRESS
            return $email;
    }
    
    $from = htmlspecialchars($Parser->getHeader('from'));
    echo $from = get_email_address($from); // NOW THIS IS THE EMAIL
    
    $to = htmlspecialchars($Parser->getHeader('to'));
    echo $from = get_email_address($to) // NOW THIS IS THE EMAIL