Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5向电子邮件添加HTML_Php_Html_Email_Laravel_Newline - Fatal编程技术网

Php Laravel 5向电子邮件添加HTML

Php Laravel 5向电子邮件添加HTML,php,html,email,laravel,newline,Php,Html,Email,Laravel,Newline,我目前正在尝试在Laravel 5中创建一封HTML电子邮件,我想在电子邮件中插入一些文本(包含元素)。我使用以下代码发送电子邮件: Mail::send(array('html' => 'emails.newinvoice'), array('text' => $emailtext), function($message) use ($email, $subject, $contact_company) { $message->to($email, $contact_

我目前正在尝试在Laravel 5中创建一封HTML电子邮件,我想在电子邮件中插入一些文本(包含

元素)。我使用以下代码发送电子邮件:

Mail::send(array('html' => 'emails.newinvoice'), array('text' => $emailtext), function($message) use ($email, $subject, $contact_company)
{
    $message->to($email, $contact_company)->subject($subject);
});
因此,
$emailtext
变量包含一些带有HTML标记的文本。在我的
emails.newinvoice
布局视图中,我有以下内容:

<!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>

如何确保插入文本中的HTML标记呈现为HTML而不是纯文本?

您需要在第一个参数中指定
HTML
键:

Mail::send( ['html' => 'emails.newinvoice'], ['text' => $emailtext], 
//           ^^^^
还可以在模板中将自动转义块
{{}
替换为未转义的
{!!!!}

<p> {!! $text !!} </p>
{!!$text!!}

您需要使用:

{!! $text !!}
而不是

{{ $text }}

Blade在回显时会自动转义任何html,除非您明确告诉它不要转义。

是的,上述解决方案工作正常

使用
{!!$contents!!}

而不是这个

{{ $contents }}
{!!$contents!!}
用于允许html
虽然此
{{$contents}}
仅用于纯文本。

检查各种解决方案后,以下代码对我有效-

 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.']);
      }  

如果您使用ShouldQueue发送电子邮件,则应再次运行命令“php artisan queue:work”,然后再次运行应用程序。之后,您检查收件人电子邮件,您可以看到以html格式显示的电子邮件内容。

不幸的是,我插入的html在此更改后仍然呈现为纯文本。还感谢您将模板中的自动转义块{{}替换为未转义的{!!!!}:这有助于我了解使用{!!$text!!}时会发生什么情况?
{{ $contents }}
 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.']);
      }