Skip to content

Commit

Permalink
Merge pull request #401 from barbushin/develop
Browse files Browse the repository at this point in the history
Bug fixes and improvements
  • Loading branch information
Sebbo94BY authored Nov 12, 2019
2 parents 0c65ac8 + 267eabf commit c23cbab
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* Example: Get and parse all unseen emails with saving their attachments one by one.
*
* @author Sebastian Krätzig <[email protected]>
*/

require_once __DIR__ . '/../vendor/autoload.php';
use PhpImap\Mailbox;
use PhpImap\Exceptions\ConnectionException;

$mailbox = new Mailbox(
'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
'[email protected]', // Username for the before configured mailbox
'*********' // Password for the before configured username
);

try {
$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) {
echo "+------ P A R S I N G ------+\n";

$email = $mailbox->getMail(
$mail_id, // ID of the email, you want to get
false // Do NOT mark emails as seen (optional)
);

echo "from-name: " . (isset($email->fromName)) ? $email->fromName : $email->fromAddress . "\n";
echo "from-email: " . $email->fromAddress . "\n";
echo "to: " . $email->to . "\n";
echo "subject: " . $email->subject . "\n";
echo "message_id: " . $email->messageId . "\n";

echo "mail has attachments? ";
if ($email->hasAttachments()) {
echo "Yes\n";
} else {
echo "No\n";
}

if (! empty($email->getAttachments())) {
echo count($email->getAttachments()) . " attachements\n";
}

// Save attachments one by one
if (! $mbox_connection->getAttachmentsIgnore()) {
$attachments = $email_content->getAttachments();

foreach ($attachments as $attachment) {
echo "--> Saving " . $attachment->name . "...";

// Set individually filePath for each single attachment
// In this case, every file will get the current Unix timestamp
$attachment->setFilePath(__DIR__.'/files/'.time());

if ($attachment->saveToDisk()) {
echo "OK, saved!\n";
} else {
echo "ERROR, could not save!\n";
}
}
}

if ($email->textHtml) {
echo "Message HTML:\n" . $email->textHtml;
} else {
echo "Message Plain:\n" . $email->textPlain;
}

if (!empty($email->autoSubmitted)) {
// Mark email as "read" / "seen"
$mailbox->markMailAsRead($mail_id);
echo "+------ IGNORING: Auto-Reply ------+\n";
}

if (!empty($email_content->precedence)) {
// Mark email as "read" / "seen"
$mailbox->markMailAsRead($mail_id);
echo "+------ IGNORING: Non-Delivery Report/Receipt ------+\n";
}
}

$mailbox->disconnect();
12 changes: 6 additions & 6 deletions src/PhpImap/DataPartInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ public function __construct($mail, $id, $part, $encoding, $options)

public function fetch()
{
if (isset($this->data)) {
return $this->data;
}

if (0 == $this->part) {
$this->data = $this->mail->imap('body', [$this->id, $this->options]);
} else {
Expand Down Expand Up @@ -66,8 +62,12 @@ public function fetch()
break;
}

if (isset($this->charset) and !empty($this->charset)) {
$this->data = $this->mail->convertStringEncoding($this->data, $this->charset, $this->mail->getServerEncoding());
if (isset($this->charset) and !empty(trim($this->charset))) {
$this->data = $this->mail->convertStringEncoding(
$this->data, // Data to convert
$this->charset, // FROM-Encoding (Charset)
$this->mail->getServerEncoding() // TO-Encoding (Charset)
);
}

return $this->data;
Expand Down
52 changes: 36 additions & 16 deletions src/PhpImap/IncomingMailAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,28 @@ public function __get($name)
return $this->filePath;
}

/**
* Sets the file path.
*
* @param string $filePath File path incl. file name and optional extension
*
* @return void
*/
public function setFilePath($filePath)
{
$this->file_path = $filePath;
}

public function addDataPartInfo(DataPartInfo $dataInfo)
{
$this->dataInfo = $dataInfo;
}

/**
* Saves the attachment object on the disk.
* Sets the data part info.
*
* @param DataPartInfo $dataInfo Date info (file content)
*
* @return bool True, if it could save the attachment on the disk
* @return void
*/
public function saveToDisk()
public function addDataPartInfo(DataPartInfo $dataInfo)
{
if (false === file_put_contents($this->filePath, $this->dataInfo->fetch())) {
unset($this->filePath);
unset($this->file_path);

return false;
}

return true;
$this->dataInfo = $dataInfo;
}

/**
Expand All @@ -92,10 +89,33 @@ public function getMimeType()
}

/**
* Gets the file content.
*
* @return string
*/
public function getContents()
{
return $this->dataInfo->fetch();
}

/**
* Saves the attachment object on the disk.
*
* @return bool True, if it could save the attachment on the disk
*/
public function saveToDisk()
{
if (is_null($this->dataInfo)) {
return false;
}

if (false === file_put_contents($this->filePath, $this->dataInfo->fetch())) {
unset($this->filePath);
unset($this->file_path);

return false;
}

return true;
}
}
Loading

0 comments on commit c23cbab

Please sign in to comment.