Skip to content

Commit c23cbab

Browse files
authored
Merge pull request #401 from barbushin/develop
Bug fixes and improvements
2 parents 0c65ac8 + 267eabf commit c23cbab

File tree

4 files changed

+167
-55
lines changed

4 files changed

+167
-55
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/**
4+
* Example: Get and parse all unseen emails with saving their attachments one by one.
5+
*
6+
* @author Sebastian Krätzig <[email protected]>
7+
*/
8+
9+
require_once __DIR__ . '/../vendor/autoload.php';
10+
use PhpImap\Mailbox;
11+
use PhpImap\Exceptions\ConnectionException;
12+
13+
$mailbox = new Mailbox(
14+
'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
15+
'[email protected]', // Username for the before configured mailbox
16+
'*********' // Password for the before configured username
17+
);
18+
19+
try {
20+
$mail_ids = $mailbox->searchMailbox('UNSEEN');
21+
} catch(ConnectionException $ex) {
22+
die("IMAP connection failed: " . $ex->getMessage());
23+
} catch (Exception $ex) {
24+
die("An error occured: " . $ex->getMessage());
25+
}
26+
27+
foreach ($mail_ids as $mail_id) {
28+
echo "+------ P A R S I N G ------+\n";
29+
30+
$email = $mailbox->getMail(
31+
$mail_id, // ID of the email, you want to get
32+
false // Do NOT mark emails as seen (optional)
33+
);
34+
35+
echo "from-name: " . (isset($email->fromName)) ? $email->fromName : $email->fromAddress . "\n";
36+
echo "from-email: " . $email->fromAddress . "\n";
37+
echo "to: " . $email->to . "\n";
38+
echo "subject: " . $email->subject . "\n";
39+
echo "message_id: " . $email->messageId . "\n";
40+
41+
echo "mail has attachments? ";
42+
if ($email->hasAttachments()) {
43+
echo "Yes\n";
44+
} else {
45+
echo "No\n";
46+
}
47+
48+
if (! empty($email->getAttachments())) {
49+
echo count($email->getAttachments()) . " attachements\n";
50+
}
51+
52+
// Save attachments one by one
53+
if (! $mbox_connection->getAttachmentsIgnore()) {
54+
$attachments = $email_content->getAttachments();
55+
56+
foreach ($attachments as $attachment) {
57+
echo "--> Saving " . $attachment->name . "...";
58+
59+
// Set individually filePath for each single attachment
60+
// In this case, every file will get the current Unix timestamp
61+
$attachment->setFilePath(__DIR__.'/files/'.time());
62+
63+
if ($attachment->saveToDisk()) {
64+
echo "OK, saved!\n";
65+
} else {
66+
echo "ERROR, could not save!\n";
67+
}
68+
}
69+
}
70+
71+
if ($email->textHtml) {
72+
echo "Message HTML:\n" . $email->textHtml;
73+
} else {
74+
echo "Message Plain:\n" . $email->textPlain;
75+
}
76+
77+
if (!empty($email->autoSubmitted)) {
78+
// Mark email as "read" / "seen"
79+
$mailbox->markMailAsRead($mail_id);
80+
echo "+------ IGNORING: Auto-Reply ------+\n";
81+
}
82+
83+
if (!empty($email_content->precedence)) {
84+
// Mark email as "read" / "seen"
85+
$mailbox->markMailAsRead($mail_id);
86+
echo "+------ IGNORING: Non-Delivery Report/Receipt ------+\n";
87+
}
88+
}
89+
90+
$mailbox->disconnect();

src/PhpImap/DataPartInfo.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ public function __construct($mail, $id, $part, $encoding, $options)
3131

3232
public function fetch()
3333
{
34-
if (isset($this->data)) {
35-
return $this->data;
36-
}
37-
3834
if (0 == $this->part) {
3935
$this->data = $this->mail->imap('body', [$this->id, $this->options]);
4036
} else {
@@ -66,8 +62,12 @@ public function fetch()
6662
break;
6763
}
6864

69-
if (isset($this->charset) and !empty($this->charset)) {
70-
$this->data = $this->mail->convertStringEncoding($this->data, $this->charset, $this->mail->getServerEncoding());
65+
if (isset($this->charset) and !empty(trim($this->charset))) {
66+
$this->data = $this->mail->convertStringEncoding(
67+
$this->data, // Data to convert
68+
$this->charset, // FROM-Encoding (Charset)
69+
$this->mail->getServerEncoding() // TO-Encoding (Charset)
70+
);
7171
}
7272

7373
return $this->data;

src/PhpImap/IncomingMailAttachment.php

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,28 @@ public function __get($name)
4646
return $this->filePath;
4747
}
4848

49+
/**
50+
* Sets the file path.
51+
*
52+
* @param string $filePath File path incl. file name and optional extension
53+
*
54+
* @return void
55+
*/
4956
public function setFilePath($filePath)
5057
{
5158
$this->file_path = $filePath;
5259
}
5360

54-
public function addDataPartInfo(DataPartInfo $dataInfo)
55-
{
56-
$this->dataInfo = $dataInfo;
57-
}
58-
5961
/**
60-
* Saves the attachment object on the disk.
62+
* Sets the data part info.
63+
*
64+
* @param DataPartInfo $dataInfo Date info (file content)
6165
*
62-
* @return bool True, if it could save the attachment on the disk
66+
* @return void
6367
*/
64-
public function saveToDisk()
68+
public function addDataPartInfo(DataPartInfo $dataInfo)
6569
{
66-
if (false === file_put_contents($this->filePath, $this->dataInfo->fetch())) {
67-
unset($this->filePath);
68-
unset($this->file_path);
69-
70-
return false;
71-
}
72-
73-
return true;
70+
$this->dataInfo = $dataInfo;
7471
}
7572

7673
/**
@@ -92,10 +89,33 @@ public function getMimeType()
9289
}
9390

9491
/**
92+
* Gets the file content.
93+
*
9594
* @return string
9695
*/
9796
public function getContents()
9897
{
9998
return $this->dataInfo->fetch();
10099
}
100+
101+
/**
102+
* Saves the attachment object on the disk.
103+
*
104+
* @return bool True, if it could save the attachment on the disk
105+
*/
106+
public function saveToDisk()
107+
{
108+
if (is_null($this->dataInfo)) {
109+
return false;
110+
}
111+
112+
if (false === file_put_contents($this->filePath, $this->dataInfo->fetch())) {
113+
unset($this->filePath);
114+
unset($this->file_path);
115+
116+
return false;
117+
}
118+
119+
return true;
120+
}
101121
}

0 commit comments

Comments
 (0)