- if ($mText != $fieldtext) {
- $trans = array('_' => '=5F', ' ' => '_', '?' => '=3F');
- $mText = strtr($mText, $trans);
+ $is_qpformat = ($mText != $fieldtext);
+ if ($stringescape && preg_match('/[^\w !#$%&\'*+\/=?^`{|}~-]/', $mText)) {
+ // if needed, make this a quoted-string instead of an atom (to speak in RFC2822 language)
+ $mText = '"'.strtr($mText, array('"' => '\"', '\\' => '\\\\')).'"';
+ }
+ if ($is_qpformat) {
+ $mText = strtr($mText, array('_' => '=5F', ' ' => '_', '?' => '=3F'));