IMAP spout

by morphiumdeus, Tuesday, July 09, 2013, 18:43 (4268 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----*/


Complete thread:

 RSS Feed of thread

powered by my little forum