From 4a55567b7caa0ef8ed4bc83ce410a9ef5f24be58 Mon Sep 17 00:00:00 2001 From: Antoine Bellion Date: Mon, 15 Jan 2018 22:34:53 +0100 Subject: [PATCH 1/3] Allow to set plain HTML in mails --- src/Illuminate/Mail/Mailable.php | 24 ++++++++++++++++++++++++ src/Illuminate/Mail/Mailer.php | 13 +++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 86ea7a7ebfa8..3cce1249b569 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -6,6 +6,7 @@ use ReflectionProperty; use BadMethodCallException; use Illuminate\Support\Str; +use Illuminate\Support\HtmlString; use Illuminate\Support\Collection; use Illuminate\Container\Container; use Illuminate\Contracts\Support\Renderable; @@ -64,6 +65,13 @@ class Mailable implements MailableContract, Renderable */ protected $markdown; + /** + * The HTML to use for the message. + * + * @var string + */ + protected $html; + /** * The view to use for the message. * @@ -185,6 +193,9 @@ public function render() */ protected function buildView() { + if (isset($this->html)) { + return ['html' => new HtmlString($this->html)]; + } if (isset($this->markdown)) { return $this->buildMarkdownView(); } @@ -585,6 +596,19 @@ public function markdown($view, array $data = []) return $this; } + /** + * Set the HTML content for the message. + * + * @param string $html + * @return $this + */ + public function html($html) + { + $this->html = $html; + + return $this; + } + /** * Set the view and view data for the message. * diff --git a/src/Illuminate/Mail/Mailer.php b/src/Illuminate/Mail/Mailer.php index 3614bae22fe0..393cef56c94a 100755 --- a/src/Illuminate/Mail/Mailer.php +++ b/src/Illuminate/Mail/Mailer.php @@ -4,6 +4,7 @@ use Swift_Mailer; use InvalidArgumentException; +use Illuminate\Support\HtmlString; use Illuminate\Contracts\View\Factory; use Illuminate\Support\Traits\Macroable; use Illuminate\Contracts\Support\Htmlable; @@ -159,6 +160,18 @@ public function raw($text, $callback) return $this->send(['raw' => $text], [], $callback); } + /** + * Send a new message when only a html part. + * + * @param string $html + * @param mixed $callback + * @return void + */ + public function html($html, $callback) + { + return $this->send(['html' => new HtmlString($html)], [], $callback); + } + /** * Send a new message when only a plain part. * From 2fccd8c401dd094aadae4db130ff9c840681c44c Mon Sep 17 00:00:00 2001 From: Antoine Bellion Date: Wed, 17 Jan 2018 16:04:40 +0100 Subject: [PATCH 2/3] Send a plain text part if available --- src/Illuminate/Mail/Mailable.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 3cce1249b569..a7ec409f3200 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -194,7 +194,10 @@ public function render() protected function buildView() { if (isset($this->html)) { - return ['html' => new HtmlString($this->html)]; + return array_filter([ + 'html' => new HtmlString($this->html), + 'text' => isset($this->textView) ? $this->textView : null + ]); } if (isset($this->markdown)) { return $this->buildMarkdownView(); From 6ca7a86f2d4ca01a11d94171df3775012f102143 Mon Sep 17 00:00:00 2001 From: Antoine Bellion Date: Wed, 17 Jan 2018 17:44:12 +0100 Subject: [PATCH 3/3] Fix CI --- src/Illuminate/Mail/Mailable.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index a7ec409f3200..5f4a3da5493d 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -6,8 +6,8 @@ use ReflectionProperty; use BadMethodCallException; use Illuminate\Support\Str; -use Illuminate\Support\HtmlString; use Illuminate\Support\Collection; +use Illuminate\Support\HtmlString; use Illuminate\Container\Container; use Illuminate\Contracts\Support\Renderable; use Illuminate\Contracts\Queue\Factory as Queue; @@ -196,7 +196,7 @@ protected function buildView() if (isset($this->html)) { return array_filter([ 'html' => new HtmlString($this->html), - 'text' => isset($this->textView) ? $this->textView : null + 'text' => isset($this->textView) ? $this->textView : null, ]); } if (isset($this->markdown)) {