Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/EdenEmailComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Eden\Mail;

abstract class EdenEmailComponent
{

public function __get($name)
{
$methodCandidate = 'get'.ucfirst($name);
if (method_exists($this, $methodCandidate)) {
return $this->$methodCandidate();
}
return null;
}
}
142 changes: 142 additions & 0 deletions src/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace Eden\Mail;

use PhpImap\IncomingMailAttachment;

class Email extends EdenEmailComponent
{
/**
* Raw email structire getted by getEmailFormat()
* @var array
*/
protected $structure;
/**
* Raw email headers
* @var EmailHeaders
*/
protected $headers;
/**
* @var int
*/
protected $id;

protected $body;

/**
* @var IncomingMailAttachment[]
*/
protected $attachments = [];

/** @var bool */
protected $hasAttachments = false;

/** @var string|null */
private $textPlain;

/** @var string|null */
private $textHtml;

/** @var string|null */
private $textApplication;

public function __construct(array $structure = null)
{
if($structure){
$this->parseStructure($structure);
}
}

protected function parseStructure(array $structure)
{
$this->createHeadersFromRaw($structure);
$this->createBodyPartsFromRaw($structure);
$this->createAttachmentsFromRaw($structure);
}

protected function createBodyPartsFromRaw($structure)
{
$this->textPlain = $structure['body']['text/plain'] ?? null;
$this->textHtml = $structure['body']['text/html'] ?? null;
}

protected function createAttachmentsFromRaw(array $structure)
{
$rawAttachmens = $structure['attachment'] ?? [];

foreach ($rawAttachmens as $filename => $attachment) {
$attachment = array_filter($attachment);
if (empty($attachment)) {
continue;
}

$mime = array_key_first($attachment);
$incomingMailAttachment = new IncomingMailAttachment();
$incomingMailAttachment->id = \bin2hex(\random_bytes(20));
$incomingMailAttachment->name = $filename;
$incomingMailAttachment->mime = $mime;
$incomingMailAttachment->fileExtension = pathinfo($filename, PATHINFO_EXTENSION);

$incomingMailAttachment->setFileContent($attachment[$mime]);

$this->attachments[] = $incomingMailAttachment;
}
}

protected function createHeadersFromRaw(array $structure)
{
$this->headers = new EmailHeaders(
array_diff_key($structure, array_flip([
'body',
'attachment',
'attachments',
]))
);
}

public function setId(int $id) : self
{
$this->id = $id;
return $this;
}

public function getId():int
{
return $this->id;
}

public function getHeaders()
{
return $this->headers;
}

public function setStructure(array $structure)
{
$this->structure = $structure;
}

public function getAttachments()
{
return $this->attachments;
}

public function getTextHtml() : ?string
{
return $this->textHtml;
}

public function getTextPlain() : ?string
{
return $this->textPlain;
}

public function getTextApplication() :?string
{
return $this->textApplication;
}

public function setTrimInfoPartsData(bool $val)
{
// mute
}
}
35 changes: 35 additions & 0 deletions src/EmailHeaders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Eden\Mail;

class EmailHeaders extends EdenEmailComponent
{
protected $headers;

function __construct(array $rawHeaders)
{
$this->headers = $rawHeaders;
}

public function getDate()
{
return isset($this->headers['date'])
? date('Y-m-d H:i:s', $this->headers['date'])
: null;
}

public function getId()
{
return $this->headers['id'];
}

public function getFromaddress()
{
return $this->headers['from']['email'] ?? null;
}

public function getToaddress()
{
return $this->headers['to'][0]['email'] ?? null;
}
}
Loading