-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemail_reader.class.php
180 lines (156 loc) · 4.17 KB
/
email_reader.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* EmailReader
*
* Access email from an IMAP or POP account.
* Currenly only supports fetching attachments.
*
* @see http://github.com/driverdan/emailreader
* @see http://driverdan.com
*
* @author Dan DeFelippi <[email protected]>
* @license MIT
* @todo Add additional features beyond saving attachments.
*/
class EmailReader {
// Stores mailbox stream
private $mbox;
// Email part types. Accessed via array position, do not reorder.
var $partTypes = array(
"text",
"multipart",
"message",
"application",
"audio",
"image",
"video",
"other",
);
/**
* Constructor opens connection to the server.
*
* @see http://www.php.net/manual/en/function.imap-open.php
*
* @param string $host Host connection string.
* @param string $user Username
* @param string $password Password
*
* @return bool Returns true on success, false on failure.
*/
function __construct($host, $user, $password) {
return (bool)($this->mbox = imap_open($host, $user, $password));
}
/**
* Destructor closes server connection.
*/
function __destruct() {
imap_close($this->mbox);
}
/**
* Decodes a message based on encoding type.
*
* @param string $message Email message part.
* @param int $encoding Encoding type.
*/
function decode($message, $encoding) {
switch ($encoding) {
case 0:
case 1:
$message = imap_8bit($message);
break;
case 2:
$message = imap_binary($message);
break;
case 3:
case 5:
$message = imap_base64($message);
break;
case 4:
$message = imap_qprint($message);
break;
}
return $message;
}
/**
* Saves all email attachments for all emails. Uses original filenames.
*
* @todo Handle duplicate filenames.
*
* @param string $path Directory path to save files in.
* @param bool $inline Save inline files (eg photos). Default is true.
* @param bool $delete Delete all emails after processing. Default is true.
*/
function saveAttachments($path, $inline = true, $delete = true) {
$numMessages = $this->getNumMessages();
// Append slash to path if missing
if ($path[strlen($path) - 1] != '/') {
$path .= '/';
}
// Loop through all messages
for ($msgId = 1; $msgId <= $numMessages; $msgId++) {
$structure = imap_fetchstructure($this->mbox, $msgId, FT_UID);
$fileNum = 2;
// Loop through all email parts
foreach ($structure->parts as $part) {
// Handle attachments and inline files (images)
if (strtoupper($part->disposition) == "ATTACHMENT" || ($inline && strtoupper($part->disposition) == "INLINE")) {
/**
* File extension is determined first by MIME type.
* This is because some phone email clients do not use real filenames for attachments.
* Other phones use CONTENT-OCTET or other generic MIME type so fallback to file extension.
* This was designed to process images so it may not work correctly for some MIME types.
*/
$ext = strtolower($part->subtype);
if (strlen($ext) > 4) {
$ext = end(explode('.', $part->dparameters[0]->value));
} else if ($ext == "jpeg") {
$ext = "jpg";
}
// @TODO Add other MIME types here?
$filename = $entry['id'] . ".$ext";
// Get the body and decode it
$body = imap_fetchbody($this->mbox, $msgId, $fileNum);
$data = self::decode($body, $part->type);
// Save the file
$fp = fopen("$path$filename", "w");
fputs($fp, $data);
fclose($fp);
$fileNum++;
}
}
if ($delete) {
$this->delete($msgId);
}
}
// Expunging is required if messages were deleted
if ($delete) {
$this->expunge();
}
}
/**
* Gets the number of messages in a mailbox.
*
* @return int Number of messages in the mailbox.
*/
function getNumMessages() {
return imap_num_msg($this->mbox);
}
/**
* Deletes a message.
*
* @param int $id ID of message to delete.
* @param bool $expunge Optionally expunge mailbox.
*/
function delete($id, $expunge = false) {
imap_delete($this->mbox, $id);
if ($expunge) {
$this->expunge();
}
}
/**
* Expunge a mailbox. Call after deleting messages.
*/
function expunge() {
imap_expunge($this->mbox);
}
}