Skip to content

Instantly share code, notes, and snippets.

@saltandvinegarcrisps
Last active October 25, 2016 16:16
Show Gist options
  • Save saltandvinegarcrisps/dafa201fd731f8474367fb6fa2c705fb to your computer and use it in GitHub Desktop.
Save saltandvinegarcrisps/dafa201fd731f8474367fb6fa2c705fb to your computer and use it in GitHub Desktop.
php imap wrapper
<?php
class Imap {
protected $dns;
protected $user;
protected $pass;
protected $options;
protected $retries;
protected $params;
protected $session;
public function __construct($dns, $user, $pass, $options = 0, $retries = 0, array $params = []) {
$this->dns = $dns;
$this->user = $user;
$this->pass = $pass;
$this->options = $options;
$this->retries = $retries;
$this->params = $params;
}
public function connect() {
if( ! $this->session = imap_open($this->dns, $this->user, $this->pass, $this->options, $this->retries, $this->params)) {
throw new \RuntimeException('Failed to connect to imap server: '.imap_last_error());
}
}
public function disconnect() {
imap_close($this->session);
}
public function check() {
return imap_check($this->session);
}
public function status($options = SA_ALL) {
return imap_status($this->session, $this->dns, $options);
}
public function info() {
return imap_mailboxmsginfo($this->session);
}
public function folders($pattern = '*') {
return imap_list($this->session, $this->dns, $pattern);
}
public function count() {
return imap_num_msg($this->session);
}
public function get($sequence, $options = 0) {
return imap_fetch_overview($this->session, $sequence, $options);
}
public function setFlag(array $mailsIds, $flag) {
return imap_setflag_full($this->session, implode(',', $mailsIds), $flag, ST_UID);
}
public function clearFlag(array $mailsIds, $flag) {
return imap_clearflag_full($this->session, implode(',', $mailsIds), $flag, ST_UID);
}
public function markAsRead(array $mailIds) {
return $this->setFlag($mailIds, '\\Seen');
}
public function markAsUnread(array $mailIds) {
return $this->clearFlag($mailIds, '\\Seen');
}
public function fetchStructure($id) {
return imap_fetchstructure($this->session, $id, FT_UID);
}
public function fetchHeader($id) {
return imap_rfc822_parse_headers(imap_fetchheader($this->session, $id, FT_UID));
}
public function fetchBodyPart($id, $part, $options = FT_UID) {
return imap_fetchbody($this->session, $id, $part, $options);
}
public function fetchBody($id, $options = FT_UID) {
return imap_body($this->session, $id, $options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment