Skip to content

Commit

Permalink
Merge pull request #396 from barbushin/develop
Browse files Browse the repository at this point in the history
Different bug fixes and improvements
  • Loading branch information
Sebbo94BY authored Oct 31, 2019
2 parents 018213e + 8b24677 commit 0c65ac8
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 88 deletions.
2 changes: 2 additions & 0 deletions examples/get_and_parse_all_emails_with_matching_subject.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
$mail_ids = $mailbox->searchMailbox('SUBJECT "part of the subject"');
} catch(ConnectionException $ex) {
die("IMAP connection failed: " . $ex->getMessage());
} catch (Exception $ex) {
die("An error occured: " . $ex->getMessage());
}

foreach ($mail_ids as $mail_id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
$mail_ids = $mailbox->searchMailbox('UNSEEN');
} catch(ConnectionException $ex) {
die("IMAP connection failed: " . $ex->getMessage());
} catch (Exception $ex) {
die("An error occured: " . $ex->getMessage());
}

foreach ($mail_ids as $mail_id) {
Expand Down
2 changes: 2 additions & 0 deletions examples/get_and_parse_unseen_emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
$mail_ids = $mailbox->searchMailbox('UNSEEN');
} catch(ConnectionException $ex) {
die("IMAP connection failed: " . $ex->getMessage());
} catch (Exception $ex) {
die("An error occured: " . $ex->getMessage());
}

foreach ($mail_ids as $mail_id) {
Expand Down
53 changes: 53 additions & 0 deletions src/PhpImap/IncomingMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ public function getAttachments()
return $this->attachments;
}

/**
* @param string $id The attachment id
*
* @return bool
*/
public function removeAttachment($id)
{
if (!isset($this->attachments[$id])) {
return false;
}

unset($this->attachments[$id]);

return true;
}

/**
* Get array of internal HTML links placeholders.
*
Expand Down Expand Up @@ -136,4 +152,41 @@ public function replaceInternalLinks($baseUri)

return str_replace($search, $replace, $fetchedHtml);
}

/**
* Embed inline image attachments as base64 to allow for
* email html to display inline images automatically.
*/
public function embedImageAttachments()
{
preg_match_all("/\bcid:[^'\"\s]{1,256}/mi", $this->textHtml, $matches);

if (\count($matches)) {
foreach ($matches as $match) {
if (!isset($match[0])) {
continue;
}

$cid = str_replace('cid:', '', $match[0]);

foreach ($this->getAttachments() as $attachment) {
if ($attachment->contentId == $cid && 'inline' == $attachment->disposition) {
$contents = $attachment->getContents();
$contentType = $attachment->getMimeType();

if (!strstr($contentType, 'image')) {
continue;
}

$base64encoded = base64_encode($contents);
$replacement = 'data:'.$contentType.';base64, '.$base64encoded;

$this->textHtml = str_replace($match[0], $replacement, $this->textHtml);

$this->removeAttachment($attachment->id);
}
}
}
}
}
}
42 changes: 38 additions & 4 deletions src/PhpImap/IncomingMailAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpImap;

use finfo;

/**
* @see https://github.com/barbushin/php-imap
*
Expand All @@ -20,6 +22,11 @@ class IncomingMailAttachment
private $file_path;
private $dataInfo;

/**
* @var string
*/
private $mimeType;

public function __get($name)
{
if ('filePath' !== $name) {
Expand Down Expand Up @@ -49,10 +56,11 @@ public function addDataPartInfo(DataPartInfo $dataInfo)
$this->dataInfo = $dataInfo;
}

/*
* Saves the attachment object on the disk
* @return boolean True, if it could save the attachment on the disk
*/
/**
* Saves the attachment object on the disk.
*
* @return bool True, if it could save the attachment on the disk
*/
public function saveToDisk()
{
if (false === file_put_contents($this->filePath, $this->dataInfo->fetch())) {
Expand All @@ -64,4 +72,30 @@ public function saveToDisk()

return true;
}

/**
* Gets the MIME type.
*
* @return string
*/
public function getMimeType()
{
if (!$this->mimeType) {
if (class_exists('finfo')) {
$finfo = new finfo(FILEINFO_MIME);

$this->mimeType = $finfo->buffer($this->getContents());
}
}

return $this->mimeType;
}

/**
* @return string
*/
public function getContents()
{
return $this->dataInfo->fetch();
}
}
Loading

0 comments on commit 0c65ac8

Please sign in to comment.