<?php
/***************************************************************************************
* *
* This file is part of the XPertMailer package (http://xpertmailer.sourceforge.net/) *
* *
* XPertMailer is free software; you can redistribute it and/or modify it under the *
* terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 2 of the License, or (at your option) any later version. *
* *
* XPertMailer is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* XPertMailer; if not, write to the Free Software Foundation, Inc., 51 Franklin St, *
* Fifth Floor, Boston, MA 02110-1301 USA *
* *
* XPertMailer SMTP & POP3 PHP Mail Client. Can send and read messages in MIME Format. *
* Copyright (C) 2006 Tanase Laurentiu Iulian *
* *
***************************************************************************************/
/**
Read and show an e-mail using a POP3 Mail Server (Gmail on port 995 via SSL)
- read messages in MIME format (with HTML embedded images)
- download attachments
*/
// config --------
$read = true; // boolean true: read mail via pop3 account
//$read = 'mail.txt'; // OR string: file path & name to read mail from file
$cdir = 'attachments'; // directory where the attachments will be write/read, must have: read+write+delete permissions
$host = 'pop.gmail.com';
$user = 'hide@address.com';
$pass = 'password';
$port = 995;
$vssl = true; // boolean OR string: tls, ssl, sslv2, sslv3
// config --------
// HTML tags and properties banned from security reason, add more if you want
$btags = array('html', 'title', 'head', 'body', 'script', 'frame', 'iframe', 'embed', 'applet', 'object');
$bprop = array('onclick', 'ondbclick', 'onmouseover', 'onmouseout', 'onload', 'onunload');
$csep = '#TypE1~2DatA#';
$unique = 0;
if (isset($_GET['down'], $_GET['fl']) && ($_GET['down'] == 'inline' || $_GET['down'] == 'attachment') && trim($_GET['fl']) != '') {
$file = trim($_GET['fl']);
$exp1 = explode('/', $file);
$file = $exp1[count($exp1)-1];
$exp2 = explode('\\', $file);
$file = $exp1[count($exp2)-1];
$path = $cdir.'/'.$file;
if (file_exists($path) && is_readable($path)) {
$data = file_get_contents($path);
if (count($exp3 = explode($csep, $data)) == 2) {
$desc = strstr($file, '.') ? $_GET['down'].'; filename="'.$file.'"' : $_GET['down'];
header('Content-Type: '.$exp3[0]);
header('Content-Length: '.strlen($exp3[1]));
header('Content-Disposition: '.$desc);
echo $exp3[1];
} else echo 'Error 2: can\'t read !';
} else echo 'Error 1: can\'t read !';
exit;
}
// path to XPM3_POP3.php from XPM3 package
require_once '../XPM3_POP3.php';
if (is_bool($read) && $read) {
$conn = XPM3_POP3::Connect($host, $user, $pass, $port, $vssl) or die('Can not connect !');
// get the last 1 mail
if ($recv = XPM3_POP3::Receive($conn, -1)) {
list($arr['number'], $arr['data']) = each($recv);
$data = $arr['data'];
XPM3_POP3::pQuit($conn);
} else {
XPM3_POP3::pQuit($conn);
die('MailBox is empty !');
}
} else if(is_string($read)) {
if (is_file($read) && is_readable($read)) $data = file_get_contents($read);
else die('Invalid config $read value !');
} else die('Invalid config $read type !');
$split = XPM3_MIME::split_content($data) or die('Invalid mail format !');
$show = array();
foreach ($split['header'] as $harr) {
foreach ($harr as $hname => $hvalue) {
$cname = strtolower($hname);
$hdecv = XPM3_MIME::decode_header($hvalue);
if ($cname == 'subject') $show['Subject'] = $hdecv['value'];
else if ($cname == 'from') $show['From'] = $hdecv['value'];
else if ($cname == 'to') $show['To'] = $hdecv['value'];
else if ($cname == 'date') $show['Date'] = $hdecv['value'];
}
}
$info = '';
if (count($show) > 0) {
foreach ($show as $hnam => $hval) $info .= '<b>'.$hnam.'</b>: '.htmlentities($hval).'<br>'."\n";
}
function tag_ban($html, $tags, $prop) {
$tfind1 = $trepl1 = $tfind2 = $trepl2 = $pfind = $prepl = array();
foreach ($tags as $tname) {
$tfind1[] = '<'.$tname;
$trepl1[] = '<X'.$tname;
$tfind2[] = '</'.$tname;
$trepl2[] = '</X'.$tname;
}
foreach ($prop as $pname) {
$pfind[] = ' '.$pname.'=';
$prepl[] = ' X'.$pname.'=';
}
$html = str_ireplace($tfind1, $trepl1, $html);
$html = str_ireplace($tfind2, $trepl2, $html);
return str_ireplace($pfind, $prepl, $html);
}
function save_file($file, $data) {
$open = fopen($file, 'w');
fwrite($open, $data);
fclose($open);
}
function size_read($size) {
$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
$ii = count($sizes) - 1;
$i = 0;
while ($size >= 1000 && $i < $ii) {
$size /= 1000;
$i++;
}
return sprintf('%01.2f %s', $size, $sizes[$i]);
}
function put_embed($html, $src) {
$find1 = $repl1 = $find2 = $repl2 = array();
foreach($src as $rimg){
if ($rimg['type'] == 'cid') {
$find1[] = ' src="cid:'.$rimg['value'].'"';
$find2[] = ' src=cid:'.$rimg['value'];
$repl1[] = ' src="?down=inline&fl='.$rimg['value'].'"';
$repl2[] = ' src="?down=inline&fl='.$rimg['value'].'"';
} else if($rimg['type'] == 'name') {
$find1[] = ' src="'.$rimg['value'].'"';
$find2[] = ' src='.$rimg['value'];
$repl1[] = ' src="?down=inline&fl='.$rimg['value'].'"';
$repl2[] = ' src="?down=inline&fl='.$rimg['value'].'"';
}
}
$html = str_ireplace($find1, $repl1, $html);
return str_ireplace($find2, $repl2, $html);
}
$content = '';
if ($split['multipart'] == 'no') {
foreach ($split['body'] as $barr) {
$ishtml = $encode = $data = false;
foreach ($barr as $bname => $bvalue) {
$sname = strtolower($bname);
if ($sname == 'content-type') {
if (substr($bvalue, 0, 9) == 'text/html') $ishtml = true;
} else if ($sname == 'content-transfer-encoding') $encode = $bvalue;
}
if (isset($barr['data'])) {
$content = $encode ? XPM3_MIME::decode_content($barr['data'], $encode) : $barr['data'];
$content = $ishtml ? tag_ban($content, $btags, $bprop) : nl2br(htmlentities($content));
}
}
} else if ($split['multipart'] == 'yes') {
$part = $attid = array();
foreach ($split['body'] as $barr) {
$ctype = $encode = $disp = $fname = $cid = false;
foreach ($barr as $bname => $bvalue) {
$sname = strtolower($bname);
if ($sname == 'content-type'){
$exp = explode('; ', $bvalue);
$ctype = strtolower($exp[0]);
} else if ($sname == 'content-transfer-encoding') {
$encode = $bvalue;
} else if ($sname == 'content-disposition') {
$exp = explode(';', $bvalue);
$val = trim(strtolower($exp[0]));
if ($val == 'inline' || $val == 'attachment') {
$disp = $val;
if (count($exp) > 1) {
foreach ($exp as $ifile) if (substr($ifile, 0, strlen('filename=')) == 'filename=') $fname = trim(substr($ifile, strlen('filename=')), '"');
}
}
} else if ($sname == 'content-id') $cid = trim($bvalue, '<>');
}
if (isset($barr['data'])) {
$decode = $encode ? XPM3_MIME::decode_content($barr['data'], $encode) : $barr['data'];
$part[] = array('multipart' => $barr['multipart'], 'type' => $ctype, 'disp' => $disp, 'name' => $fname, 'cid' => $cid, 'data' => $decode);
}
}
if (count($part) > 0) {
$mixed = $related = $alternative = false;
foreach ($part as $carr) {
if (substr($carr['multipart'], -strlen('alternative')) == 'alternative') {
if ($carr['type']) {
if ($carr['type'] == 'text/plain') $alternative['text'] = $carr['data'];
else if ($carr['type'] == 'text/html') $alternative['html'] = $carr['data'];
}
} else if (substr($carr['multipart'], -strlen('related')) == 'related') {
if ($carr['type']) {
if ($carr['type'] == 'text/plain') $related['text'] = $carr['data'];
else if ($carr['type'] == 'text/html') $related['html'] = $carr['data'];
else if ($carr['disp'] && $carr['disp'] == 'inline' && substr($carr['type'], 0, strlen('image/')) == 'image/') {
if ($carr['cid']) {
$related['image'][] = array('type' => 'cid', 'value' => $carr['cid']);
save_file($cdir.'/'.$carr['cid'], $carr['type'].$csep.$carr['data']);
} else if($carr['name']) {
$related['image'][] = array('type' => 'name', 'value' => $carr['name']);
save_file($cdir.'/'.$carr['name'], $carr['type'].$csep.$carr['data']);
}
}
}
} else if (substr($carr['multipart'], -strlen('mixed')) == 'mixed') {
if ($carr['type'] && $carr['type'] == 'text/plain') $mixed['text'] = $carr['data'];
else if ($carr['type'] && $carr['type'] == 'text/html') $mixed['html'] = $carr['data'];
else {
$ctype = $carr['type'] ? $carr['type'] : 'application/octet-stream';
$cname = $carr['name'] ? $carr['name'] : XPM3_MIME::unique($unique++);
$cdisp = $carr['disp'] ? $carr['disp'] : 'inline';
$mixed['attach'][] = array('type' => $ctype, 'name' => $cname, 'size' => strlen($carr['data']));
save_file($cdir.'/'.$cname, $ctype.$csep.$carr['data']);
}
}
}
if ($alternative) {
if (isset($alternative['html'])) {
if($related && isset($related['image'])) $alternative['html'] = put_embed($alternative['html'], $related['image']);
$content .= tag_ban($alternative['html'], $btags, $bprop);
} else if (isset($alternative['text'])) $content .= nl2br(htmlentities($alternative['text']));
}
if ($related) {
if ($content == '') {
if (isset($related['html'])) {
if (isset($related['image'])) $related['html'] = put_embed($related['html'], $related['image']);
$content .= tag_ban($related['html'], $btags, $bprop);
} else if(isset($related['text'])) $content .= nl2br(htmlentities($related['text']));
}
}
if ($mixed) {
if ($content == '') {
if (isset($mixed['html'])) $content .= tag_ban($mixed['html'], $btags, $bprop);
if (isset($mixed['text'])) {
if ($content != '') $content .= '<hr>'."\n";
$content .= nl2br(htmlentities($mixed['text']));
}
} else {
if (isset($mixed['html'])) {
$mixed['attach'][] = array('type' => 'text/html', 'name' => 'mixed.html', 'size' => strlen($mixed['html']));
save_file($cdir.'/mixed.html', 'text/html'.$csep.$mixed['html']);
}
if (isset($mixed['text'])) {
$mixed['attach'][] = array('type' => 'text/plain', 'name' => 'mixed.txt', 'size' => strlen($mixed['text']));
save_file($cdir.'/mixed.txt', 'text/plain'.$csep.$mixed['text']);
}
}
if (isset($mixed['attach'])) {
$detatt = array();
foreach ($mixed['attach'] as $dattch) {
$descatth = strstr($dattch['name'], '.') ? $dattch['name'] : $dattch['type'];
$detatt[] = '<a style="font-family: Verdana;font-size: 10pt" href="?down=attachment&fl='.$dattch['name'].'">'.$descatth.'</a> [ '.size_read($dattch['size']).' ]';
}
$info .= '<b>Attachments:</b> '.implode(" | \n", $detatt).'<br>'."\n";
}
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>XPM3 - POP3 E-Mail Reader</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<font style="font-family: Verdana;font-size: 10pt">
<?php echo $info; ?>
</font>
<hr color="gray" size="3">
<?php echo $content; ?>
</body>
</html>