Skip to content

Instantly share code, notes, and snippets.

@Abban
Last active February 26, 2022 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Abban/4504349 to your computer and use it in GitHub Desktop.
Save Abban/4504349 to your computer and use it in GitHub Desktop.
Library for importing from IMAP and parsing the message.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Get Mail
*
* IMAP library for CodeIgniter. Requires Datamapper (http://datamapper.wanwizard.eu/)
*
* @package Get Mail
* @author Web Together (http://webtogether.ie)
* @version 0.9
* @license MIT License Copyright (c) 2008 Erick Hartanto
*/
class Get_mail{
private $_debug = false;
private $_arrErrors = array();
private $hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
private $username = 'username';
private $password = 'password';
private $archive = true;
private $archive_folder = '[Gmail]/All Mail';
private $inbox;
public function __construct(){}
/**
* Destruct
*
* Shows errors
*/
public function __destruct(){
if($this->_debug && count($this->_arrErrors) > 0) {
echo '<pre>'.print_r($this->_arrErrors, true).'</pre>';exit;
}
}
/**
* __get
*
* Allows models to access CI's loaded classes using the same
* syntax as controllers.
*
* @param string
* @access private
*/
public function __get($key){
$CI =& get_instance();
return $CI->$key;
}
/**
* connect
*
* Connects to the server with the given login details.
*
* @return void
*/
private function connect(){
$this->inbox = imap_open($this->hostname, $this->username, $this->password) or die(imap_last_error());
}
/**
* close
*
* Closes the IMAP connection.
*
* @return void
*/
private function close(){
imap_close($this->inbox);
}
/**
* Get Emails
*
* Grab the emails from the mailbox. Move them to the mail archive.
*
* @return array
*/
public function get_emails(){
$this->connect();
$emails = imap_search($this->inbox,'ALL');
if($emails){
$output = array();
foreach($emails as $email_number){
// get information specific to this email
$overview = imap_fetch_overview($this->inbox, $email_number, 0);
// get plaintext
$message = imap_fetchbody($this->inbox, $email_number, '1');
// if not plaintext get full message
if(empty($message)) $message = imap_fetchbody($this->inbox, $email_number, '2');
$output[] = array( 'overview' => $overview[0], 'message' => $this->parse_message($message), 'full_message' => $message );
if($this->archive) imap_mail_move($this->inbox, $email_number, $this->archive_folder);
}
$this->close();
return $output;
}else{
$this->close();
return false;
}
}
/**
* Parse Message
*
* Try and get the actual reply from the email content
*
* @param string
* @return string
*/
public function parse_message($message){
$array = preg_split("/(\r\n|\n|\r)/", $message);
$message_array = array();
if($this->_debug){
echo '<pre>';
print_r($array);
echo '</pre>';
}
foreach($array as $item){
if($item == '--' || $item == '-- ' || $item == '-- ' || $item == '-- \n' || $item == '--\n'){
// Standard signature break identifier
break;
}elseif($this->starts_with($item, '-----Original Message-----')){
// MS Outlook default
break;
}elseif($this->starts_with($item, '________________________________')){
// 32 underscores, Outlook again
break;
}elseif($this->starts_with($item, 'On ') && $this->ends_with($item, ' wrote:')){
// OS X Mail.app default
break;
}elseif($this->starts_with($item, 'From: ')){
// failsafe four Outlook and some other reply formats
break;
}elseif($this->starts_with($item, 'Sent from my iPhone')){
// iPhone
break;
}elseif($this->starts_with($item, 'Sent from my BlackBerry')){
// Blackberry
break;
}else{
$message_array[] = $item;
}
}
if($this->_debug) echo implode('\n', $message_array);
return implode('\n', $message_array);
}
/**
* Starts With
*
* Checks to see if a string starts with another string
*
* @param string
* @param string
* @return boolean
*/
function starts_with($haystack, $needle){
return !strncmp($haystack, $needle, strlen($needle));
}
/**
* Ends With
*
* Checks to see if a string ends with another string
*
* @param string
* @param string
* @return boolean
*/
function ends_with($string, $test){
$strlen = strlen($string);
$testlen = strlen($test);
if ($testlen > $strlen) return false;
return substr_compare($string, $test, -$testlen) === 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment