New onsite patient portal, take 4.
[openemr.git] / portal / patient / fwk / libs / verysimple / Email / Pop3Client.php
blobf60c5585e37de6c7fef5d5d429d7851a4d394d77
1 <?php
2 /** @package verysimple::Email */
4 /**
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>
8 *
9 * @package verysimple::Email
10 * @author VerySimple Inc.
11 * @copyright 1997-2007 VerySimple, Inc.
12 * @license LGPL
13 * @version 1.1
15 class Pop3Client {
16 private $mbox;
17 private $do_delete = false;
18 public static $VERSION = "1.1";
19 function Pop3Client() {
20 if (! function_exists ( "imap_open" )) {
21 require_once ('PEAR.php');
22 if (! pear::loadExtension ( 'imap' ))
23 throw new Exception ( "Pop3Client: Unable to load imap extension" );
27 /**
28 * Opens a connection to the mail server
30 * @param string $user
31 * username
32 * @param string $pass
33 * password
34 * @param string $host
35 * host (ex mail.server.com)
36 * @param int $port
37 * port (default = 110)
38 * @param string $mbtype
39 * type of mailbox (default = pop3) (refer to www.php.net/imap_open)
40 * @param string $mbfolder
41 * name of folder to read (default = INBOX)
42 * @param int $options
43 * Flags for opening connect (refer to www.php.net/imap_open)
44 * @param int $retries
45 * Number of times to retry the connection (default = 1)
48 function Open($user, $pass, $host, $port = "110", $mbtype = "pop3", $mbfolder = "INBOX", $options = null, $retries = 1) {
49 $this->mbox = imap_open ( "{" . $host . ":" . $port . "/" . $mbtype . "/notls}" . $mbfolder . "", $user, $pass, $options, $retries );
52 /**
53 * Delete a message from the mailbox
55 * @param int $msgnum
56 * The id of the message within the mailbox
59 function DeleteMessage($msgnum) {
60 imap_delete ( $this->mbox, $msgnum );
61 $this->do_delete = true;
64 /**
65 * Close the connection to the mail server
67 * @param bool $empty_trash
68 * (default true) whether to empty the trash upon exit
71 function Close($empty_trash = true) {
72 if ($this->do_delete && $empty_trash) {
73 imap_expunge ( $this->mbox );
76 imap_close ( $this->mbox );
79 /**
80 * Returns the number of messages in the mail account folder
82 * @return int Number of messages
85 function GetMessageCount() {
86 $summary = $this->GetSummary ();
87 return $summary->Nmsgs;
90 /**
91 * Returns an object with the following properties:
92 * Date, Driver, Mailbox, Nmsgs, Recent
94 * @return stdClass
96 function GetSummary() {
97 return imap_check ( $this->mbox );
101 * Returns an array containing summary information about the messages.
102 * Use this function to list messages without downloading the entire
103 * contents of each one
105 * @return Array
107 function GetQuickHeaders() {
108 return imap_headers ( $this->mbox );
112 * Returns an object containing message header information
114 * @param int $msgno
115 * message number to retrieve
116 * @return stdClass
117 * @link http://www.php.net/imap_headerinfo
119 function GetHeader($msgno) {
120 return imap_headerinfo ( $this->mbox, $msgno );
124 * Returns an array containing all files attached to message
126 * @param int $msgno
127 * The index of the message to retrieve
128 * @param bool $include_raw_data
129 * 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.
130 * @return array An array of attachments
133 function GetAttachments($msgno, $include_raw_data = true) {
134 $struct = imap_fetchstructure ( $this->mbox, $msgno );
135 $contentParts = count ( $struct->parts );
136 $attachments = array ();
138 if ($contentParts >= 2) {
139 for($i = 2; $i <= $contentParts; $i ++) {
140 $att [$i - 2] = imap_bodystruct ( $this->mbox, $msgno, $i );
141 // these extra bits help us later...
142 $att [$i - 2]->x_msg_id = $msgno;
143 $att [$i - 2]->x_part_id = $i;
146 for($k = 0; $k < sizeof ( $att ); $k ++) {
147 if (strtolower ( $att [$k]->parameters [0]->value ) == "us-ascii" && $att [$k]->parameters [1]->value != "") {
148 $attachments [$k] = $this->_getPartFromStruct ( $att [$k], $include_raw_data );
149 } elseif (strtolower ( $att [$k]->parameters [0]->value ) != "iso-8859-1") {
150 $attachments [$k] = $this->_getPartFromStruct ( $att [$k], $include_raw_data );
155 return $attachments;
157 private function _getPartFromStruct($struct, $include_raw_data) {
158 // print_r($struct);
159 $part = null;
160 $part->msgnum = $struct->x_msg_id;
161 $part->partnum = $struct->x_part_id;
162 $part->filename = $struct->parameters [0]->value;
163 $part->type = $this->GetPrimaryType ( $struct );
164 $part->subtype = $struct->subtype;
165 $part->mimetype = $this->GetMimeType ( $struct );
166 $part->rawdata = (! $include_raw_data) ? null : $this->GetAttachmentRawData ( $struct->x_msg_id, $struct->x_part_id, $struct->encoding );
167 return $part;
171 * Returns the raw data for an attachment.
172 * The raw data is the actual file contents of an attachment.
174 * @param int $msgno
175 * message number
176 * @param int $partnum
177 * which attachment to retrieve
178 * @param int $encoding_id
179 * 0 = no encoding, 3 = base64, 4 = qprint
180 * @return mixed file contents of the attachment
183 function GetAttachmentRawData($msgno, $partnum, $encoding_id = 0) {
184 $content = imap_fetchbody ( $this->mbox, $msgno, $partnum );
186 if ($encoding_id == 3) {
187 return imap_base64 ( $content );
188 } elseif ($encoding_id == 4) {
189 return imap_qprint ( $content );
192 return $content;
196 * Returns a text representation of the MIME type of the primary part of a message
198 * @param object $structure
199 * message structure
200 * @return string
203 function GetPrimaryType(&$structure) {
204 $primary_mime_type = array (
205 "TEXT",
206 "MULTIPART",
207 "MESSAGE",
208 "APPLICATION",
209 "AUDIO",
210 "IMAGE",
211 "VIDEO",
212 "OTHER"
214 return $primary_mime_type [( int ) $structure->type];
218 * Returns the MimeType of the primary part of the given message
220 * @param object $structure
221 * The message to inspect
222 * @return string Mime Type
225 function GetMimeType(&$structure) {
226 if ($structure->subtype) {
227 return $this->GetPrimaryType ( $structure ) . '/' . $structure->subtype;
229 return "TEXT/PLAIN";
233 * Returns what is best determined to be the body text of the messages
235 * @param int $msgnum
236 * Message number in the mailbox
237 * @param bool $prefer_html
238 * If an html version is provided, return that
239 * @return string The contents of the message body
242 function GetMessageBody($msgnum, $prefer_html = true) {
243 if ($prefer_html) {
244 $body = $this->GetPart ( $msgnum, "TEXT/HTML" );
245 $body = $body ? $body : $this->GetPart ( $msgnum, "TEXT/PLAIN" );
246 } else {
247 $body = $this->GetPart ( $msgnum, "TEXT/PLAIN" );
248 $body = $body ? $body : $this->GetPart ( $msgnum, "TEXT/HTML" );
251 return $body;
255 * Returns a single part of a message
257 * @param int $msg_number
258 * The message number in the mailbox
259 * @param string $mime_type
260 * The mime type of the part to retrieve
261 * @param object $structure
262 * The message structure
263 * @param int $part_number
264 * The id of the part number within the message
265 * @return object The message part, or false if no match exists
268 function GetPart($msg_number, $mime_type, $structure = false, $part_number = false) {
269 $stream = $this->mbox;
270 $prefix = "";
272 $structure = $structure ? $structure : imap_fetchstructure ( $stream, $msg_number );
274 if ($structure) {
275 if ($mime_type == $this->GetMimeType ( $structure )) {
276 $part_number = $part_number ? $part_number : "1";
277 $text = imap_fetchbody ( $stream, $msg_number, $part_number );
279 if ($structure->encoding == 3) {
280 return imap_base64 ( $text );
281 } else if ($structure->encoding == 4) {
282 return imap_qprint ( $text );
283 } else {
284 return $text;
288 if ($structure->type == 1) /* multipart */
290 while ( list ( $index, $sub_structure ) = each ( $structure->parts ) ) {
291 if ($part_number) {
292 $prefix = $part_number . '.';
294 $data = $this->GetPart ( $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1) );
296 if ($data) {
297 return $data;
303 // no structure returned
304 return false;