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

15

27

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.

Bignoniaceous answered 18/8, 2010 at 3:16 Comment(2)
'\r\n' or should it be "\r\n"?Garey
"\r\n" printing __Emission
E
32

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

Encyclopedic answered 19/9, 2012 at 8:0 Comment(0)
A
27

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

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

Algia answered 18/8, 2010 at 3:16 Comment(2)
made sure code had "\r\n" and not '\r\n' as I had in my example. Still no line. Could be my phone.Bignoniaceous
Working fine in samsung galaxy grandHoulihan
M
5

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

Meson answered 4/2, 2018 at 17:42 Comment(2)
worked nice. I think we should use %0a when we send message text using get methodNaxos
"%0a" Worked Foe Me !Plumbaginaceous
A
3

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
Actualize answered 4/8, 2017 at 11:53 Comment(0)
P
2

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

Plumbism answered 9/1, 2013 at 10:41 Comment(0)
D
2

Had same problem, this works for me.

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

Dittman answered 19/7, 2019 at 13:3 Comment(0)
P
2

This worked for me in php,

$message = "Welcome,
some text
Thank you for choosing us.";
Physiognomy answered 11/3, 2020 at 7:27 Comment(1)
Start with this answer. It's the most simple solution to the question. (move to others if this fails for some reason)Ali
R
1

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.

Rattigan answered 20/10, 2014 at 2:48 Comment(0)
C
0

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.

Celka answered 18/8, 2010 at 3:18 Comment(3)
Yes, I've tried the <br> and <br/> (<br> disapears but doesn't add a line break and <br/> shows up as > in my message)Bignoniaceous
@Matt: Try a plain-text email, then.Celka
I did but still did not get the new line.Bignoniaceous
B
0

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.

Bignoniaceous answered 18/8, 2010 at 15:7 Comment(0)
T
0

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

Tarpley answered 20/10, 2014 at 2:52 Comment(0)
I
0

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;
Infanticide answered 28/4, 2017 at 22:41 Comment(0)
C
0

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);
Consult answered 17/1, 2024 at 14:43 Comment(0)
C
-1

You can use PHP_EOL for ending a line.

Cliffhanger answered 28/9, 2013 at 14:50 Comment(0)
Z
-1

Use %0a , I think it will work

Zygoma answered 21/6, 2016 at 11:29 Comment(3)
Give proper answer.Mickiemickle
This is exactly the same as the top voted answer. What extra value does this answer provide?Hathcock
@Hathcock you right but it seems this is newer than the same answer and maybe copied it.Undertenant

© 2022 - 2025 — McMap. All rights reserved.