3 function email_connect($mailbox,$username,$password) {
4 if(! function_exists('imap_open'))
7 $mbox = @imap_open($mailbox,$username,$password);
12 function email_poll($mbox,$email_addr) {
14 if(! ($mbox && $email_addr))
17 $search1 = @imap_search($mbox,'FROM "' . $email_addr . '"', SE_UID);
21 $search2 = @imap_search($mbox,'TO "' . $email_addr . '"', SE_UID);
25 $search3 = @imap_search($mbox,'CC "' . $email_addr . '"', SE_UID);
29 $search4 = @imap_search($mbox,'BCC "' . $email_addr . '"', SE_UID);
33 $res = array_unique(array_merge($search1,$search2,$search3,$search4));
39 function construct_mailbox_name($mailacct) {
40 $ret = '{' . $mailacct['server'] . ((intval($mailacct['port'])) ? ':' . $mailacct['port'] : '');
41 $ret .= (($mailacct['ssltype']) ? '/' . $mailacct['ssltype'] . '/novalidate-cert' : '');
42 $ret .= '}' . $mailacct['mailbox'];
47 function email_msg_meta($mbox,$uid) {
48 $ret = (($mbox && $uid) ? @imap_fetch_overview($mbox,$uid,FT_UID) : array(array()));
49 return ((count($ret)) ? $ret[0] : array());
52 function email_msg_headers($mbox,$uid) {
53 $raw_header = (($mbox && $uid) ? @imap_fetchheader($mbox,$uid,FT_UID) : '');
54 $raw_header = str_replace("\r",'',$raw_header);
56 $h = split("\n",$raw_header);
58 foreach($h as $line ) {
59 if (preg_match("/^[a-zA-Z]/", $line)) {
60 $key = substr($line,0,strpos($line,':'));
61 $value = substr($line,strpos($line,':')+1);
63 $last_entry = strtolower($key);
64 $ret[$last_entry] = trim($value);
67 $ret[$last_entry] .= ' ' . trim($line);
74 function email_get_msg($mbox,$uid) {
77 $struc = (($mbox && $uid) ? @imap_fetchstructure($mbox,$uid,FT_UID) : null);
83 $ret['body'] = email_get_part($mbox,$uid,$struc,0);
86 foreach($struc->parts as $ptop => $p) {
87 $x = email_get_part($mbox,$uid,$p,$ptop + 1);
95 // At the moment - only return plain/text.
96 // Later we'll repackage inline images as data url's and make the HTML safe
98 function email_get_part($mbox,$uid,$p,$partno) {
99 // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
100 global $htmlmsg,$plainmsg,$charset,$attachments;
106 ? @imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
107 : @imap_body($mbox,$uid,FT_UID|FT_PEEK);
109 // Any part may be encoded, even plain text messages, so check everything.
111 $data = quoted_printable_decode($data);
112 elseif ($p->encoding==3)
113 $data = base64_decode($data);
116 // get all parameters, like charset, filenames of attachments, etc.
119 foreach ($p->parameters as $x)
120 $params[strtolower($x->attribute)] = $x->value;
122 foreach ($p->dparameters as $x)
123 $params[strtolower($x->attribute)] = $x->value;
126 // Any part with a filename is an attachment,
127 // so an attached text file (type 0) is not mistaken as the message.
129 if ($params['filename'] || $params['name']) {
130 // filename may be given as 'Filename' or 'Name' or both
131 $filename = ($params['filename'])? $params['filename'] : $params['name'];
132 // filename may be encoded, so see imap_mime_header_decode()
133 $attachments[$filename] = $data; // this is a problem if two files have same name
137 if ($p->type == 0 && $data) {
138 // Messages may be split in different parts because of inline attachments,
139 // so append parts together with blank row.
140 if (strtolower($p->subtype)=='plain')
141 return (trim($data) ."\n\n");
145 // $htmlmsg .= $data ."<br><br>";
146 $charset = $params['charset']; // assume all parts are same charset
150 // Many bounce notifications embed the original message as type 2,
151 // but AOL uses type 1 (multipart), which is not handled here.
152 // There are no PHP functions to parse embedded messages,
153 // so this just appends the raw source to the main message.
154 // elseif ($p->type==2 && $data) {
155 // $plainmsg .= $data."\n\n";
160 foreach ($p->parts as $partno0=>$p2) {
161 $x = email_get_part($mbox,$uid,$p2,$partno . '.' . ($partno0+1)); // 1.2, 1.2.1, etc.
170 function email_header_encode($in_str, $charset) {
172 $need_to_convert = false;
174 for($x = 0; $x < strlen($in_str); $x ++) {
175 if((ord($in_str[$x]) == 0) || ((ord($in_str[$x]) > 128))) {
176 $need_to_convert = true;
180 if(! $need_to_convert)
183 if ($out_str && $charset) {
185 // define start delimimter, end delimiter and spacer
187 $start = "=?" . $charset . "?B?";
188 $spacer = $end . "\r\n " . $start;
190 // determine length of encoded text within chunks
191 // and ensure length is even
192 $length = 75 - strlen($start) - strlen($end);
195 [EDIT BY danbrown AT php DOT net: The following
196 is a bugfix provided by (gardan AT gmx DOT de)
197 on 31-MAR-2005 with the following note:
198 "This means: $length should not be even,
199 but divisible by 4. The reason is that in
200 base64-encoding 3 8-bit-chars are represented
201 by 4 6-bit-chars. These 4 chars must not be
202 split between two encoded words, according
205 $length = $length - ($length % 4);
207 // encode the string and split it into chunks
208 // with spacers after each chunk
209 $out_str = base64_encode($out_str);
210 $out_str = chunk_split($out_str, $length, $spacer);
212 // remove trailing spacer and
213 // add start and end delimiters
214 $spacer = preg_quote($spacer);
215 $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
216 $out_str = $start . $out_str . $end;