Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wp_mail line breaks in textarea for html email

I am having an issue using the wp_mail function, and hoping someone can help me.

I need to insert line breaks in the email that user added to the text area 'message.'

Can anyone please help?

I currently have the follow code sending an email from a contact form:

<?php

if( !isset($_REQUEST) ) return;

require('../../../../wp-load.php');
function wpse27856_set_content_type(){
return "text/html";
}
add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );

$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$msg = $_REQUEST['message'];


$headers = 'From: '.$name.' <'.$email.'>' . "\r\n";
$message = '
<html>
<body>
Someone has made an enquiry on the online contact form:

<br /><br />
        <b>Contact Details:</b><br />
        '.$name.'<br />
        '.$phone.'<br />
        '.$email.'<br /><br />
        <b>Message:</b><br />
        '.$msg.'<br />

        <br /><br />


</body>
</html>
            ';

wp_mail('[email protected]', 'Contact form Message' , $message, $headers);


?>
like image 576
James Dickinson Avatar asked Dec 26 '22 10:12

James Dickinson


2 Answers

By default, wp_mail() sends messages as plain text, so the HTML is not parsed by email clients.

Include the following headers with your email:

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

Since your email is in HTML, it's always good practice to make it valid with the proper HTML boilerplate (HTML, HEAD, BODY...)

Alternatively, you can replace your
tags by carriage returns (\r\n), although you'll still need to get rid of tags.

This is already covered by the PHP documentation for mail(), around which wp_mail() wraps.

http://php.net/manual/en/function.mail.php

like image 184
nicbou Avatar answered Jan 07 '23 18:01

nicbou


You can add header to your mail if your textarea contains html

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
like image 23
v0d1ch Avatar answered Jan 07 '23 16:01

v0d1ch