<?php
/**
 * IMAP Email Object Class.
 *
 * @category  Protocols
 * @package   Protocols
 * @author    Benjamin Hall <ben@conobe.co.uk>
 * @copyright 2019 Copyright (c) Benjamin Hall
 * @license   MIT https://github.com/benhall14/php-imap-reader
 * @link      https://conobe.co.uk/projects/php-imap-reader/
 */
class Email{public$id;public$date;public$udate;public$subject;public$to=array();public$from;public$reply_to=array();public$cc=array();public$recent;public$unseen;public$flagged;public$answered;public$deleted;public$draft;public$size;public$text_plain;public$text_html;public$attachments=array();public function isTo($email){foreach($this->to as$to){if($email==$to->email){return true;}}return false;}public function replyTo(){return$this->reply_to;}public function cc(){return$this->cc;}public function to(){return$this->to;}public function id(){return$this->id;}public function size(){return(int)$this->size;}public function date($format='Y-m-d H:i:s'){return$this->date->format($format);}public function subject(){return$this->subject;}public function fromName(){return$this->from&&$this->from->name?$this->from->name:null;}public function fromEmail(){return$this->from&&$this->from->email?$this->from->email:null;}public function plain(){return$this->text_plain;}public function html(){return$this->text_html?$this->injectInline($this->text_html):false;}public function hasAttachments(){return(count($this->attachments))?true:false;}public function attachments(){return$this->attachments;}public function attachment($attachment_id){return isset($this->attachments[$attachment_id])?$this->attachments[$attachment_id]:false;}public function isRecent(){return$this->recent;}public function isUnseen(){return$this->unseen;}public function isFlagged(){return$this->flagged;}public function isAnswered(){return$this->answered;}public function isDeleted(){return$this->deleted;}public function isDraft(){return$this->draft;}public function setSubject($subject){$this->subject=$subject;return$this;}public function setId($id){$this->id=$id;return$this;}public function setDate($date){$this->date=new DateTime($date);return$this;}public function setUdate($date){$this->udate=$date;return$this;}public function setSize($size){$this->size=(int)$size;return$this;}public function setUnseen($boolean){$this->unseen=(bool)$boolean;return$this;}public function setAnswered($boolean){$this->answered=(bool)$boolean;return$this;}public function setDraft($boolean){$this->draft=(bool)$boolean;return$this;}public function setRecent($boolean){$this->recent=(bool)$boolean;return$this;}public function setFlagged($boolean){$this->flagged=(bool)$boolean;return$this;}public function setDeleted($boolean){$this->deleted=(bool)$boolean;return$this;}public function addTo($mailbox,$host,$name=false){if(!$mailbox||!$host){return false;}$to=new stdClass();$to->name=($name)?$name:false;$to->mailbox=$mailbox;$to->host=$host;$to->email=$to->mailbox.'@'.$to->host;$this->to[]=$to;return$this;}public function addReplyTo($mailbox,$host,$name=false){if(!$mailbox||!$host){return false;}$reply_to=new stdClass();$reply_to->name=($name)?$name:false;$reply_to->mailbox=$mailbox;$reply_to->host=$host;$reply_to->email=$reply_to->mailbox.'@'.$reply_to->host;$this->reply_to[]=$reply_to;return$this;}public function addCC($mailbox,$host,$name=false){if(!$mailbox||!$host){return false;}$cc=new stdClass();$cc->name=$name?:false;$cc->mailbox=$mailbox;$cc->host=$host;$cc->email=$cc->mailbox.'@'.$cc->host;$this->cc[]=$cc;return$this;}public function setFrom($mailbox,$host,$name=false){$this->from=new stdClass();$this->from->name=$name?:false;$this->from->mailbox=$mailbox;$this->from->host=$host;$this->from->email=$this->from->mailbox.'@'.$this->from->host;return $this;}public function setHTML($html){$this->text_html.=trim($html);return$this;}public function setPlain($plain){$this->text_plain.=trim($plain);return$this;}public function addAttachment(EmailAttachment $attachment){$this->attachments[$attachment->id()]=$attachment;return$this;}public function injectInline($body){if($this->attachments){foreach($this->attachments as$attachment){if($attachment->isInline()){$body=str_replace('cid:'.$attachment->id(),$attachment->filePath(),$body);}}}return$body;}}
