Commit 48a596d2 authored by Roman Mátyus's avatar Roman Mátyus
Browse files

Refactoring and fix plaintext mails

parent ebbe8dd0
...@@ -47,8 +47,7 @@ class FileMailer extends Object implements IMailer ...@@ -47,8 +47,7 @@ class FileMailer extends Object implements IMailer
{ {
$this->checkRequirements(); $this->checkRequirements();
$content = $message->generateMessage(); $content = $message->generateMessage();
preg_match("/----------[a-z0-9]{10}--/", $content, $match); $message_id = self::mailParser($content)->message_id;
$message_id = substr($match[0], 10, -2);
$path = $this->tempDir."/".$this->prefix.$message_id; $path = $this->tempDir."/".$this->prefix.$message_id;
if ($bytes = file_put_contents($path, $content)) if ($bytes = file_put_contents($path, $content))
return $bytes; return $bytes;
...@@ -77,4 +76,64 @@ class FileMailer extends Object implements IMailer ...@@ -77,4 +76,64 @@ class FileMailer extends Object implements IMailer
return $this->prefix; return $this->prefix;
} }
/**
* Parser of stored files.
* @param string $content
* @return StdClass
*/
public static function mailParser($content)
{
preg_match("/Message-ID: <[a-zA-Z0-9-]*@[a-zA-Z0-9-]*>/", $content, $match);
$message_id = (isset($match[0])) ? substr($match[0], 13, -2) : NULL;
$message_id = explode("@",$message_id);
$message_id = $message_id[0];
$mess = explode("\r\n\r\n", $content);
preg_match_all("/[a-zA-Z-]*: .*/", $mess[0], $matches);
$header = array();
foreach ($matches[0] as $line) {
$temp = explode(": ",$line);
$header[strtolower($temp[0])] = iconv_mime_decode(str_replace(array("\r","\n","\r\n"),"",$temp[1]));
}
if (isset($header["date"]))
$header["date"] = new DateTime($header["date"]);
if (preg_match("/\r\n\r\n----------/", $content)) { // html mail
$mess = explode("\r\n\r\n----------", $content);
$mess = substr($mess[1], 10, -22);
$mess = explode("----------", $mess);
$temp_mess = array();
foreach ($mess as $part) {
if (preg_match("/text\/html/", $part))
$temp_mess["html"] = $part;
elseif (preg_match("/text\/plain/", $part))
$temp_mess["plain"] = $part;
}
$mess = $temp_mess;
$temp_mess = array();
foreach ($mess as $type => $part) {
$temp_mess[$type] = explode("\r\n",$part);
for($i=0;$i<=3;$i++)
unset($temp_mess[$type][$i]);
$temp_mess[$type] = implode("\r\n",$temp_mess[$type]);
}
$mess = $temp_mess;
} else {
$mess = array(
"plain" => $mess[1],
"html" => NULL,
);
}
return (object) array_merge(
array(
"message_id" => $message_id,
"header" => $header,
"plain" => $mess['plain'],
"html" => $mess['html'],
"raw" => $content,
),
$header
);
}
} }
...@@ -40,12 +40,11 @@ img#mailpanel-icon { ...@@ -40,12 +40,11 @@ img#mailpanel-icon {
border:1px solid silver; border:1px solid silver;
} }
</style> </style>
<h1>MailPanel: {if $countAll==0}no{else}{$countAll}{/if} mail{if $countAll>1}s{/if}, {$countNew} new</h1> <h1>MailPanel: {if $countAll==0}no{else}{$countAll}{/if} mail{if $countAll>1}s{/if}, {$countNew} new</h1>
<div n:if="$countAll>0"><a href="/?mail-panel=delete-all">Delete All</a></div> <div n:if="$countAll>0"><a href="/?mail-panel=delete-all">Delete All</a></div>
<div n:if="$countAll>0" class="nette-inner"> <div n:if="$countAll>0" class="nette-inner">
<div n:foreach="$messages as $message"> <div n:foreach="$messages as $message">
<a class="nette-toggler" rel="#nette-mailPanel-message-{$message->message_id}" href="#">{$message->date|date:"d.m.Y H:i:s"} {$message->subject} ►</a> <a class="nette-toggler" rel="#nette-mailPanel-message-{$message->message_id}" href="#">{$message->date} {$message->subject} ►</a>
<div id="nette-mailPanel-message-{$message->message_id}" class="nette-collapsed"> <div id="nette-mailPanel-message-{$message->message_id}" class="nette-collapsed">
<table n:if="!empty($show)" n:inner-foreach="$show as $item"> <table n:if="!empty($show)" n:inner-foreach="$show as $item">
<tr n:if="$message->$item"> <tr n:if="$message->$item">
...@@ -70,8 +69,8 @@ img#mailpanel-icon { ...@@ -70,8 +69,8 @@ img#mailpanel-icon {
<pre>{$message->plain}</pre> <pre>{$message->plain}</pre>
</div> </div>
</span> </span>
<a class="nette-toggler" rel="#nette-mailPanel-message-html-{$message->message_id}" href="#">html ►</a> <a n:if="$message->html" class="nette-toggler" rel="#nette-mailPanel-message-html-{$message->message_id}" href="#">html ►</a>
<span id="nette-mailPanel-message-html-{$message->message_id}" class="nette-collapsed"> <span n:if="$message->html" id="nette-mailPanel-message-html-{$message->message_id}" class="nette-collapsed">
<div class="nette-mailPanel-box"> <div class="nette-mailPanel-box">
{!$message->html} {!$message->html}
</div> </div>
......
...@@ -164,7 +164,7 @@ class MailPanel extends Control implements IBarPanel { ...@@ -164,7 +164,7 @@ class MailPanel extends Control implements IBarPanel {
foreach (Finder::findFiles('*')->in($this->fileMailer->tempDir) as $file) { foreach (Finder::findFiles('*')->in($this->fileMailer->tempDir) as $file) {
$message = $this->cache->load($file->getFilename()); $message = $this->cache->load($file->getFilename());
if ($message === NULL) { if ($message === NULL) {
$message = self::mailParser(file_get_contents($file)); $message = FileMailer::mailParser(file_get_contents($file));
$this->cache->save($file->getFilename(),$message); $this->cache->save($file->getFilename(),$message);
} }
$time = new DateTime; $time = new DateTime;
...@@ -176,58 +176,6 @@ class MailPanel extends Control implements IBarPanel { ...@@ -176,58 +176,6 @@ class MailPanel extends Control implements IBarPanel {
return TRUE; return TRUE;
} }
/**
* Parser of stored files.
* @param string $content
* @return StdClass
*/
public static function mailParser($content)
{
preg_match("/----------[a-z0-9]{10}--/", $content, $match);
$message_id = (isset($match[0])) ? substr($match[0], 10, -2) : NULL;
$mess = explode("\r\n\r\n----------", $content);
preg_match_all("/[a-zA-Z-]*: .*/", $mess[0], $matches);
$header = array();
foreach ($matches[0] as $line) {
$temp = explode(": ",$line);
$header[strtolower($temp[0])] = iconv_mime_decode(substr($temp[1],0,-1));
}
if (isset($header["date"]))
$header["date"] = new DateTime($header["date"]);
$mess = explode("\r\n\r\n----------", $content);
$mess = substr($mess[1], 10, -22);
$mess = explode("----------", $mess);
$temp_mess = array();
foreach ($mess as $part) {
if (preg_match("/text\/html/", $part))
$temp_mess["html"] = $part;
elseif (preg_match("/text\/plain/", $part))
$temp_mess["plain"] = $part;
}
$mess = $temp_mess;
$temp_mess = array();
foreach ($mess as $type => $part) {
$temp_mess[$type] = explode("\r\n",$part);
for($i=0;$i<=3;$i++)
unset($temp_mess[$type][$i]);
$temp_mess[$type] = implode("\r\n",$temp_mess[$type]);
}
$mess = $temp_mess;
return (object) array_merge(
array(
"message_id" => $message_id,
"header" => $header,
"plain" => $mess['plain'],
"html" => $mess['html'],
"raw" => $content,
),
$header
);
}
/** /**
* Autoremove mails from filesystem an cache by argument 'autoremove'. * Autoremove mails from filesystem an cache by argument 'autoremove'.
*/ */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment