IMAP spout

by morphiumdeus, Tuesday, July 09, 2013, 14:48 (4112 days ago)

Hello,
has someone already implemented a IMAP spout as described on the homepage? I could really use this. I'm not too good with php, but otherwise I would try to hack it myself...
Thanks, md

IMAP spout

by morphiumdeus, Tuesday, July 09, 2013, 18:43 (4111 days ago) @ morphiumdeus

Ok, so programmed a very simple (and very dirty) Email-Spout. It will only show the subject, date and html/plain body of a mail. No attachments, nothing else. Please do not judge me for not really implementing the get_icon, get_link and get_html... functions. And it has some problems with utf-8 or whatever conversion (help appreciated). I copied this in /spouts/mail/mail.php.

<?PHP
namespace spouts\mail;

class mailItem {
private $id;
private $title;
private $date;
private $content;
public function setId($id) {
$this->id = $id;
}
public function get_id() {
return $this->id;
}
public function setTitle($title) {
$this->title = $title;
}
public function get_title() {
return $this->title;
}
public function setDate($date) {
$this->date = $date;
}
public function get_date() {
return date('Y-m-d H:i:s', $this->date);
}
public function setContent($content) {
$this->content = $content;
}
public function get_content() {
return $this->content;
}
public function get_link() {
return $this->title;
}
public function getHtmlUrl() {
return $this->title;
}
}

class mail extends \spouts\spout {
public $name = 'Email';
public $description = 'email imap account as source';
public $params = array(
"email" => array(
"title" => "Email",
"type" => "text",
"default" => "",
"required" => true,
"validation" => array("email")
),
"password" => array(
"title" => "Password",
"type" => "password",
"default" => "",
"required" => true,
"validation" => array("notempty")
),
"host" => array(
"title" => "URL",
"type" => "text",
"default" => "{imap.one.com:993/service=imap/ssl}INBOX",
"required" => true,
"validation" => array("notempty")
)
);

/**

* current fetched items

*

* @var array|bool

*/

protected $items = false;

//

// Iterator Interface

//

/**

* reset iterator

*

* @return void

*/

public function rewind() {

if($this->items!==false)

reset($this->items);

}

/**

* receive current item

*

* @return current item

*/

public function current() {

if($this->items!==false)

return $this;

return false;

}

/**

* receive key of current item

*

* @return mixed key of current item

*/

public function key() {

if($this->items!==false)

return key($this->items);

return false;

}

/**

* select next item

*

* @return next item

*/

public function next() {

if($this->items!==false)

next($this->items);

return $this;

}

/**

* end reached

*

* @return bool false if end reached

*/

public function valid() {

if($this->items!==false)

return current($this->items) !== false;

return false;

}

//

// Source Methods

//

/**

* loads content for given source

*

* @return void

* @param mixed $params the params of this source

*/

public function load($params) {

// initialize
$mails = '';
$mbox = imap_open($params["host"],$params["email"],$params["password"]) or die("Could not open Mailbox - try again later!");
$message_count = imap_num_msg($mbox);
for ($i = 1; $i <= $message_count; ++$i) {
$thisMail = new mailItem();
$thisMail->setTitle(quoted_printable_decode(imap_header($mbox, $i)->subject));
$thisMail->setDate(imap_header($mbox, $i)->udate);
$thisMail->setId(imap_header($mbox, $i)->message_id);
$struct = imap_fetchstructure($mbox, $i, $options = null);
if(isset($struct->parts)) {
foreach($struct->parts as $key => $value) {
if(strtoupper($value->subtype) == "HTML"){
$thisMail->setContent(quoted_printable_decode(imap_fetchbody($mbox, $i, $key)));
}
}
}
else $thisMail->setContent(quoted_printable_decode(imap_fetchbody($mbox, $i, "1")));
$mails[]=$thisMail;
}
imap_close($mbox);


// save fetched items

$this->items = @$mails;

}

/*----Please continue with 2nd part----*/

IMAP spout

by morphiumdeus, Tuesday, July 09, 2013, 18:43 (4111 days ago) @ morphiumdeus

/*---- 2nd part ----*/

/**

* returns an unique id for this item

*

* @return string id as hash

*/

public function getId() {

if($this->items!==false && $this->valid()) {

$id = @current($this->items)->get_id();

if(strlen($id)>255)

$id = md5($id);

return $id;

}

return false;

}

/**

* returns the current title as string

*

* @return string title

*/

public function getTitle() {

if($this->items!==false && $this->valid())

return @current($this->items)->get_title();

return false;

}

/**

* returns the content of this item

*

* @return string content

*/

public function getContent() {

if($this->items!==false && $this->valid())

return @current($this->items)->get_content();

return false;

}

/**

* returns the date of this item

*

* @return string date

*/

public function getDate() {

if($this->items!==false && $this->valid())

$date = @current($this->items)->get_date();

if(strlen($date)==0)

$date = date('Y-m-d H:i:s');

return $date;

}


/**
* returns the global html url for the source
*
* @return string url as html
*/
public function getHtmlUrl() {
if(isset($this->htmlUrl))
return $this->htmlUrl;
return false;
}

/**
* returns the icon of this item
*
* @return string icon url
*/
public function getIcon() {
if(isset($this->faviconUrl))
return $this->faviconUrl;

$this->faviconUrl = false;
$imageHelper = $this->getImageHelper();
$htmlUrl = $this->getHtmlUrl();
if($htmlUrl && $imageHelper->fetchFavicon($htmlUrl))
$this->faviconUrl = $imageHelper->getFaviconUrl();
return $this->faviconUrl;
}


/**
* returns the link of this item
*
* @return string link
*/
public function getLink() {
if($this->items!==false && $this->valid())
return @current($this->items)->get_link();
return false;
}

/**

* destroy the plugin (prevent memory issues)

*/

public function destroy() {

unset($this->items);

$this->items = false;

}

}

RSS Feed of thread
powered by my little forum