Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP new line break in emails

Tags:

php

newline

I have the following code:

$message = "Good news! The item# $item_number on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\n The seller, $bid_user is making this offer.\n \nItem Title : $title\n  \nAll the best,\n $bid_user\n $email\n ";  echo $message; $htmlContent = $baseClass->email_friend($item_number, $title, $bid_price, $bid_user, $mcat, $message, $email, $VIEWSTATE, $EVENTVALIDATION, $URL); 

The problem is that the new line break (\n) is not working.

like image 669
Michael Avatar asked Jul 08 '10 17:07

Michael


People also ask

How do you insert a new line in an email?

In your Send Outlook Mail Message activity you have selected the IsBodyHtml in the property as True, so you can use "<br />" tag where you need NewLine. This is a html tag for a new line.

How do I add a new line to a string in PHP?

The nl2br() function inserts HTML line breaks (<br> or <br />) in front of each newline (\n) in a string.

How do you insert a line break in plain text email?

You can insert a break into plaintext emails using {! BR()} .

How do I add a line in HTML email?

In the most basic way you can Just press Ctrl + Enter when typing out the email, e.g. If you are needing something more complex, then you can Generate the email using a hidden HtmlText Box. If you are comfortable with HTML. You can then add a <br/> tag to allow a new line.


2 Answers

Try \r\n in place of \n

The difference between \n and \r\n

It should be noted that this is applicable to line returns in emails. For other scenarios, please refer to rokjarc's answer.

like image 69
Brendan Bullen Avatar answered Sep 20 '22 23:09

Brendan Bullen


I know this is an old question but anyway it might help someone.

I tend to use PHP_EOL for this purposes (due to cross-platform compatibility).

echo "line 1".PHP_EOL."line 2".PHP_EOL; 

If you're planning to show the result in a browser then you have to use "<br>".

EDIT: since your exact question is about emails, things are a bit different. For pure text emails see Brendan Bullen's accepted answer. For HTML emails you simply use HTML formatting.

like image 42
Rok Jarc Avatar answered Sep 21 '22 23:09

Rok Jarc