Your IP : 216.73.216.240
<?php
/*
* @package Tech Fry Library
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*/
namespace TechFry\Library;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
// Class Email Reading using IMAP
class TfEmail
{
public static $options = array();
public static $mailbox;
public static $output = array();
public static function init($options = array())
{
// options - server, port, username, password, folder
self::$options = $options;
self::$mailbox = '{' . self::$options['server'] . ':' . self::$options['port'] . '/imap/ssl/novalidate-cert}';
}
public static function read_email($options = array(), $search_term = 'ALL', $n = 1, $to_delete = false)
{
self::init($options);
self::get_mailboxes();
// Connect to email server for specific mailbox
$imap = imap_open(self::$mailbox . self::$options['folder'], self::$options['username'], self::$options['password'])
or die('Cannot connect to email: ' . imap_last_error());
$mbox = imap_check($imap);
self::$output['total_messages'] = $mbox->Nmsgs;
$emails = imap_search($imap, $search_term);
$message_number = $emails[$n-1];
$headerinfo = (array) imap_headerinfo($imap, $message_number); // date, subject, from, to, reply_to, size
self::$output['message_number'] = trim($headerinfo['Msgno']);
self::$output['date'] = $headerinfo['date'];
self::$output['subject'] = $headerinfo['subject'];
self::$output['from_email'] = $headerinfo['from'][0]->mailbox . '@' . $headerinfo['from'][0]->host;
self::$output['from_name'] = $headerinfo['from'][0]->personal;
self::$output['from_host'] = $headerinfo['from'][0]->host;
self::$output['from_mailbox'] = $headerinfo['from'][0]->mailbox;
self::$output['to'] = (array) $headerinfo['to'][0];
self::$output['reply_to'] = (array) $headerinfo['reply_to'][0];
self::$output['udate'] = $headerinfo['udate'];
self::$output['size'] = $headerinfo['Size'];
$s = (array) imap_fetchstructure($imap, $message_number); // type, parts
//self::$output['s'] = $s;
self::$output['plain'] = '';
self::$output['html'] = '';
if (!$s['parts']) // Simple message
{
self::$output['html'] .= imap_body($imap, $message_number);
self::$output['plain'] .= imap_body($imap, $message_number);
}
else
{
$output['total_parts'] = \count($s['parts']);
$num = 0;
foreach ($s['parts'] as $k => $p)
{
self::get_part($imap, $message_number, $p, $k + 1);
}
}
if ($to_delete)
{
imap_delete($imap, $message_number);
imap_expunge($imap);
}
imap_close($imap);
return self::$output;
}
// Get all folders
public static function get_mailboxes($options = array())
{
if (!empty($options))
{
self::init($options);
}
$imap = imap_open(self::$mailbox, self::$options['username'], self::$options['password'])
or die('Cannot connect to email: ' . imap_last_error());
self::$output['folders'] = imap_list($imap, self::$mailbox, '*');
imap_close($imap);
return self::$output['folders'];
}
public static function get_part($imap, $message_number, $p, $part_n)
{
$body = imap_fetchbody($imap, $message_number, $part_n);
// 1. Decode data
if ($p->encoding == 3)
{
$body = imap_base64($body);
}
elseif ($p->encoding == 4)
{
$body = imap_qprint($body);
}
// 2. Get part parameters
if ($p->parameters)
{
foreach ($p->parameters as $x)
{
$params[strtolower($x->attribute)] = $x->value;
}
}
if ($p->dparameters)
{
foreach ($p->dparameters as $x)
{
$params[strtolower($x->attribute)] = $x->value;
}
}
// 3. Handle by part type
// Type 0 is text message; Type 1 is multipart; Type 5 is image
if ($p->type == 0 && strtolower($p->subtype) == 'plain')
{
self::$output['plain'] .= trim($body);
}
elseif ($p->type == 0 && strtolower($p->subtype) == 'html')
{
self::$output['html'] .= trim($body);
}
// 4. Attachments
if ($params['filename'] || $params['name'])
{
$num++;
$filename = ($params['filename']) ? $params['filename'] : $params['name'];
self::$output['attachments'][$num] = array('filename' => $filename, 'filesize' => $params['size'], 'body' => $body);
}
// 5. Subparts Recursion
if ($p->parts)
{
foreach ($p->parts as $k2 => $p2)
{
self::get_part($imap, $message_number, $p2, $part_n . '.' . ($k2 + 1));
}
}
}
}