Laravel 5 adding HTML to email
Solution 1
You need to specify html
key in the first parameter:
Mail::send( ['html' => 'emails.newinvoice'], ['text' => $emailtext],
// ^^^^
Also replace auto-escaped block {{ }}
with unescaped {!! !!}
in the template:
<p> {!! $text !!} </p>
Solution 2
You need to use:
{!! $text !!}
instead of
{{ $text }}
Blade automatically escapes any html when echoing unless you explicitly tell it not to.
Solution 3
Yeah the solution above work just fine..
use {!! $contents !!}
instead of this
{{ $contents }}
This {!! $contents !!}
is for allowing html
While this {{ $contents }}
is just for plain text.
Solution 4
After checking various solutions, following codes worked for me -
try {
$template_data = ['otp' => $otp, 'name' => $name];
//send verification code
Mail::send(['html' => 'email.account_verification'], $template_data,
function ($message) use ($email) {
$message->to($email)
->from('[email protected]') //not sure why I have to add this
->subject('Account verification');
});
return Response::json(['code' => 200, 'msg' => 'Sent successfully']);
} catch (Exception $ex) {
return Response::json(['code' => 200, 'msg' => 'Something went wrong, please try later.']);
}
Related videos on Youtube
Comments
-
Devos50 almost 3 years
I'm currently trying to create a HTML email in Laravel 5 and I have some text (which contains
<br/>
elements) I want to insert in the email. I use the following piece of code to send the email:Mail::send(array('html' => 'emails.newinvoice'), array('text' => $emailtext), function($message) use ($email, $subject, $contact_company) { $message->to($email, $contact_company)->subject($subject); });
So the
$emailtext
variable contains some text with HTML tags. In myemails.newinvoice
layout view, I have the following:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head></head> <body> <p>{{{ $text }}}</p> </body> </html>
When sending the email, the inserted text in my mail and the HTML elements in that text are displayed as normal characters which means that my email shows up like:
test<br/>test
Instead of
test test
How can I make sure that the HTML tags in the inserted text get rendered as HTML and not as plain text?
-
dev7 about 9 yearsWhat happens when you use {!! $text !!}?
-
-
Devos50 about 9 yearsUnfortunately, my inserted HTML still gets rendered as plain text after this change.
-
Simranjeet almost 6 yearsThanks Also replace auto-escaped block {{ }} with unescaped {!! !!} in the template: this helps me