Skip to content

Commit

Permalink
Properly utilize the plainOnly flag in mail.beforeAddContent (#3479)
Browse files Browse the repository at this point in the history
Credit to @hlev
  • Loading branch information
hlev authored and Luke Towers committed Sep 24, 2018
1 parent fe19a7f commit d1ff4a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
5 changes: 3 additions & 2 deletions ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,10 @@ protected function registerMailer()
/*
* Override standard Mailer content with template
*/
Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data, $raw) {
Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data, $raw, $plain) {
$method = $raw === null ? 'addContentToMailer' : 'addRawContentToMailer';
return !MailManager::instance()->$method($message, $raw ?: $view, $data);
$plainOnly = $view === null; // When "plain-text only" email is sent, $view is null, this sets the flag appropriately
return !MailManager::instance()->$method($message, $raw ?: $view ?: $plain, $data, $plainOnly);
});
}

Expand Down
36 changes: 24 additions & 12 deletions classes/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class MailManager

/**
* Same as `addContentToMailer` except with raw content.
*
* @return bool
*/
public function addRawContentToMailer($message, $content, $data)
Expand All @@ -70,11 +71,16 @@ public function addRawContentToMailer($message, $content, $data)
}

/**
* This function hijacks the `addContent` method of the `October\Rain\Mail\Mailer`
* This function hijacks the `addContent` method of the `October\Rain\Mail\Mailer`
* class, using the `mailer.beforeAddContent` event.
*
* @param \Illuminate\Mail\Message $message
* @param string $code
* @param array $data
* @param bool $plainOnly Add only plain text content to the message
* @return bool
*/
public function addContentToMailer($message, $code, $data)
public function addContentToMailer($message, $code, $data, $plainOnly = false)
{
if (isset($this->templateCache[$code])) {
$template = $this->templateCache[$code];
Expand All @@ -87,16 +93,21 @@ public function addContentToMailer($message, $code, $data)
return false;
}

$this->addContentToMailerInternal($message, $template, $data);
$this->addContentToMailerInternal($message, $template, $data, $plainOnly);

return true;
}

/**
* Internal method used to share logic between `addRawContentToMailer` and `addContentToMailer`
*
* @param \Illuminate\Mail\Message $message
* @param string $template
* @param array $data
* @param bool $plainOnly Add only plain text content to the message
* @return void
*/
protected function addContentToMailerInternal($message, $template, $data)
protected function addContentToMailerInternal($message, $template, $data, $plainOnly = false)
{
/*
* Start twig transaction
Expand Down Expand Up @@ -124,12 +135,14 @@ protected function addContentToMailerInternal($message, $template, $data)
'subject' => $swiftMessage->getSubject()
];

/*
* HTML contents
*/
$html = $this->renderTemplate($template, $data);
if (!$plainOnly) {
/*
* HTML contents
*/
$html = $this->renderTemplate($template, $data);

$message->setBody($html, 'text/html');
$message->setBody($html, 'text/html');
}

/*
* Text contents
Expand Down Expand Up @@ -200,9 +213,8 @@ public function renderTemplate($template, $data = [])

/**
* Render the Markdown template into text.
*
* @param string $view
* @param array $data
* @param $content
* @param array $data
* @return string
*/
public function renderText($content, $data = [])
Expand Down

0 comments on commit d1ff4a4

Please sign in to comment.