Skip to content

Instantly share code, notes, and snippets.

@neofreko
Forked from anonymous/IndexController.php
Last active December 10, 2015 01:58
Show Gist options
  • Save neofreko/4363208 to your computer and use it in GitHub Desktop.
Save neofreko/4363208 to your computer and use it in GitHub Desktop.
I wish I have mail-preview.rb in PHP. Originally, these are part of ZF (CLI) controller code. Converting to non-controller is left as exercise for the reader. Yes, I stole the template from the original erb
<?php
class IndexController {
public function mailpreviewAction() {
$getopt = $this->getRequest()->getOpt();
$extra = $getopt->getRemainingArgs();
// create new getopt
$cliopt = new Zend_Console_Getopt(array(
'filename|f=s' => 'Zend generated email',
'browser|b=s' => 'Open using specified browser app'
));
$cliopt->setArguments($extra);
if (!$cliopt->filename)
die($cliopt->getUsageMessage ());
$zm = new Zend_Mail_Message_File(array('file' => $cliopt->filename));
$flags = $zm->getFlags();
$numberOfParts = $zm->countParts();
$this->view->multipart = $zm->isMultipart();
$parts = array();
if ($numberOfParts) {
for($i=0;$i<$numberOfParts;$i++)
$parts[] = $zm->getPart($i+1);
$this->view->parts = $parts;
}
$headers = $zm->getHeaders();
//print_r($headers);
foreach($headers as $key => $value)
//$headers[$key] = Zend_Mime_Decode::decodeQuotedPrintable($value, 0, 'utf-8');
if (strpos ($value, '=?UTF-8?Q?')) { // utf, quoted-printable
$value = preg_replace('/=\?UTF-8\?Q\?/', '', $value);
$value = preg_replace('/\?=$/', '', $value);
$headers[$key] = quoted_printable_decode($value);
}
//die(print_r($headers, true));
$this->view->headers = $headers;
$body_part = new stdClass();
$body_part->chartset = 'UTF-8';
if (array_key_exists('content-type', $headers)) {
if (preg_match('/charset=(.*?)$/', $headers['content-type'], $matches)) {
$body_part->charset = $matches[1];
}
}
$body_part->body = quoted_printable_decode($zm->getContent());
$this->view->body_part = $body_part;
$this->_helper->viewRenderer->setNoRender(false);
if ($cliopt->browser) {
// render view and open browser
$tmp_filename = '/tmp/mailpreview-'. basename($cliopt->filename).'.html';
$content = $this->view->render('index/mailpreview.phtml');
$fh = fopen($tmp_filename, 'w+');
fwrite($fh, $content);
fclose($fh);
//die(system('chrome '.$tmp_filename));
} else {
}
}
}
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $this->body_part->charset ?>" />
</head>
<style type="text/css">
#message_headers {
width: 100%;
padding: 10px 0 0 0;
margin: 0;
background: #fff;
font-size: 12px;
font-family: "Lucida Grande";
border-bottom: 1px solid #dedede;
overflow: hidden;
}
#message_headers dl {
float: left;
margin: 0 0 10px 0;
padding: 0;
}
#message_headers dt {
width: 80px;
padding: 1px;
float: left;
text-align: right;
font-weight: bold;
color: #7f7f7f;
}
#message_headers dd {
margin-left: 90px;
padding: 1px;
}
#message_headers p.alternate {
float: right;
margin: 0;
}
#message_headers p.alternate a {
color: #09c;
}
pre#message_body {
padding: 10px;
white-space: pre-wrap;
}
</style>
<div id="message_headers">
<dl>
<?php if (isset($this->headers['smtp-from']) && $this->headers['from'] != $this->headers['smtp-from']): ?>
<dt>SMTP-From:</dt>
<dd><?php echo htmlentities($this->headers['smtp-from']) ?></dd>
<?php endif; ?>
<dt>From:</dt>
<dd><?php echo htmlentities($this->headers['from']) ?></dd>
<?php if ($this->headers['reply-to']): ?>
<dt>Reply-To:</dt>
<dd><?php echo htmlentities($this->headers['reply-to']) ?></dd>
<?php endif; ?>
<dt>Subject:</dt>
<dd><strong><?php echo $this->headers['subject'] ?></strong></dd>
<dt>Date:</dt>
<dd><?php echo htmlentities($this->headers['date']) ?></dd>
<?php if (isset($this->headers['smtp-to']) && $this->headers['to'] != $this->headers['smtp-to']): ?>
<dt>SMTP-To:</dt>
<dd><?php echo htmlentities($this->headers['smtp-to']) ?></dd>
<?php endif ?>
<dt>To:</dt>
<dd><?php echo htmlentities($this->headers['to']) ?></dd>
</dl>
<?php if ($this->multipart): ?>
<p class="alternate">
<?php if ($this->headers['content-type'] && !preg_match('/text\/html/', $this->headers['content-type'])): ?>
<a href="#">View Text version</a>
<?php else: ?>
<a href="#">View HTML version</a>
<?php endif; ?>
</p>
<?php endif; ?>
</div>
<?php if ($this->headers['content-type'] && !preg_match('/text\/html/', $this->headers['content-type'])): ?>
<pre>
<?php echo $this->body_part->body; ?>
</pre>
<?php else: ?>
<pre id="message_body">
<?php echo $this->body_part->body; ?>
</pre>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment