Skip to content

Instantly share code, notes, and snippets.

@wilcorrea
Last active December 7, 2016 18:27
Show Gist options
  • Save wilcorrea/9e656ce60d673ddcf51bc8207c781e21 to your computer and use it in GitHub Desktop.
Save wilcorrea/9e656ce60d673ddcf51bc8207c781e21 to your computer and use it in GitHub Desktop.
<?php
namespace Core\Mail;
/**
* Class Imap
* @package Core\Mail
*/
class Imap
{
/**
* @var string
*/
private $hostname = '';
/**
* @var string
*/
private $username = '';
/**
* @var string
*/
private $password = '';
/**
* @var resource
*/
private $connection = null;
/**
* Imap constructor.
* @param $hostname
* @param $username
* @param $password
* @param array $options
*/
public function __construct($hostname, $username, $password, $options = ["port" => 143, "settings" => array("notls", "novalidate-cert"), "tagged" => "INBOX"])
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
if (is_array($options)) {
if (isset($options['port'])) {
$this->hostname = $this->hostname . ":" . $options['port'];
}
if (isset($options['settings'])) {
$settings = $options['settings'];
if (is_array($settings)) {
foreach ($settings as $value) {
$this->hostname = $this->hostname . "/" . $value;
}
}
}
$this->hostname = "{" . $this->hostname . "}";
if (isset($options['tagged'])) {
$this->hostname = $this->hostname . $options['tagged'];
}
}
if (!function_exists('imap_open')) {
print 'Error: imap not installed' . PHP_EOL;
}
}
/**
* @return null|resource
*/
public function connect()
{
if ($this->connection === null) {
$this->connection = imap_open($this->hostname, $this->username, $this->password) or die('Cannot connect to host: ' . imap_last_error());
}
return $this->connection;
}
/**
* @return null
*/
public function disconnect()
{
if ($this->connection) {
imap_close($this->connection);
}
return $this->connection = null;
}
/**
* @return int
*/
public function getCountMessages()
{
$countMessages = 0;
if ($this->connection) {
$countMessages = imap_num_msg($this->connection);
}
return $countMessages;
}
/**
* @param int $items
* @return array
*/
public function getMessages($items = 20)
{
$messages = [];
$countMessages = $this->getCountMessages();
$limit = $countMessages - $items;
if ($countMessages < $items) {
$limit = 0;
}
for ($i = $countMessages; $i > $limit; $i--) {
$header = imap_header($this->connection, $i);
$toInfo = (isset($header->to)) ? $header->to[0] : null;
$fromInfo = $header->from[0];
$replyInfo = $header->reply_to[0];
$uid = imap_uid($this->connection, $i);
/** @noinspection SpellCheckingInspection */
$message = [
"id" => $i,
"to" => (isset($toInfo->mailbox) && isset($toInfo->host)) ? $toInfo->mailbox : "",
"toAddr" => (isset($toInfo->mailbox) && isset($toInfo->host)) ? $toInfo->mailbox . "@" . $fromInfo->host : "",
"fromAddr" => (isset($fromInfo->mailbox) && isset($fromInfo->host)) ? $fromInfo->mailbox . "@" . $fromInfo->host : "",
"fromName" => (isset($fromInfo->personal)) ? $fromInfo->personal : "",
"replyAddr" => (isset($replyInfo->mailbox) && isset($replyInfo->host)) ? $replyInfo->mailbox . "@" . $replyInfo->host : "",
"replyName" => (isset($replyInfo->personal)) ? $replyInfo->personal : "",
"subject" => (isset($header->subject)) ? $header->subject : "",
"udate" => (isset($header->udate)) ? $header->udate : "",
"body" => $this->getBody($uid, $this->connection)
];
$messages[$i] = $message;
}
return $messages;
}
/**
* @param $id
* @return bool
*/
public function deleteMessage($id)
{
return imap_delete($this->connection, $id);
}
/**
* @return bool
*/
public function commit()
{
return imap_expunge($this->connection);
}
/**
* @param $uid
* @param $imap
* @return bool
*/
private function getBody($uid, $imap)
{
$body = $this->getPart($imap, $uid, "TEXT/HTML");
// if HTML body is empty, try getting text body
if ($body == "") {
$body = $this->getPart($imap, $uid, "TEXT/PLAIN");
}
return $body;
}
/**
* @param $imap
* @param $uid
* @param $mimeType
* @param bool $structure
* @param bool $partNumber
* @return bool|string
*/
private function getPart($imap, $uid, $mimeType, $structure = false, $partNumber = false)
{
if (!$structure) {
$structure = imap_fetchstructure($imap, $uid, FT_UID);
}
if ($structure) {
if ($mimeType == $this->getMimeType($structure)) {
if (!$partNumber) {
$partNumber = 1;
}
$text = imap_fetchbody($imap, $uid, $partNumber, FT_UID);
switch ($structure->encoding) {
case 3:
return imap_base64($text);
case 4:
return imap_qprint($text);
default:
return $text;
}
}
// multipart
if ($structure->type == 1) {
foreach ($structure->parts as $index => $subStruct) {
$prefix = "";
if ($partNumber) {
$prefix = $partNumber . ".";
}
$data = $this->getPart($imap, $uid, $mimeType, $subStruct, $prefix . ($index + 1));
if ($data) {
return $data;
}
}
}
}
return false;
}
/**
* @param $structure
* @return string
*/
private function getMimeType($structure)
{
$primaryMimetype = ["TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER"];
if ($structure->subtype) {
return $primaryMimetype[(int)$structure->type] . "/" . $structure->subtype;
}
return "TEXT/PLAIN";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment