Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress mail header set else plain text

Hope to get some help with a piece of code, I am using a theme for wordpress which sets the mail headers to text/html, this causes some problems with plain text mail ex. linebreaks don't show anymore.

I tried setting :

} else {
    return 'text/plain';
}

but I don't know php very well so I don't know where to place it to make it work. I would like to set the text/plain for mails not defined.

this is the code for the wp header :

 /**
 * filter mail headers
 */
function wp_mail($compact) {
    if (isset($_GET['action']) && $_GET['action'] == 'lostpassword') return $compact;
    if ($compact['headers'] == '') {

        //$compact['headers']   = 'MIME-Version: 1.0' . "\r\n";
        $compact['headers'] = 'Content-type: text/html; charset=utf-8' . "\r\n";
        $compact['headers'].= "From: " . get_option('blogname') . " < " . get_option('admin_email') . "> \r\n";
    }

    $compact['message'] = str_ireplace('[site_url]', home_url() , $compact['message']);
    $compact['message'] = str_ireplace('[blogname]', get_bloginfo('name') , $compact['message']);
    $compact['message'] = str_ireplace('[admin_email]', get_option('admin_email') , $compact['message']);

    $compact['message'] = html_entity_decode($compact['message'], ENT_QUOTES, 'UTF-8');
    $compact['subject'] = html_entity_decode($compact['subject'], ENT_QUOTES, 'UTF-8');

    //$compact['message']       =   et_get_mail_header().$compact['message'].et_get_mail_footer();

    return $compact;
}
like image 740
Elcangri Avatar asked Apr 19 '15 07:04

Elcangri


1 Answers

Instead of changing that, change your plain line breaks to html.

$message=nl2br($message); // of course use your var name.

That way you get to keep a standard format for email as well. plain text has nothing so special to need a separate header in this case. This function will convert all line breaks to html version.

Other than new lines most of your plain text will hold its formatting even in html because it has no special tags.

Here is how you will place it

function wp_mail($compact) {
    // leave your existing code intact here, don't remove it.
    $compact["message"]=nl2br($compact["message"]); 
    return $compact;
}
like image 70
Hanky Panky Avatar answered Nov 06 '22 16:11

Hanky Panky