Skip to content

Instantly share code, notes, and snippets.

@DasLampe
Last active March 5, 2024 06:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DasLampe/4774506 to your computer and use it in GitHub Desktop.
Save DasLampe/4774506 to your computer and use it in GitHub Desktop.
A small IMAP class to connect via fsockopen with SSL. Helpful for e.g. gmail.com. (Be careful! Scripted in 2010)
<?php
// +----------------------------------------------------------------------+
// | Copyright (c) 2010 DasLampe <daslampe@lano-crew.org> |
// | Encoding: UTF-8 |
// +----------------------------------------------------------------------+
class IMAP
{
var $sock;
var $host;
var $port;
var $sid;
var $tag;
var $_lastTag;
var $currentMailbox;
function __construct()
{
$this->sock = null;
$this->host = "localhost";
$this->port = "143";
$this->sid = "";
$this->tag = 0;
$this->_lastTag = $this->tag;
$this->currentMailbox = "";
}
function __destruct()
{
fwrite($this->sock, "A000".$this->getTag()." LOGOUT\r\n");
}
function connect($host="", $port="", $ssl=false)
{
$this->host = $host;
$this->port = $port;
if($ssl == true)
{
$this->host = "ssl://".$host;
}
$this->sock = fsockopen($this->host, $this->port, $errno, $errstr);
if(!$this->_assumedNextLine('* OK'))
{
echo 'FAIL CONNECT';
}
}
function getTag()
{
$this->tag = $this->tag +1;
$this->_lastTag = "A000".$this->tag;
return $this->tag;
}
function login($username, $pass)
{
fwrite($this->sock, "A000".$this->getTag()." LOGIN ".$username." ".$pass."\r\n");
if(!$this->_assumedNextLine($this->_lastTag))
{
}
}
function selectMailBox($mailbox)
{
fwrite($this->sock, "A000".$this->getTag()." SELECT ".$mailbox."\r\n");
$this->currentMailbox = $mailbox;
$this->readResponse($this->_lastTag);
}
function countMessages()
{
fwrite($this->sock, "A000".$this->getTag()." SEARCH ALL\r\n");
$response = $this->readResponse($this->_lastTag);
foreach($response as $id)
{
if($id[0] == "S")
{
$count = count_chars($id, 1);
}
}
return $count[32];
}
public function getSubject($msgId)
{
if(empty($msgId))
{
return false;
}
$this->fetch($msgId." BODY[HEADER.FIELDS (subject)]");
$array = array();
while(!$this->readLine($tokens, $this->_lastTag))
{
if(!empty($tokens) &amp;&amp; preg_match("/FETCH/", $tokens))
{
$msgId = preg_split("/ /", $tokens, 2);
$msgId = $msgId[0];
}
if(!empty($tokens) &amp;&amp; !preg_match("/FETCH/", $tokens))
{
$array[] = array("id" =&gt; $msgId,
"subject" =&gt; $tokens);
}
}
return mb_decode_mimeheader($array[0]['subject']);
}
public function getMsgText($msgId="")
{
if(empty($msgId))
{
return false;
}
$this->fetch($msgId." BODY[TEXT]");
$array = array();
$array[] = "=?ISO-8859-1?Q?";
while(!$this->readLine($tokens, $this->_lastTag, $tag))
{
$array[] .= $tag.' '.$tokens;
}
//Löschen der 1. und letzten Response
unset($array[1]); //FETCH
unset($array[count($array)]); //Klammer
$array = array_values($array);
//Array2String
$array = implode('', $array);
//Decodieren und Zeilenumbrüche setzten
return nl2br(mb_decode_mimeheader($array));
}
public function getFromOrTo($msgId="", $fromOrTo="")
{
if(empty($msgId) || empty($fromOrTo))
{
return false;
}
$this->fetch($msgId." BODY[HEADER.FIElDS (".$fromOrTo.")]");
while(!$this->readLine($tokens, $this->_lastTag))
{
if(!isset($return) &amp;&amp; !preg_match('/FETCH/', $tokens))
{
$return = htmlspecialchars($tokens);
}
}
return mb_decode_mimeheader($return);
}
private function fetch($command, $param=false)
{
if(!$param)
{
fwrite($this->sock, "A000".$this->getTag()." FETCH ".$command."\r\n");
}
else
{
fwrite($this->sock, "A000".$this->getTag()." FETCH ".$command." ".$param."\r\n");
}
}
function _nextLine()
{
$line = @fgets($this->sock);
if ($line === false)
{
echo 'FAIL!!';
return false;
}
return $line;
}
protected function _assumedNextLine($start)
{
$line = $this->_nextLine();
return strpos($line, $start) === 0;
}
public function readLine(&amp;$tokens = array(), $wantedTag = '*', &amp;$tag="")
{
$line = $this->_nextTaggedLine($tag);
$tokens = $line;
$tag = $tag;
if(($tag == "A000".$this->tag) || ($tag == $wantedTag))
{
return $tag;
}
}
public function readResponse($tag="*")
{
if($tag == "lastTag")
{
$tag = $this->_lastTag;
}
$lines = array();
while (!$this->readLine($tokens, $tag)) {
$lines[] = $tokens;
}
return $lines;
}
protected function _nextTaggedLine(&amp;$tag)
{
$line = $this->_nextLine();
// seperate tag from line
@list($tag, $line) = explode(' ', $line, 2);
return $line;
}
}
@duanhdan
Copy link

I have some IMAP server like that:
$mail_config['googlemail.com'] = array("imap", "imap.googlemail.com", "993", "ssl");
$mail_config['gmail.com'] = array("imap", "imap.googlemail.com", "993", "ssl");
$mail_config['wildblue.net'] = array("imap", "imap.googlemail.com", "993", "ssl");
$mail_config['svcable.net'] = array("imap", "imap.googlemail.com", "993", "ssl");
$mail_config['aol.com'] = array("imap", "imap.aol.com", "993", "ssl");
$mail_config['aim.com'] = array("imap", "imap.aol.com", "993", "ssl");
$mail_config['yahoo.com'] = array("imap", "imap.mail.yahoo.com", "993", "ssl");
$mail_config['btinternet.com'] = array("imap", "imap.mail.yahoo.com", "993", "ssl");
$mail_config['ymail.com'] = array("imap", "imap.mail.yahoo.com", "995", "ssl");
$mail_config['rocketmail.com'] = array("imap", "imap.mail.yahoo.com", "995", "ssl");
Could you show me how I could connect to an IMAP server, I do that but It isn't ok:
$a = new IMAP();
$a->connect("imap.mail.yahoo.com", "993", true);
$a->login("name@yahoo.com", "pass");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment