-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #401 from barbushin/develop
Bug fixes and improvements
- Loading branch information
Showing
4 changed files
with
167 additions
and
55 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
examples/get_and_parse_unseen_emails_save_attachments_one_by_one.php
This file contains 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
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(); |
This file contains 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
This file contains 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
Oops, something went wrong.