Laravel 5 adding HTML to email

后端 未结 4 1028
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-31 00:01

I\'m currently trying to create a HTML email in Laravel 5 and I have some text (which contains
elements) I want to insert in the email. I use the fo

相关标签:
4条回答
  • 2020-12-31 00:32

    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('test@yourdamin.com') //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.']);
          }  
    
    0 讨论(0)
  • 2020-12-31 00:33

    You need to use:

    {!! $text !!}
    

    instead of

    {{ $text }}
    

    Blade automatically escapes any html when echoing unless you explicitly tell it not to.

    0 讨论(0)
  • 2020-12-31 00:42

    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.

    0 讨论(0)
  • 2020-12-31 00:45

    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>
    
    0 讨论(0)
提交回复
热议问题