Laravel 5.3 将 html 添加到 MailMessage->line

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41283714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:58:52  来源:igfitidea点击:

Laravel 5.3 add html to MailMessage->line

phphtmllaravelemaillaravel-5

提问by Arno van Oordt

I like to add some html tags to the body of my mail using MailMessage like this:

我喜欢使用 MailMessage 在邮件正文中添加一些 html 标签,如下所示:

$mailMessage = new MailMessage();
$mailMessage->line(trans('mail.someLine') );

In mail.php:

在mail.php中:

'someLine' => 'Bla bla <a href="html://someurl">html://someurl</a>'

But in the actual mail the entire line comes out as plain text.

但在实际邮件中,整行都以纯文本形式出现。

I've tried to use html_entity_decodebut without any success:

我尝试使用html_entity_decode但没有任何成功:

 $mailMessage->line(html_entity_decode(trans('mail.someLine')));

Seems like the line method does it's own encoding. Is there a workaround for this?

似乎 line 方法有自己的编码。有解决方法吗?

ps The rest of the mail has proper html so that's not the point!

ps 邮件的其余部分具有适当的 html,所以这不是重点!

update: $mailMessage->action won't do in this case. Let say it should work for something like this as well: $mailMessage->line('Bla bla <strong>something strong</strong> bla');

更新: $mailMessage->action 在这种情况下不起作用。假设它也适用于这样的事情: $mailMessage->line('Bla bla <strong>something strong</strong> bla');

回答by Artur Anyszek

Better to use HtmlStringclass.

最好使用HtmlString类。

use Illuminate\Support\HtmlString;

return (new MailMessage)
  ->line(new HtmlString($someHtmlBody));

Line will be show as processed HTML code.

行将显示为已处理的 HTML 代码。

回答by Arno van Oordt

Checking the MailMessage/SimpleMessage code I found it was not these classes that did the encoding but the email.blade.php template itself.

检查 MailMessage/SimpleMessage 代码,我发现进行编码的不是这些类,而是 email.blade.php 模板本身。

Changing the {{ $line }}parts into {!! $line !!}in the email.blade.php eventually did the trick.

将这些{{ $line }}部分更改为 {!! $line !!}email.blade.php 最终成功了。

ps Make sure to run php artisan vendor:publishfirst so you don't need to change the original email.blade.php in the vendor folder!

ps 一定php artisan vendor:publish要先运行,这样就不用修改vendor文件夹里原来的email.blade.php了!

回答by ggg

That's because the line function is not supposed to contain html. If you want to pass an URL you should use the action function. You might also want to customize your mail template. Check the official docs about the mail notifications: https://laravel.com/docs/5.3/notifications#mail-notifications

那是因为 line 函数不应该包含 html。如果你想传递一个 URL,你应该使用 action 函数。您可能还想自定义您的邮件模板。查看关于邮件通知的官方文档:https: //laravel.com/docs/5.3/notifications#mail-notifications

回答by Jaymin Panchal

try by adding action tag with MailMesage object.

尝试使用 MailMesage 对象添加操作标记。

 $mailMessage
        ->line('Simple mail')
        ->action('Link Title', url('password/reset', $this->token));