happiness's avatar

Insert new line in mail message of notification

I am using a variable, $messagetext, to save the text to be send in a notification. The notification is send through email, but it also can be displayed in the ui of the web app. It would be convenient for me to insert a new line character in the text instead since I use a single $this->message->line($messagetext) command to construct the message, instead of creating multiple strings and then iterating to add them in separate ->line() arguments.

My $messagetext is: $messagetext = "text"

I have tried "text \n text ", "text text ", "text nl2br(\n) text " and other variations of them with single, double quotes, attach dots, etc Am I missing something?

0 likes
8 replies
gych's avatar

Maybe you can add some logic yourself to handle and split the line breaks to two separate strings. I've never used this approach myself before but I think it could work.

happiness's avatar

@Snapey thank you. That doesn't work either. Also tried it with various brackets and quotes. I can't seem to be able to insert html code in the string that will be translated as html.

Snapey's avatar

@happiness your string could use markdown for the email which uses four spaces and a newline

$message = "First line of text. \nSecond line"

note the use of double quotes

in the browser

{!! nl2br(e($message)) !!}

You could also use this expression in markdown which respects <br>

im-nazmul's avatar

Can you try the PHP_EOL constant or the PHP_EOL environment variable instead of \n? Then your message should be like this:

$messagetext = "line 1" . PHP_EOL . "line 2";

Make sure that you're using {!! !!} to allow HTML rendering, as {!! !!} does not escape HTML entities.

Snapey's avatar

@im-nazmul php_eol is not going to generate any html

puklipo's avatar

All new lines will be removed. However, if passed as an array, they will not be removed.

$text = 'text with new line';

return (new MailMessage)
        ->line([$text]);

source: Illuminate/Notifications/Messages/SimpleMessage.php formatLine()

Snapey's avatar

@puklipo how does this work when displaying the notification in the browser?

Please or to participate in this conversation.