2 /** @package verysimple::Email */
5 * Generic interface for connecting to a POP3 or IMAP Server
6 * GetPart, GetMimeType & GetAttachments based on code by Kevin Steffer
7 * <http://www.linuxscope.net/articles/mailAttachmentsPHP.html>
9 * @package verysimple::Email
10 * @author VerySimple Inc.
11 * @copyright 1997-2007 VerySimple, Inc.
18 private $do_delete = false;
19 public static $VERSION = "1.1";
22 if (! function_exists("imap_open")) {
23 require_once('PEAR.php');
24 if (! pear
::loadExtension('imap')) {
25 throw new Exception("Pop3Client: Unable to load imap extension");
31 * Opens a connection to the mail server
38 * host (ex mail.server.com)
40 * port (default = 110)
41 * @param string $mbtype
42 * type of mailbox (default = pop3) (refer to www.php.net/imap_open)
43 * @param string $mbfolder
44 * name of folder to read (default = INBOX)
46 * Flags for opening connect (refer to www.php.net/imap_open)
48 * Number of times to retry the connection (default = 1)
51 function Open($user, $pass, $host, $port = "110", $mbtype = "pop3", $mbfolder = "INBOX", $options = null, $retries = 1)
53 $this->mbox
= imap_open("{" . $host . ":" . $port . "/" . $mbtype . "/notls}" . $mbfolder . "", $user, $pass, $options, $retries);
57 * Delete a message from the mailbox
60 * The id of the message within the mailbox
63 function DeleteMessage($msgnum)
65 imap_delete($this->mbox
, $msgnum);
66 $this->do_delete
= true;
70 * Close the connection to the mail server
72 * @param bool $empty_trash
73 * (default true) whether to empty the trash upon exit
76 function Close($empty_trash = true)
78 if ($this->do_delete
&& $empty_trash) {
79 imap_expunge($this->mbox
);
82 imap_close($this->mbox
);
86 * Returns the number of messages in the mail account folder
88 * @return int Number of messages
91 function GetMessageCount()
93 $summary = $this->GetSummary();
94 return $summary->Nmsgs
;
98 * Returns an object with the following properties:
99 * Date, Driver, Mailbox, Nmsgs, Recent
103 function GetSummary()
105 return imap_check($this->mbox
);
109 * Returns an array containing summary information about the messages.
110 * Use this function to list messages without downloading the entire
111 * contents of each one
115 function GetQuickHeaders()
117 return imap_headers($this->mbox
);
121 * Returns an object containing message header information
124 * message number to retrieve
126 * @link http://www.php.net/imap_headerinfo
128 function GetHeader($msgno)
130 return imap_headerinfo($this->mbox
, $msgno);
134 * Returns an array containing all files attached to message
137 * The index of the message to retrieve
138 * @param bool $include_raw_data
139 * The raw data is the actual contents of the attachment file. Setting this to FALSE will allow you to display the name, size, type, etc of all attachments without actually downloading the contents of the files. Use GetAttachmentRawData to retrieve the raw data for an individual attachment.
140 * @return array An array of attachments
143 function GetAttachments($msgno, $include_raw_data = true)
145 $struct = imap_fetchstructure($this->mbox
, $msgno);
146 $contentParts = count($struct->parts
);
147 $attachments = array ();
149 if ($contentParts >= 2) {
150 for ($i = 2; $i <= $contentParts; $i ++
) {
151 $att [$i - 2] = imap_bodystruct($this->mbox
, $msgno, $i);
152 // these extra bits help us later...
153 $att [$i - 2]->x_msg_id
= $msgno;
154 $att [$i - 2]->x_part_id
= $i;
157 for ($k = 0; $k < sizeof($att); $k ++
) {
158 if (strtolower($att [$k]->parameters
[0]->value
) == "us-ascii" && $att [$k]->parameters
[1]->value
!= "") {
159 $attachments [$k] = $this->_getPartFromStruct($att [$k], $include_raw_data);
160 } elseif (strtolower($att [$k]->parameters
[0]->value
) != "iso-8859-1") {
161 $attachments [$k] = $this->_getPartFromStruct($att [$k], $include_raw_data);
168 private function _getPartFromStruct($struct, $include_raw_data)
172 $part->msgnum
= $struct->x_msg_id
;
173 $part->partnum
= $struct->x_part_id
;
174 $part->filename
= $struct->parameters
[0]->value
;
175 $part->type
= $this->GetPrimaryType($struct);
176 $part->subtype
= $struct->subtype
;
177 $part->mimetype
= $this->GetMimeType($struct);
178 $part->rawdata
= (! $include_raw_data) ?
null : $this->GetAttachmentRawData($struct->x_msg_id
, $struct->x_part_id
, $struct->encoding
);
183 * Returns the raw data for an attachment.
184 * The raw data is the actual file contents of an attachment.
188 * @param int $partnum
189 * which attachment to retrieve
190 * @param int $encoding_id
191 * 0 = no encoding, 3 = base64, 4 = qprint
192 * @return mixed file contents of the attachment
195 function GetAttachmentRawData($msgno, $partnum, $encoding_id = 0)
197 $content = imap_fetchbody($this->mbox
, $msgno, $partnum);
199 if ($encoding_id == 3) {
200 return imap_base64($content);
201 } elseif ($encoding_id == 4) {
202 return imap_qprint($content);
209 * Returns a text representation of the MIME type of the primary part of a message
211 * @param object $structure
216 function GetPrimaryType(&$structure)
218 $primary_mime_type = array (
228 return $primary_mime_type [( int ) $structure->type
];
232 * Returns the MimeType of the primary part of the given message
234 * @param object $structure
235 * The message to inspect
236 * @return string Mime Type
239 function GetMimeType(&$structure)
241 if ($structure->subtype
) {
242 return $this->GetPrimaryType($structure) . '/' . $structure->subtype
;
249 * Returns what is best determined to be the body text of the messages
252 * Message number in the mailbox
253 * @param bool $prefer_html
254 * If an html version is provided, return that
255 * @return string The contents of the message body
258 function GetMessageBody($msgnum, $prefer_html = true)
261 $body = $this->GetPart($msgnum, "TEXT/HTML");
262 $body = $body ?
$body : $this->GetPart($msgnum, "TEXT/PLAIN");
264 $body = $this->GetPart($msgnum, "TEXT/PLAIN");
265 $body = $body ?
$body : $this->GetPart($msgnum, "TEXT/HTML");
272 * Returns a single part of a message
274 * @param int $msg_number
275 * The message number in the mailbox
276 * @param string $mime_type
277 * The mime type of the part to retrieve
278 * @param object $structure
279 * The message structure
280 * @param int $part_number
281 * The id of the part number within the message
282 * @return object The message part, or false if no match exists
285 function GetPart($msg_number, $mime_type, $structure = false, $part_number = false)
287 $stream = $this->mbox
;
290 $structure = $structure ?
$structure : imap_fetchstructure($stream, $msg_number);
293 if ($mime_type == $this->GetMimeType($structure)) {
294 $part_number = $part_number ?
$part_number : "1";
295 $text = imap_fetchbody($stream, $msg_number, $part_number);
297 if ($structure->encoding
== 3) {
298 return imap_base64($text);
299 } else if ($structure->encoding
== 4) {
300 return imap_qprint($text);
306 if ($structure->type
== 1) { /* multipart */
307 while (list ( $index, $sub_structure ) = each($structure->parts
)) {
309 $prefix = $part_number . '.';
312 $data = $this->GetPart($msg_number, $mime_type, $sub_structure, $prefix . ($index +
1));
321 // no structure returned