'SSL'
* or
* 'ssl' => 'TLS'
* if you want to use ssl support.
*
* Because of problems with Windows filenames (maildir needs : in filenames) the maildir folder is in a tar.
* Untar maildir.tar in maildir/ to test maildir support (won't work on Windows).
*
* The structure of the class is very simple. Every method named show...() output HTML, run() inits mail storage
* after login and calls a show method, everything else inits and checks variables and mail storage handler.
*
* @category Zend
* @package Zend_Mail
* @subpackage Demos
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Demo_Zend_Mail_SimpleMailer
{
/**
* Mail storage type (mbox, mbox-folder, maildir, maildir-folder, pop3, imap)
*
* @var string
*/
private $type;
/**
* Filename, dirname or hostname for current mailstorage
*
* @var string
*/
private $param;
/**
* Selected mail message or null if none
*
* @var integer
*/
private $messageNum;
/**
* Mail storage handler
*
* @var Zend_Mail_Storage
*/
private $mail;
/**
* Query string with current selection for output
*
* @var string
*/
private $queryString;
/**
* Don't run run(), needed for auth
*
* @var boolean
*/
private $noRun = false;
/**
* Init class for run() and output
*
* @return void
*/
function __construct()
{
$this->initVars();
$this->loadClasses();
$this->whitelistParam();
// we use http auth for username and password or mail storage
if (($this->type == 'pop3' || $this->type == 'imap') && !isset($_SERVER['PHP_AUTH_USER'])) {
$this->needAuth();
return;
}
switch ($this->type) {
case 'mbox':
$this->mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->param));
break;
case 'mbox-folder':
$this->mail = new Zend_Mail_Storage_Folder_Mbox(array('dirname' => $this->param));
break;
case 'maildir':
$this->mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->param));
break;
case 'maildir-folder':
$this->mail = new Zend_Mail_Storage_Folder_Maildir(array('dirname' => $this->param));
break;
case 'pop3':
$this->mail = new Zend_Mail_Storage_Pop3(array('host' => $this->param,
'user' => $_SERVER['PHP_AUTH_USER'],
'password' => $_SERVER['PHP_AUTH_PW']));
break;
case 'imap':
$this->mail = new Zend_Mail_Storage_Imap(array('host' => $this->param,
'user' => $_SERVER['PHP_AUTH_USER'],
'password' => $_SERVER['PHP_AUTH_PW']));
break;
default:
$this->mail = null;
break;
}
}
/**
* Check parameter and type
*
* @return void
*/
function whitelistParam()
{
$whitelist = array('mbox' => array('mbox/INBOX', 'mbox/subfolder/test'),
'mbox-folder' => array('mbox'),
'maildir' => array('maildir', 'maildir/.subfolder', 'maildir/.subfolder.test'),
'maildir-folder' => array('maildir', 'maildir/.subfolder', 'maildir/.subfolder.test'),
'pop3' => array(),
'imap' => array());
if ($this->type === null || @$whitelist[$this->type] === array() || @in_array($this->param, $whitelist[$this->type])) {
return;
}
throw new Exception('Unknown type or param not in whitelist');
}
/**
* Load needed classes
*
* @return void
*/
function loadClasses()
{
$classname = array('mbox' => 'Zend_Mail_Storage_Mbox',
'mbox-folder' => 'Zend_Mail_Storage_Folder_Mbox',
'maildir' => 'Zend_Mail_Storage_Maildir',
'maildir-folder' => 'Zend_Mail_Storage_Folder_Maildir',
'pop3' => 'Zend_Mail_Storage_Pop3',
'imap' => 'Zend_Mail_Storage_Imap');
if (isset($classname[$this->type])) {
Zend_Loader::loadClass($classname[$this->type]);
}
Zend_Loader::loadClass('Zend_Mail_Storage');
}
/**
* Init variables
*
* @return void
*/
function initVars()
{
$this->type = isset($_GET['type']) ? $_GET['type'] : null;
$this->param = isset($_GET['param']) ? $_GET['param'] : null;
$this->folder = isset($_GET['folder']) ? $_GET['folder'] : null;
$this->messageNum = isset($_GET['message']) && is_numeric($_GET['message']) ? $_GET['message'] : null;
$this->queryString = http_build_query(array('type' => $this->type,
'param' => $this->param,
'folder' => $this->folder));
}
/**
* Send http auth headers, for username and password in pop3 and imap
*
* @return void
*/
function needAuth()
{
header("WWW-Authenticate: Basic realm='{$this->type} credentials'");
header('HTTP/1.0 401 Please enter credentials');
$this->noRun = true;
}
/**
* Get data from mail storage and output html
*
* @return void
*/
function run()
{
if ($this->noRun) {
return;
}
if ($this->mail instanceof Zend_Mail_Storage_Folder_Interface && $this->folder) {
// could also be done in constructor of $this->mail with parameter 'folder' => '...'
$this->mail->selectFolder($this->folder);
}
$message = null;
try {
if ($this->messageNum) {
$message = $this->mail->getMessage($this->messageNum);
}
} catch(Zend_Mail_Exception $e) {
// ignored, $message is still null and we display the list
}
if (!$this->mail) {
$this->showChooseType();
} else if ($message) {
$this->showMessage($message);
} else {
$this->showList();
}
}
/**
* Output html header
*
* @param string $title page title
* @return void
*/
function showHeader($title)
{
echo "
{$title}
{$title}
";
}
/**
* Output html footer
*
* @return void
*/
function showFooter()
{
echo '';
}
/**
* Output type selection AKA "login-form"
*
* @return void
*/
function showChooseType()
{
$this->showHeader('Choose Type');
echo '
';
$this->showFooter();
}
/**
* Output mail message
*
* @return void
*/
function showMessage($message)
{
try {
$from = $message->from;
} catch(Zend_Mail_Exception $e) {
$from = '(unknown)';
}
try {
$to = $message->to;
} catch(Zend_Mail_Exception $e) {
$to = '(unknown)';
}
try {
$subject = $message->subject;
} catch(Zend_Mail_Exception $e) {
$subject = '(unknown)';
}
$this->showHeader($subject);
echo "
From: | $from |
Subject: | $subject |
To: | $to |
---|
";
if ($message->isMultipart()) {
echo '';
foreach (new RecursiveIteratorIterator($message) as $part) {
echo "- Part with type {$part->contentType}:
- ";
echo htmlentities($part);
echo '
';
}
echo ' ';
} else {
echo htmlentities($message->getContent());
}
echo " |
back to list";
if ($this->messageNum > 1) {
echo " - queryString}&message=", $this->messageNum - 1, '">prev';
}
if ($this->messageNum < $this->mail->countMessages()) {
echo " - queryString}&message=", $this->messageNum + 1, '">next';
}
$this->showFooter();
}
/**
* Output message list
*
* @return void
*/
function showList()
{
$this->showHeader('Overview');
echo ' | From | To | Subject |
';
foreach ($this->mail as $num => $message) {
if ($this->mail->hasFlags) {
$class = array();
if ($message->hasFlag(Zend_Mail_Storage::FLAG_RECENT)) {
$class['unread'] = 'unread';
$class['new'] = 'new';
}
if (!$message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)) {
$class['unread'] = 'unread';
}
if ($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED)) {
$class['flagged'] = 'flagged';
}
$class = implode(' ', $class);
echo "";
} else {
echo '
';
}
echo "read | ";
try {
echo "{$message->from} | {$message->to} | {$message->subject} | ";
} catch(Zend_Mail_Exception $e){
echo 'error | ';
}
echo '
';
}
echo '
';
if ($this->mail instanceof Zend_Mail_Storage_Folder_Interface) {
$this->showFolders();
}
$this->showFooter();
}
/**
* Output folder list
*
* @return void
*/
function showFolders()
{
echo "
";
}
}
// init and run mailer
$SimpleMailer = new Demo_Zend_Mail_SimpleMailer();
$SimpleMailer->run();