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 $search = imap_search($mbox,'FROM "' . $email_addr . '"', SE_UID);
19 $search2 = imap_search($mbox,'TO "' . $email_addr . '"', SE_UID);
21 if($search && $search2)
22 $res = array_merge($search,$search2);
28 return (($res) ? $res : array());
32 function construct_mailbox_name($mailacct) {
33 $ret = '{' . $mailacct['server'] . ((intval($mailacct['port'])) ? ':' . $mailacct['port'] : '');
34 $ret .= (($mailacct['ssltype']) ? '/' . $mailacct['ssltype'] . '/novalidate-cert' : '');
35 $ret .= '}' . $mailacct['mailbox'];
40 function email_msg_meta($mbox,$uid) {
41 $ret = (($mbox && $uid) ? imap_fetch_overview($mbox,$uid,FT_UID) : array(array()));
42 return ((count($ret)) ? $ret[0] : array());
45 function email_msg_headers($mbox,$uid) {
46 $raw_header = (($mbox && $uid) ? imap_fetchheader($mbox,$uid,FT_UID) : '');
47 $raw_header = str_replace("\r",'',$raw_header);
49 $h = split("\n",$raw_header);
51 foreach($h as $line ) {
52 if (preg_match("/^[a-zA-Z]/", $line)) {
53 $key = substr($line,0,strpos($line,':'));
54 $value = substr($line,strpos($line,':')+1);
56 $last_entry = strtolower($key);
57 $ret[$last_entry] = trim($value);
60 $ret[$last_entry] .= ' ' . trim($line);
67 function email_get_msg($mbox,$uid) {
70 $struc = (($mbox && $uid) ? imap_fetchstructure($mbox,$uid,FT_UID) : null);
76 $ret['body'] = email_get_part($mbox,$uid,$struc,0);
79 foreach($struc->parts as $ptop => $p) {
80 $x = email_get_part($mbox,$uid,$p,$ptop + 1);
88 // At the moment - only return plain/text.
89 // Later we'll repackage inline images as data url's and make the HTML safe
91 function email_get_part($mbox,$uid,$p,$partno) {
92 // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
93 global $htmlmsg,$plainmsg,$charset,$attachments;
99 ? imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
100 : imap_body($mbox,$uid,FT_UID|FT_PEEK);
102 // Any part may be encoded, even plain text messages, so check everything.
104 $data = quoted_printable_decode($data);
105 elseif ($p->encoding==3)
106 $data = base64_decode($data);
109 // get all parameters, like charset, filenames of attachments, etc.
112 foreach ($p->parameters as $x)
113 $params[strtolower($x->attribute)] = $x->value;
115 foreach ($p->dparameters as $x)
116 $params[strtolower($x->attribute)] = $x->value;
119 // Any part with a filename is an attachment,
120 // so an attached text file (type 0) is not mistaken as the message.
122 if ($params['filename'] || $params['name']) {
123 // filename may be given as 'Filename' or 'Name' or both
124 $filename = ($params['filename'])? $params['filename'] : $params['name'];
125 // filename may be encoded, so see imap_mime_header_decode()
126 $attachments[$filename] = $data; // this is a problem if two files have same name
130 if ($p->type == 0 && $data) {
131 // Messages may be split in different parts because of inline attachments,
132 // so append parts together with blank row.
133 if (strtolower($p->subtype)=='plain')
134 return (trim($data) ."\n\n");
138 // $htmlmsg .= $data ."<br><br>";
139 $charset = $params['charset']; // assume all parts are same charset
143 // Many bounce notifications embed the original message as type 2,
144 // but AOL uses type 1 (multipart), which is not handled here.
145 // There are no PHP functions to parse embedded messages,
146 // so this just appends the raw source to the main message.
147 // elseif ($p->type==2 && $data) {
148 // $plainmsg .= $data."\n\n";
153 foreach ($p->parts as $partno0=>$p2) {
154 $x = email_get_part($mbox,$uid,$p2,$partno . '.' . ($partno0+1)); // 1.2, 1.2.1, etc.
163 function email_header_encode($in_str, $charset) {
165 if ($out_str && $charset) {
167 // define start delimimter, end delimiter and spacer
169 $start = "=?" . $charset . "?B?";
170 $spacer = $end . "\r\n " . $start;
172 // determine length of encoded text within chunks
173 // and ensure length is even
174 $length = 75 - strlen($start) - strlen($end);
177 [EDIT BY danbrown AT php DOT net: The following
178 is a bugfix provided by (gardan AT gmx DOT de)
179 on 31-MAR-2005 with the following note:
180 "This means: $length should not be even,
181 but divisible by 4. The reason is that in
182 base64-encoding 3 8-bit-chars are represented
183 by 4 6-bit-chars. These 4 chars must not be
184 split between two encoded words, according
187 $length = $length - ($length % 4);
189 // encode the string and split it into chunks
190 // with spacers after each chunk
191 $out_str = base64_encode($out_str);
192 $out_str = chunk_split($out_str, $length, $spacer);
194 // remove trailing spacer and
195 // add start and end delimiters
196 $spacer = preg_quote($spacer);
197 $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
198 $out_str = $start . $out_str . $end;