【问题标题】:Add bold text and line break into Laravel Notification Email在 Laravel 通知电子邮件中添加粗体文本和换行符
【发布时间】:2019-02-10 18:39:38
【问题描述】:

Laravel 5.6

我正在尝试通过电子邮件发送 Laravel 通知。我想让一些文本加粗,并放入换行符,而不是 line($text) 方法带来的全新段落。所以我在通知类中试过这个。我也尝试过使用\n 字符串换行。

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

我尝试过通过php artisan vendor:publish --tag=laravel-mail 命令发布和不发布刀片模板,然后毫无乐趣地将{{$line}} 更新为{!! $line !!}}。想不通。

它在mailtrap中这样打印

【问题讨论】:

    标签: php html laravel notifications


    【解决方案1】:

    也许你已经完成了你的工作,因为这个问题太老了。但我想分享一个在 Laravel 邮件模板上编写 HTML 标签的简单方法。

    首先,你需要导入 use Illuminate\Support\HtmlString;

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

    HtmlString 类使您可以在邮件正文上编写 HTML 标记。 这对正在寻找此类解决方案的人会有所帮助。

    谢谢。

    【讨论】:

      【解决方案2】:

      我想通了,以防外面有人和我一样愚蠢。
      我认为这被打破有两个原因。 @DouwedeHaan 建议我在使用 \n 时使用双引号而不是单引号,这并没有多大作用,但结合下一部分我认为可以解决问题。

      呈现 html 的刀片模板在 markdown 中。我没有想到这一点。它的布局是特定的,我在发布它后不小心破坏了它,删除了一些行并使用缩进格式化文件,这破坏了一切。

      我删除了文件,重新发布了模板,将{{$line}} 的所有实例更新为{!! $line !!},确保文件的其余部分保持原样,更新了我的通知以使用双引号并坚持使用&lt;br/&gt; 和@ 987654327@ 标签,现在它可以按预期工作了。

      【讨论】:

      • 太棒了!有用。谢谢!
      【解决方案3】:

      使用带单引号的普通换行符

      'This will create a
      new line'
      

      或使用带双引号的\n

      "This will also create a \n new line"
      

      More info here

      【讨论】:

      • 不,这似乎不起作用。它去掉了 \n 标签,仍然把它放在一行上。这就是建议。
      【解决方案4】:

      您确实需要更新通知视图,以便您走在正确的轨道上。

      基本上在发布通知包的资源之后这样做:

      $ php artisan vendor:publish --tag=laravel-notifications

      您需要做的就是打开 resources/views/vendor/notifications/email.blade.php 并将出现的 {{ $line }} 替换为 {!! $line !!}。

      这只是告诉 Blade 不要逃避标签。

      希望这会有所帮助!

      【讨论】:

      • 你显然没有阅读我的问题。我已经这样做了。我知道什么{!! !!} 会。
      猜你喜欢
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      相关资源
      最近更新 更多