-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Markdown Emails + Notifications (#16768)
This PR adds support for writing mailables and notifications in simple Markdown syntax using Blade and rendering the contents in a responsive HTML temlate along with plain text counterpart. Customizable themes and exportable views.
- Loading branch information
1 parent
b3780d7
commit 44880df
Showing
41 changed files
with
1,182 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
namespace Illuminate\Mail; | ||
|
||
use Parsedown; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\HtmlString; | ||
use Illuminate\View\Factory as ViewFactory; | ||
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles; | ||
|
||
class Markdown | ||
{ | ||
/** | ||
* The view factory implementation. | ||
* | ||
* @var \Illuminate\View\Factory | ||
*/ | ||
protected $view; | ||
|
||
/** | ||
* The current theme being used when generating emails. | ||
* | ||
* @var string | ||
*/ | ||
protected $theme = 'default'; | ||
|
||
/** | ||
* The registered component paths. | ||
* | ||
* @var array | ||
*/ | ||
protected $componentPaths = []; | ||
|
||
/** | ||
* Create a new Markdown renderer instance. | ||
* | ||
* @param \Illuminate\View\Factory $view | ||
* @param array $options | ||
* @return void | ||
*/ | ||
public function __construct(ViewFactory $view, array $options = []) | ||
{ | ||
$this->view = $view; | ||
$this->theme = Arr::get($options, 'theme', 'default'); | ||
$this->loadComponentsFrom(Arr::get($options, 'paths', [])); | ||
} | ||
|
||
/** | ||
* Render the Markdown template into HTML. | ||
* | ||
* @param string $view | ||
* @param array $data | ||
* @param \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles|null $inliner | ||
* @return \Illuminate\Support\HtmlString | ||
*/ | ||
public function render($view, array $data = [], $inliner = null) | ||
{ | ||
$this->view->flushFinderCache(); | ||
|
||
$contents = $this->view->replaceNamespace( | ||
'mail', $this->htmlComponentPaths() | ||
)->make($view, $data)->render(); | ||
|
||
return new HtmlString(with($inliner ?: new CssToInlineStyles)->convert( | ||
$contents, $this->view->make('mail::themes.'.$this->theme)->render() | ||
)); | ||
} | ||
|
||
/** | ||
* Render the Markdown template into HTML. | ||
* | ||
* @param string $view | ||
* @param array $data | ||
* @return \Illuminate\Support\HtmlString | ||
*/ | ||
public function renderText($view, array $data = []) | ||
{ | ||
$this->view->flushFinderCache(); | ||
|
||
return new HtmlString(preg_replace("/[\r\n]{2,}/", "\n\n", $this->view->replaceNamespace( | ||
'mail', $this->markdownComponentPaths() | ||
)->make($view, $data)->render())); | ||
} | ||
|
||
/** | ||
* Parse the given Markdown text into HTML. | ||
* | ||
* @param string $text | ||
* @return string | ||
*/ | ||
public static function parse($text) | ||
{ | ||
$parsedown = new Parsedown; | ||
|
||
return new HtmlString($parsedown->text($text)); | ||
} | ||
|
||
/** | ||
* Get the HTML component paths. | ||
* | ||
* @return array | ||
*/ | ||
public function htmlComponentPaths() | ||
{ | ||
return array_map(function ($path) { | ||
return $path.'/html'; | ||
}, $this->componentPaths()); | ||
} | ||
|
||
/** | ||
* Get the Markdown component paths. | ||
* | ||
* @return array | ||
*/ | ||
public function markdownComponentPaths() | ||
{ | ||
return array_map(function ($path) { | ||
return $path.'/markdown'; | ||
}, $this->componentPaths()); | ||
} | ||
|
||
/** | ||
* Get the component paths. | ||
* | ||
* @return array | ||
*/ | ||
protected function componentPaths() | ||
{ | ||
return array_unique(array_merge($this->componentPaths, [ | ||
__DIR__.'/resources/views', | ||
])); | ||
} | ||
|
||
/** | ||
* Register new mail component paths. | ||
* | ||
* @param array $paths | ||
* @return void | ||
*/ | ||
public function loadComponentsFrom(array $paths = []) | ||
{ | ||
$this->componentPaths = $paths; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<table class="action" align="center" width="100%" cellpadding="0" cellspacing="0"> | ||
<tr> | ||
<td align="center"> | ||
<table width="100%" border="0" cellpadding="0" cellspacing="0"> | ||
<tr> | ||
<td align="center"> | ||
<table border="0" cellpadding="0" cellspacing="0"> | ||
<tr> | ||
<td> | ||
<a href="{{ $url }}" class="button button-{{ $color or 'blue' }}" target="_blank">{{ $slot }}</a> | ||
</td> | ||
</tr> | ||
</table> | ||
</td> | ||
</tr> | ||
</table> | ||
</td> | ||
</tr> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<tr> | ||
<td> | ||
<table class="footer" align="center" width="570" cellpadding="0" cellspacing="0"> | ||
<tr> | ||
<td class="content-cell" align="center"> | ||
{{ Illuminate\Mail\Markdown::parse($slot) }} | ||
</td> | ||
</tr> | ||
</table> | ||
</td> | ||
</tr> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<tr> | ||
<td class="header"> | ||
<a href="{{ $url }}"> | ||
{{ $slot }} | ||
</a> | ||
</td> | ||
</tr> |
Oops, something went wrong.