Sending raw markdown emails

If raw == (string) content, then it is a markdown email (html/text are ignored -- markdown sets them)
If raw == (bool) true, then it is true raw (html/text must be supplied)
This commit is contained in:
Samuel Georges 2017-07-30 00:25:11 +10:00
parent 96d0535d09
commit 5a417d72d3
4 changed files with 50 additions and 8 deletions

View File

@ -322,9 +322,9 @@ class ServiceProvider extends ModuleServiceProvider
/*
* Override standard Mailer content with template
*/
Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data) {
MailManager::instance()->addContentToMailer($message, $view, $data);
return false;
Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data, $raw) {
$method = $raw === null ? 'addContentToMailer' : 'addRawContentToMailer';
return !MailManager::instance()->$method($message, $raw ?: $view, $data);
});
}

View File

@ -56,9 +56,25 @@ class MailManager
*/
protected $isTwigStarted = false;
/**
* Same as `addContentToMailer` except with raw content.
* @return bool
*/
public function addRawContentToMailer($message, $content, $data)
{
$template = new MailTemplate;
$template->fillFromContent($content);
$this->addContentToMailerInternal($message, $template, $data);
return true;
}
/**
* This function hijacks the `addContent` method of the `October\Rain\Mail\Mailer`
* class, using the `mailer.beforeAddContent` event.
* @return bool
*/
public function addContentToMailer($message, $code, $data)
{
@ -69,6 +85,21 @@ class MailManager
$this->templateCache[$code] = $template = MailTemplate::findOrMakeTemplate($code);
}
if (!$template) {
return false;
}
$this->addContentToMailerInternal($message, $template, $data);
return true;
}
/**
* Internal method used to share logic between `addRawContentToMailer` and `addContentToMailer`
* @return void
*/
protected function addContentToMailerInternal($message, $template, $data)
{
/*
* Start twig transaction
*/

View File

@ -105,14 +105,22 @@ class MailTemplate extends Model
public function afterFetch()
{
if (!$this->is_custom) {
$this->fillFromView();
$this->fillFromView($this->code);
}
}
public function fillFromView()
public function fillFromContent($content)
{
$sections = self::getTemplateSections($this->code);
$this->fillFromSections(MailParser::parse($content));
}
public function fillFromView($path)
{
$this->fillFromSections(self::getTemplateSections($path));
}
protected function fillFromSections($sections)
{
$this->content_html = $sections['html'];
$this->content_text = $sections['text'];
$this->subject = array_get($sections, 'settings.subject', 'No subject');
@ -128,10 +136,12 @@ class MailTemplate extends Model
public static function findOrMakeTemplate($code)
{
if (!$template = self::whereCode($code)->first()) {
$template = self::whereCode($code)->first();
if (!$template && View::exists($code)) {
$template = new self;
$template->code = $code;
$template->fillFromView();
$template->fillFromView($code);
}
return $template;

View File

@ -1,5 +1,6 @@
name = "Subcopy"
==
-----
{{ body|trim }}
==
<table class="subcopy" width="100%" cellpadding="0" cellspacing="0">