Search code examples
phpencodingmime

Replacing characters in MIME encoded emails


I am looking for a way to simply replace characters with their ASCII counterparts in MIME encoded emails. I've written preliminary code below, but it seems like the str_replace commands I'm using will keep on going forever to catch all possible combinations. Is there a more efficient way to do this?

<?php
    $strings = "=?utf-8?Q?UK=20Defence=20=2D=20Yes=2C=20Both=20Labour=20and=20Tory=20Need=20To=20Be=20Very=20Much=20Clearer=20On=20Defence?=";

    function decodeString($input){
        $space = array("=?utf-8?Q?","=?UTF-8?Q?", "=20","?=");
        $hyphen = array("=E2=80=93","=2D");
        $dotdotdot = "=E2=80=A6";
        $pound = "=C2=A3";
        $comma = "=2C";
        $decode = str_replace($space, ' ', $input);
        $decode = str_replace($hyphen, '-', $decode);
        $decode = str_replace($pound, '£', $decode);
        $decode = str_replace($comma, ',', $decode);
        $decode = str_replace($dotdotdot, '...', $decode);
        return $decode;
    }

    echo decodeString($strings);
?>

Solution

  • I figured it out - I have to pass $strings to the mb_decode_mimeheader() function.