Matt
Matt

Reputation: 271

Trying to get new line on sms message sent from php script

I've been trying to get a new line generated in my SMS message sent from a PHP script. I've used \r\n, <BR> and some hex codes. No matter what I do the message comes to my phone without line breaks.

$body .= 'City:'.$venue.'\r\n'; //<- doesn't work
$body .= 'State:'.$state.'<br>'; //<- doesn't work

This is my header type...(complete header not included)

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

I use mail to send...

mail($somenumber,$subject,$body,$headers)

Everything works fine in the sense that I receive the message. I hope that I'm missing something, because this is driving me crazy.

Upvotes: 27

Views: 63701

Answers (15)

dustbunny
dustbunny

Reputation: 43

I've been trying to figure out how to fix this and if you are using phpmailer, setting the isHtml(false) fixed my issue and made my <br become a line break on my innerText.

$mail->isHTML(false);

Upvotes: 0

Arati
Arati

Reputation: 109

This worked for me in php,

$message = "Welcome,
some text
Thank you for choosing us.";

Upvotes: 2

Javed
Javed

Reputation: 437

Had same problem, this works for me.

$text .= chr(10) . 'hello world'; But all other answer didn't when i tested.

Upvotes: 2

Mahmoud Saleh
Mahmoud Saleh

Reputation: 33605

for me sometimes %0a works and sometimes \n works depends on the SMS gateway

Upvotes: 6

waqas
waqas

Reputation: 91

Try "\n" instead of '\n';

Because in single quotes it take the character as it is.

Example:

echo nl2br('one \n two');//print: one \n two
echo nl2br("one \n two");//print: one <br> two

Upvotes: 3

Nicolas Giszpenc
Nicolas Giszpenc

Reputation: 703

Let the ascii do the work for you. ASCII character 10 is the carriage return. This worked for me on Android.

$body =  'City:' . $city;
$body .= chr(10) . 'State:' . $state;
$body .= chr(10) . 'Zip:' . $zip;

Upvotes: 0

Arun Raj R
Arun Raj R

Reputation: 11

Use %0a , I think it will work

Upvotes: -1

user2034047
user2034047

Reputation: 23

Use double quotes, single quotes don't recognize new lines

Upvotes: 0

Roger Gajraj
Roger Gajraj

Reputation: 533

You have to understand how that message is encoded to be sent. For my situation, using routosms api, I had to disable their api's use of urlencode(using php) on my message string. Then, %0A worked.

Upvotes: 1

Roshan Pal
Roshan Pal

Reputation: 7796

You can use PHP_EOL for ending a line.

Upvotes: -1

user1960994
user1960994

Reputation: 21

Use \r\n and then encode it using urlencode(). It worked on Android

Upvotes: 2

Nasser Al-Wohaibi
Nasser Al-Wohaibi

Reputation: 4661

Use: %0a, worked for me in Nokia and Android

Upvotes: 32

Matt
Matt

Reputation: 1

Just a little bit of code helps the medicine go down. I forgot an important little piece of code:

Content-Transfer-Encoding: 7bit

You need the code above added to your header. HTH.

Upvotes: 0

anonymous
anonymous

Reputation: 44

'\n' will print two characters: \ and n

"\n" will print a line feed character (0x0A)

Upvotes: 27

Borealid
Borealid

Reputation: 98469

You are setting the content type as text/html. Try sending a <br/>. HTML is whitespace-agnostic and uses the break tag to force a new line.

If you don't want the message to be HTML, don't mark it as such.

Upvotes: 0

Related Questions