Last active
September 28, 2018 12:36
-
-
Save zelding/a3402fc0a8f3f5de0edc4dd5eb110e49 to your computer and use it in GitHub Desktop.
I didn't wanted to create 2 gitst, so the Address class is at the bottom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class EmailHeader | |
* | |
* I'm sick of darn stdClasses, so I made this | |
* it doesn't cover every possible properties | |
* | |
* @link http://php.net/manual/en/function.imap-headerinfo.php | |
*/ | |
class EmailHeader | |
{ | |
const FLAG_NOT = ' '; | |
const FLAG_RECENT_SEEN = 'R'; | |
const FLAG_RECENT_UNSEEN = 'N'; | |
const FLAG_UNSEEN = 'U'; | |
const FLAG_FLAGGED = 'F'; | |
const FLAG_ANSWERED = 'A'; | |
const FLAG_DELETED = 'D'; | |
const FLAG_DRAFT = 'X'; | |
#region Addresses | |
/** | |
* full to: line, up to 1024 characters | |
* | |
* @var null|string {1024} | |
*/ | |
protected $toAddress = null; | |
/** | |
* @var Address[] | |
*/ | |
protected $to = []; | |
/** | |
* full from: line, up to 1024 characters | |
* | |
* @var null|string | |
*/ | |
protected $fromAddress = null; | |
/** | |
* @var Address[] | |
*/ | |
protected $from = []; | |
/** | |
* full cc: line, up to 1024 characters | |
* | |
* @var null|string | |
*/ | |
protected $ccAddress = null; | |
/** | |
* @var Address[] | |
*/ | |
protected $cc = []; | |
/** | |
* full cc: line, up to 1024 characters | |
* | |
* @var null|string | |
*/ | |
protected $bccAddress = null; | |
/** | |
* @var Address[] | |
*/ | |
protected $bcc = []; | |
/** | |
* full to: line, up to 1024 characters | |
* | |
* @var null|string {1024} | |
*/ | |
protected $replyToAddress = null; | |
/** | |
* @var Address[] | |
*/ | |
protected $replyTo = []; | |
/** | |
* full to: line, up to 1024 characters | |
* | |
* @var null|string {1024} | |
*/ | |
protected $senderAddress = null; | |
/** | |
* @var Address[] | |
*/ | |
protected $sender = []; | |
/** | |
* full to: line, up to 1024 characters | |
* | |
* @var null|string {1024} | |
*/ | |
protected $returnPathAddress = null; | |
/** | |
* @var Address[] | |
*/ | |
protected $returnPath = []; | |
#endregion | |
/** @var \DateTime */ | |
protected $date; | |
/** @var int unix time */ | |
protected $uDate; | |
/** @var string */ | |
protected $subject; | |
/** @var string */ | |
protected $messageId; | |
/** @var null|string */ | |
protected $inReplyTo = null; | |
/** @var null|string */ | |
protected $followUpTo = null; | |
#region FLAGS | |
/** | |
* R if recent and seen, N if recent and not seen, ' ' if not recent | |
* | |
* @var string | |
*/ | |
protected $recent = self::FLAG_NOT; | |
/** | |
* U if not seen AND not recent, ' ' if seen OR not seen and recent | |
* | |
* @var string | |
*/ | |
protected $unseen = self::FLAG_NOT; | |
/** | |
* F if flagged, ' ' if not flagged | |
* | |
* @var string | |
*/ | |
protected $flagged = self::FLAG_NOT; | |
/** | |
* A if answered, ' ' if unanswered | |
* | |
* @var string | |
*/ | |
protected $answered = self::FLAG_NOT; | |
/** | |
* D if deleted, ' ' if not deleted | |
* | |
* @var string | |
*/ | |
protected $deleted = self::FLAG_NOT; | |
/** | |
* X if draft, ' ' if not draft | |
* | |
* @var string | |
*/ | |
protected $draft = self::FLAG_NOT; | |
#endregion | |
/** @var int */ | |
protected $msgNo = 0; | |
/** @var int */ | |
protected $size = 0; | |
/** | |
* EmailHeader constructor. | |
* | |
* @param \stdClass $headerInfo the data returned from `imap_headerinfo` | |
* @param bool $skippInvalidAddresses if true, invalid addresses won't be processed | |
*/ | |
public function __construct(\stdClass $headerInfo, $skippInvalidAddresses = false) | |
{ | |
#region addresses | |
if ( isset($headerInfo->toaddress) ) { | |
$this->toAddress = $headerInfo->toaddress; | |
} | |
if ( isset($headerInfo->to) && is_array($headerInfo->to) ) { | |
$this->to = $this->createAddressArray($headerInfo->to, $skippInvalidAddresses); | |
} | |
if ( isset($headerInfo->fromaddress) ) { | |
$this->fromAddress = $headerInfo->fromaddress; | |
} | |
if ( isset($headerInfo->from) && is_array($headerInfo->from) ) { | |
$this->from = $this->createAddressArray($headerInfo->from, $skippInvalidAddresses); | |
} | |
if ( isset($headerInfo->ccaddress) ) { | |
$this->ccAddress = $headerInfo->ccaddress; | |
} | |
if ( isset($headerInfo->cc) && is_array($headerInfo->cc) ) { | |
$this->cc = $this->createAddressArray($headerInfo->cc, $skippInvalidAddresses); | |
} | |
if ( isset($headerInfo->bccaddress) ) { | |
$this->bccAddress = $headerInfo->bccaddress; | |
} | |
if ( isset($headerInfo->bcc) && is_array($headerInfo->bcc) ) { | |
$this->bcc = $this->createAddressArray($headerInfo->bcc, $skippInvalidAddresses); | |
} | |
if ( isset($headerInfo->reply_toaddress) ) { | |
$this->replyToAddress = $headerInfo->reply_toaddress; | |
} | |
if ( isset($headerInfo->reply_to) && is_array($headerInfo->reply_to) ) { | |
$this->replyTo = $this->createAddressArray($headerInfo->reply_to, $skippInvalidAddresses); | |
} | |
if ( isset($headerInfo->senderaddress) ) { | |
$this->senderAddress = $headerInfo->senderaddress; | |
} | |
if ( isset($headerInfo->sender) && is_array($headerInfo->sender) ) { | |
$this->sender = $this->createAddressArray($headerInfo->sender, $skippInvalidAddresses); | |
} | |
if ( isset($headerInfo->return_pathaddress) ) { | |
$this->returnPathAddress = $headerInfo->return_pathaddress; | |
} | |
if ( isset($headerInfo->return_path) && is_array($headerInfo->return_path) ) { | |
$this->returnPath = $this->createAddressArray($headerInfo->return_path, $skippInvalidAddresses); | |
} | |
#endregion | |
if ( isset($headerInfo->date) || isset($headerInfo->Date) ) { | |
$this->date = isset($headerInfo->date) ? | |
new \DateTime($headerInfo->date) : | |
new \DateTime($headerInfo->Date); | |
} | |
if ( isset($headerInfo->udate) ) { | |
$this->uDate = $headerInfo->udate; | |
if ( null === $this->date ) { | |
$this->date = \DateTime::createFromFormat('U', $headerInfo->udate); | |
} | |
} | |
if ( isset($headerInfo->subject) || isset($headerInfo->Subject) ) { | |
$this->subject = isset($headerInfo->subject) ? | |
$headerInfo->subject : | |
$headerInfo->Subject; | |
} | |
if ( isset($headerInfo->in_reply_to) ) { | |
$this->inReplyTo = $headerInfo->in_reply_to; | |
} | |
if ( isset($headerInfo->message_id) ) { | |
$this->messageId = $headerInfo->message_id; | |
} | |
if ( isset($headerInfo->followup_to) ) { | |
$this->followUpTo = $headerInfo->followup_to; | |
} | |
if ( isset($headerInfo->Recent) ) { | |
$this->recent = $headerInfo->Recent; | |
} | |
if ( isset($headerInfo->Unseen) ) { | |
$this->unseen = $headerInfo->Unseen; | |
} | |
if ( isset($headerInfo->Flagged) ) { | |
$this->flagged = $headerInfo->Flagged; | |
} | |
if ( isset($headerInfo->Answered) ) { | |
$this->answered = $headerInfo->Answered; | |
} | |
if ( isset($headerInfo->Deleted) ) { | |
$this->deleted = $headerInfo->Deleted; | |
} | |
if ( isset($headerInfo->Draft) ) { | |
$this->draft = $headerInfo->Draft; | |
} | |
if ( isset($headerInfo->Msgno) ) { | |
$this->msgNo = $headerInfo->Msgno; | |
} | |
if ( isset($headerInfo->Size) ) { | |
$this->size = $headerInfo->Size; | |
} | |
} | |
/** | |
* @return bool | |
*/ | |
public function isValid() | |
{ | |
return $this->messageId !== null; | |
} | |
/** | |
* @param \stdClass[] $addresses | |
* @param bool $filterInvalid if true, invalid addresses are not added | |
* | |
* @return Address[] | |
*/ | |
protected function createAddressArray(array $addresses, $filterInvalid = false) | |
{ | |
$addressArray = []; | |
foreach($addresses as $addressData) { | |
$properObj = new Address($addressData); | |
if ( !$filterInvalid || $properObj->isValid() ) { | |
$addressArray[] = $properObj; | |
} | |
} | |
return $addressArray; | |
} | |
#region simple properties | |
/** | |
* @return null|string | |
*/ | |
public function getToAddress() | |
{ | |
return $this->toAddress; | |
} | |
/** | |
* @return null|string | |
*/ | |
public function getFromAddress() | |
{ | |
return $this->fromAddress; | |
} | |
/** | |
* @return null|string | |
*/ | |
public function getCcAddress() | |
{ | |
return $this->ccAddress; | |
} | |
/** | |
* @return null|string | |
*/ | |
public function getBccAddress() | |
{ | |
return $this->bccAddress; | |
} | |
/** | |
* @return null|string | |
*/ | |
public function getReplyToAddress() | |
{ | |
return $this->replyToAddress; | |
} | |
/** | |
* @return null|string | |
*/ | |
public function getSenderAddress() | |
{ | |
return $this->senderAddress; | |
} | |
/** | |
* @return null|string | |
*/ | |
public function getReturnPathAddress() | |
{ | |
return $this->returnPathAddress; | |
} | |
/** | |
* @return \DateTime | |
*/ | |
public function getDate() | |
{ | |
return $this->date; | |
} | |
/** | |
* @return int | |
*/ | |
public function getUDate() | |
{ | |
return $this->uDate; | |
} | |
/** | |
* @return string | |
*/ | |
public function getSubject() | |
{ | |
return $this->subject; | |
} | |
/** | |
* @return string | |
*/ | |
public function getMessageId() | |
{ | |
return $this->messageId; | |
} | |
/** | |
* @return null|string | |
*/ | |
public function getInReplyTo() | |
{ | |
return $this->inReplyTo; | |
} | |
/** | |
* @return null|string | |
*/ | |
public function getFollowUpTo() | |
{ | |
return $this->followUpTo; | |
} | |
/** | |
* @return int | |
*/ | |
public function getMsgNo() | |
{ | |
return $this->msgNo; | |
} | |
/** | |
* @return int | |
*/ | |
public function getSize() | |
{ | |
return $this->size; | |
} | |
#endregion | |
#region Address-Getters | |
/** | |
* @return Address[] | |
*/ | |
public function getTo() | |
{ | |
return $this->to; | |
} | |
/** | |
* @return Address[] | |
*/ | |
public function getFrom() | |
{ | |
return $this->from; | |
} | |
/** | |
* @return Address[] | |
*/ | |
public function getCc() | |
{ | |
return $this->cc; | |
} | |
/** | |
* @return Address[] | |
*/ | |
public function getBcc() | |
{ | |
return $this->bcc; | |
} | |
/** | |
* @return Address[] | |
*/ | |
public function getReplyTo() | |
{ | |
return $this->replyTo; | |
} | |
/** | |
* @return Address[] | |
*/ | |
public function getSender() | |
{ | |
return $this->sender; | |
} | |
/** | |
* @return Address[] | |
*/ | |
public function getReturnPath() | |
{ | |
return $this->returnPath; | |
} | |
#endregion | |
#region FLAG-GETTERS | |
/** | |
* @return bool | |
*/ | |
public function isRecent() | |
{ | |
return in_array($this->recent, [self::FLAG_RECENT_SEEN, self::FLAG_RECENT_UNSEEN]); | |
} | |
/** | |
* @return bool | |
*/ | |
public function isUnseen() | |
{ | |
return $this->unseen === self::FLAG_UNSEEN; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isFlagged() | |
{ | |
return $this->flagged === self::FLAG_FLAGGED; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isAnswered() | |
{ | |
return $this->answered === self::FLAG_ANSWERED; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isDeleted() | |
{ | |
return $this->deleted === self::FLAG_DELETED; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isDraft() | |
{ | |
return $this->draft === self::FLAG_DRAFT; | |
} | |
#endregion | |
} | |
final class Address | |
{ | |
/** @var string|null */ | |
private $personal = null; | |
/** @var string|null */ | |
private $adl = null; | |
/** @var string|null */ | |
private $mailbox = null; | |
/** @var string|null */ | |
private $host = null; | |
/** | |
* Address constructor. | |
* | |
* @param \stdClass $addressData | |
*/ | |
public function __construct(\stdClass $addressData) | |
{ | |
if ( isset($addressData->personal) ) | |
$this->personal = $addressData->personal; | |
if ( isset($addressData->adl) ) | |
$this->adl = $addressData->adl; | |
if ( isset($addressData->mailbox) ) | |
$this->mailbox = $addressData->mailbox; | |
if ( isset($addressData->host) ) | |
$this->host = $addressData->host; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isValid() | |
{ | |
return $this->mailbox !== null && $this->host !== null; | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->personal; | |
} | |
/** | |
* @return string | |
*/ | |
public function getAddress() | |
{ | |
return $this->mailbox . '@' . $this->host; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment