'Add bold text and line break into Laravel Notification Email

Laravel 5.6

I'm attempting to send a Laravel Notification via email. I want to make make some of the text bold, and put in line breaks, without a whole new paragraph that the line($text) method brings. So I have tried this in the notification class. I have also tried using the \n string for new line.

return (new MailMessage)
->subject('Booking Confirmed - Please Read')
->greeting('Hello ' . $this->booking->user->first_name . ',')
->line('Thank you for booking. Here are your booking details:')
->line($this->booking->provider->providerType->name . '<br>' .
    $date . '<br>' .
    $this->booking->start_at . '<br>' .
    $this->booking->address_1 . '<br>' .
    $this->booking->address_2 . '<br>' .
    $this->booking->address_3 . '<br>' .
    $this->booking->city . '<br>' .
    $this->booking->postcode . '<br>' .
    '£' . $this->booking->price
)
->line('<strong>Need to cancel?</strong><br>' .
    'Don\'t forget to contact the provider on the details above if you need to cancel or reschedule. 
    They won\'t mind, as long as you tell them.'
)
->line('<strong>Payment</strong><br>' .
    'Pay the Service provider direct as you normally would. This keeps things simple and costs down.'
)
->line('<strong>FAQ\'s</strong><br>' .
    'Please click here for more information'
)
->line('<strong>Don\'t forget to leave feedback after!</strong><br>' .
    'Help build your relationship with your Service Providers by leaving feedback'
)
->line('We hope to see you again soon!')

I have tried with and without publishing the blade templates via the php artisan vendor:publish --tag=laravel-mail command and then updating {{$line}} to {!! $line !!}} with no joy. Can't figure it out.

It prints put like this in mailtrap enter image description here



Solution 1:[1]

I figured this out, incase anyone else out there is as dumb as me.
I think there are 2 reasons why this was broken. @DouwedeHaan suggested I use double quotes instead of single when using \n which didn't do much but combined with the next part I think did the trick.

The blade template that renders the html is in markdown. I hadn't figured this out. The layout of it is specific, and I had accidentally broken it after I published it by removing some lines and formatting the file with indents which broke everything.

I deleted the file, re-published the template updated all instanced of {{$line}} to {!! $line !!} ensuring to leave the rest of the file as is, updated my notification to use double quotes and stuck with <br/> and <strong></strong> tags and now it works as expected.

enter image description here

Solution 2:[2]

Perhaps you have done your job as the question is so old. But I would like to share an easy way to write an HTML tag on the Laravel mail template.

First of all, you are required to import use Illuminate\Support\HtmlString;

->line(new HtmlString('Last date: <strong>' . $this->due_date . '</strong>'))

The HtmlString class enable you to write HTML tag on mail body. It would be helpful for someone who is looking for this type of solution.

Thanks.

Solution 3:[3]

Use normal line breaks with single quotes

'This will create a
new line'

or use \n with double quotes

"This will also create a \n new line"

More info here

Solution 4:[4]

I'm not sure which version of Laravel it was introduced in, but you can use Markdown in MailMessage, so HtmlString is not necessary if simple formatting is all that is required.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Halsey
Solution 2 Nazmus Shakib
Solution 3 Douwe de Haan
Solution 4 Brendan