| Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/ |
| Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/TEmail.php |
<?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 for accessing and managing email using IMAP (Everything is synced)
class TEmail
{
public $options = [];
public $server;
public $port;
public $username;
public $password;
public $folder;
public $mailbox;
public $imap;
public $output = [];
public function __construct($options = [])
{
// options - server, port, username, password, folder
$this->server = $options['server'] ?? $options['email_server'];
$this->port = $options['port'] ?? $options['port'] ?? 993;
$this->username = $options['username'] ?? $options['email_username'];
$this->password = $options['password'] ?? $options['email_password'];
$this->folder = $options['folder'] ?? $options['email_folder'] ?? 'INBOX';
$this->options = $options;
$this->mailbox = '{' . $this->server . ':' . $this->port . '/imap/ssl/novalidate-cert}';
$this->connect();
}
// Connect to email server for specific mailbox or folder
public function connect()
{
$this->imap = imap_open($this->mailbox . $this->folder, $this->username, $this->password)
or die('Cannot connect to email: ' . imap_last_error());
}
public function get_emails($criteria = 'ALL')
{
$email_data = [];
$emails = imap_search($this->imap, $criteria);
// Array of email or message numbers
return $emails;
}
public function get_email($email_number)
{
$overview = imap_fetch_overview($this->imap, $email_number);
if (!$overview)
{
return null;
}
$email_data = (array) $overview[0];
$header = imap_headerinfo($this->imap, $email_number); // from, to, reply_to, sender
$from = $header->from[0];
$to = $header->to[0];
$reply_to = isset($header->reply_to[0]) ? $header->reply_to[0] : null;
$sender = $header->sender[0];
$email_data['to_email'] = $to->mailbox . "@" . $to->host;
$email_data['from_email'] = $from->mailbox . '@' . $from->host;
$email_data['sender_email'] = $sender->mailbox . '@' . $sender->host;
$email_data['reply_to_email'] = $reply_to ? $reply_to->mailbox . "@" . $reply_to->host : '';
$structure = imap_fetchstructure($this->imap, $email_number);
$email_data['plain'] = '';
$email_data['html'] = '';
// Simple email and not multi-part
if (!isset($structure->parts))
{
$body = imap_fetchbody($this->imap, $email_number, 1);
$encoding = $structure->encoding;
$decoded = $this->decode_message($body, $encoding);
if (strtolower($structure->subtype) == 'plain')
{
$email_data['plain'] = $decoded;
}
else
{
$email_data['html'] = htmlspecialchars($decoded);
}
}
else
{
// Multipart - loop all parts
foreach ($structure->parts as $i => $part)
{
$part_number = $i + 1;
$body = imap_fetchbody($this->imap, $email_number, $part_number);
$decoded = $this->decode_message($body, $part->encoding);
$subtype = strtolower($part->subtype ?? '');
if ($subtype === 'plain' && !$email_data['plain'])
{
$email_data['plain'] = $decoded;
}
elseif ($subtype === 'html' && !$email_data['html'])
{
$email_data['html'] = htmlspecialchars($decoded);
}
// Attachments
if (!empty($part->dparameters))
{
foreach ($part->dparameters as $param)
{
if (strtolower($param->attribute) == 'filename')
{
$filename = $param->value;
$email_data['attachments'][] = array(
'filename' => $filename,
'mime' => $subtype,
'content' => $decoded,
);
}
}
}
if (!empty($part->parameters))
{
foreach ($part->parameters as $param)
{
if (strtolower($param->attribute) == 'name')
{
$filename = $param->value;
$email_data['attachments'][] = array(
'filename' => $filename,
'mime' => $subtype,
'content' => $decoded,
);
}
}
}
}
}
return $email_data;
}
public function mark_as_read($email_number)
{
imap_setflag_full($this->imap, $email_number, "\\Seen");
}
public function delete_email($email_number)
{
imap_delete($this->imap, $email_number);
}
public function close()
{
imap_close($this->imap);
}
public function get_total_messages()
{
return imap_num_msg($this->imap);
}
// Get all folders
public function get_mailboxes()
{
$folders = imap_list($this->imap, $this->mailbox, '*');
$cleaned = [];
foreach ($folders as $folder)
{
// Remove the server prefix
$cleaned[] = str_replace($this->mailbox, '', $folder);
}
return $cleaned;
}
public function decode_message($body, $encoding)
{
switch ($encoding)
{
case 0:
return $body; // 7BIT
case 1:
return imap_8bit($body);
case 2:
return imap_binary($body);
case 3:
return base64_decode($body);
case 4:
return quoted_printable_decode($body);
case 5:
return $body; // OTHER
default:
return $body;
}
}
}