2017-01-05 7 views
4

私はデフォルトのnotification system(Laravel 5.3)を使用してメールを送信しています。メッセージにHTMLタグを追加したい。これは、(それがプレーンテキストで強力なタグが表示されます)動作しません。Laravel:通知のHTML

public function toMail($notifiable) 
{ 
    return (new MailMessage) 
       ->subject('Info') 
       ->line("Hello <strong>World</strong>") 
       ->action('Voir le reporting', config('app.url')); 
} 

私は、テキストがメール通知テンプレートに{{ $text }}に表示されているので、それは普通のことだ知っています。

->line(new \Illuminate\Support\HtmlString('Hello <strong>World</strong>')) 

しかし、それは動作しません:私はcsrf_field()ヘルパーと同じシステムを使用しようとしたことは、プレーンテキストとして強い表示されます。

ビューを変更せずにHTMLタグを送信できますか?(私はビューを変更したくない:保護テキストは他のすべてのケースでOKです)。十分にはっきりしていることを願っています。

答えて

1

また、MailMessageクラスを拡張した新しいMailClassを作成することもできます。

たとえばあなたがapp\Notifications

<?php 

namespace App\Notifications; 

use Illuminate\Notifications\Messages\MailMessage; 

class MailExtended extends MailMessage 
{ 
    /** 
    * The notification's data. 
    * 
    * @var string|null 
    */ 
    public $viewData; 

    /** 
    * Set the content of the notification. 
    * 
    * @param string $greeting 
    * 
    * @return $this 
    */ 
    public function content($content) 
    { 
     $this->viewData['content'] = $content; 

     return $this; 
    } 

    /** 
    * Get the data array for the mail message. 
    * 
    * @return array 
    */ 
    public function data() 
    { 
     return array_merge($this->toArray(), $this->viewData); 
    } 
} 

でこのクラスを作成することができます。そして、あなたの通知に使用します。その代わり

return (new MailMessage()) 

に変更し、それを:

return (new MailExtended()) 

そして、content varを通知ビューで使用できます。あなたは(php artisan vendor:publish)を通知ビューを公開する場合たとえば、あなたはresources/views/vendor/notificationsemail.blade.phpを編集して、この追加することができます

@if (isset($content)) 
<hr> 
    {!! $content !!} 
<hr> 
@endif 

我々はこのようにそれを行うと、魔法のように動作します:D

5

実行php artisan vendor:publish からresources/views/vendor/notificationsvendorディレクトリからコピーします。

このビューを開き、{{ $line }}{!! $line !!}に2か所で変更します。 Laravel 5.3では、これらは101および137行です。

これは、通知メールにHTMLタグを使用できるようにするunescapedline文字列を表示します。

関連する問題