Skip to content

Commit

Permalink
Sending raw markdown emails
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Samuel Georges committed Jul 29, 2017
1 parent eebb1c1 commit 7479207
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
6 changes: 3 additions & 3 deletions ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ protected function registerMailer()
/*
* 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);
});
}

Expand Down
31 changes: 31 additions & 0 deletions classes/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -69,6 +85,21 @@ public function addContentToMailer($message, $code, $data)
$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
*/
Expand Down
20 changes: 15 additions & 5 deletions models/MailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,22 @@ public static function syncAll()
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');
Expand All @@ -128,10 +136,12 @@ protected static function getTemplateSections($code)

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;
Expand Down
1 change: 1 addition & 0 deletions views/mail/partial-subcopy.htm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name = "Subcopy"
==
-----
{{ body|trim }}
==
<table class="subcopy" width="100%" cellpadding="0" cellspacing="0">
Expand Down

0 comments on commit 7479207

Please sign in to comment.