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);
?>
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
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With