Ben Ben - 3 months ago 7
PHP Question

How to send text with linebreaks coming from user input with PHPMailer

I need to send text from users typed in a html form (

textarea
) along with emails. I use PHPMailer with HTML setup. This works perfect also because I need to embedd logos. The only problem is that email programs which receive the mail ignores line breaks from this text which. I parse the text like this :

$mail->isHTML(true);
$mail->Body = $_POST['mail_Text'];


What do I need to do ?

Answer

nl2br() should work for you.

So, add mail body like this.

$mail->Body =  nl2br($_POST['mail_Text']);

nl2br() returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).