8

How can I add break lines?

I'm using this:

$mailbody=$username."\r\n";
$mailbody.=$email."\r\n";

$altbody=$username."\r\n";
$altbody.=$email."\r\n";

But break lines are not there.

I'm using $altbody because for some reason the software I use to receive the emails does not read HTML, only plain text, so, $altbody is the body when the email reader does not support HTML, so, I can't use BR neither...

Any ideas?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Thomas Hardy
  • 81
  • 1
  • 1
  • 3

7 Answers7

11

normally linebreaks with only \n should work in plain text emails.
but stick to the <br/>s in your html-mail $mailbody.

Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123
  • 2
    I can't use
    in a non-html mailer and for some reason it removes the \r and \n
    – Thomas Hardy Mar 05 '12 at 07:53
  • 1
    Late to the party. I had the same problem. The \n's were stripped because there was a msgHTML() after setting the AltBody, msgHTML recreates the AltBody. Fixed by first calling msgHTML() and afterwards setting the AltBody. – Jan Ehrhardt Jul 23 '20 at 23:34
0

Do you have code earlier on that formats the message body? You may inadvertently be stripping out all html tags and all \ (e.g. if you use stripslashes).

GK79
  • 241
  • 1
  • 4
  • 17
0

I had a similar issue when I sent an email with text from an HTML textarea. Solved it by using php nl2br.

Vajira Lasantha
  • 2,435
  • 3
  • 23
  • 39
0

With PHPMailer you need to use the entire include entire <html> block. Try this:

$msg = "<html><body>
Type your messsage here.<br><br>
Sincerely,<br>
<br>
Contributor
</body></html>";
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
0

You can try heredoc like as

$altbody = <<<MAIL
Hello $username,

Welcome to example.com!
MAIL;

or

$altbody = <<<MAIL
Hello $username,\nWelcome to example.com!
MAIL;
Mert S. Kaplan
  • 1,045
  • 11
  • 16
0
$text  = $sender_name."<br>";
$text .=$reply_to_email."<br>";
$text .= $Mobile_No."<br>";
$text .= $message."<br>";
Chris Walsh
  • 3,423
  • 2
  • 42
  • 62
0

This does not directly answer the question asked, but it does answer the question that led me to this post (and i cant leave a comment yet):

I used single quotes, but PHP interprets single and double quotes in a different manner.

Try using double quotes for the AltBody like "\n" and not '\n'.

skalta
  • 1,033
  • 1
  • 7
  • 18