Strange Characters in Outlook Mail Gravity Forms

GravityWPGravityForms0 Comments

It can be that your e-mail looks different in Gmail than in Outlook: something went wrong with encoding your text. Characters like ë or é can look like ‘é’ or ë. This is a quick fix to try, but better is to find out what’s really going wrong.

Add this code to your functions.php

// Merge tag modifier for mail bug special characters in outlook
add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifier, $field, $raw_value ) {
    if ( $merge_tag != 'all_fields' && $modifier == 'gwp_mail_outlook' ) {
        /* Only do the slow convert if there are 8-bit characters */
        if ( !preg_match("/[\200-\237]/", $value) && !preg_match("/[\241-\377]/", $value) )
               return $value;

        // decode three byte unicode characters
          $value = preg_replace_callback("/([\340-\357])([\200-\277])([\200-\277])/",
                    create_function ('$matches', 'return \'&#\'.((ord($matches[1])-224)*4096+(ord($matches[2])-128)*64+(ord($matches[3])-128)).\';\';'),

        // decode two byte unicode characters
          $value = preg_replace_callback("/([\300-\337])([\200-\277])/",
                    create_function ('$matches', 'return \'&#\'.((ord($matches[1])-192)*64+(ord($matches[2])-128)).\';\';'),

}     return $value;
}, 10, 5 );

How to use it in your Gravity Forms mail?

After adding the above code in your functions.php file (in your theme directory, preferable your child-theme, so it will not be overwritten), you can use the gwp_outlookmailtext modifier inside the mergetags you’re using.

For example:

{First field:1:gwp_mail_outlook}

After it, these merge-tags will show the appropriate characters in both Gmail, Hotmail as Outlook. Don’t use this modifier (gwp_mail_outlook) in the subject line of the e-mail, because this already shows correctly in Outlook.

Share this Post