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);
18 return (($search) ? $search : array());
22 function construct_mailbox_name($mailacct) {
23 $ret = '{' . $mailacct['server'] . ((intval($mailacct['port'])) ? ':' . $mailacct['port'] : '');
24 $ret .= (($mailacct['ssltype']) ? '/' . $mailacct['ssltype'] . '/novalidate-cert' : '');
25 $ret .= '}' . $mailacct['mailbox'];
30 function email_msg_meta($mbox,$uid) {
31 $ret = (($mbox && $uid) ? imap_fetch_overview($mbox,$uid,FT_UID) : array(array()));
32 return ((count($ret)) ? $ret[0] : array());
35 function email_msg_headers($mbox,$uid) {
36 $raw_header = (($mbox && $uid) ? imap_fetchheader($mbox,$uid,FT_UID) : '');
37 $raw_header = str_replace("\r",'',$raw_header);
39 $h = split("\n",$raw_header);
41 foreach($h as $line ) {
42 if (preg_match("/^[a-zA-Z]/", $line)) {
43 $key = substr($line,0,strpos($line,':'));
44 $value = substr($line,strpos($line,':')+1);
46 $last_entry = strtolower($key);
47 $ret[$last_entry] = trim($value);
50 $ret[$last_entry] .= ' ' . trim($line);
57 function email_get_msg($mbox,$uid) {
60 $struc = (($mbox && $uid) ? imap_fetchstructure($mbox,$uid,FT_UID) : null);
66 $ret['body'] = email_get_part($mbox,$uid,$struc,0);
69 foreach($struc->parts as $ptop => $p) {
70 $x = email_get_part($mbox,$uid,$p,$ptop + 1);
78 // At the moment - only return plain/text.
79 // Later we'll repackage inline images as data url's and make the HTML safe
81 function email_get_part($mbox,$uid,$p,$partno) {
82 // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
83 global $htmlmsg,$plainmsg,$charset,$attachments;
89 ? imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
90 : imap_body($mbox,$uid,FT_UID|FT_PEEK);
92 // Any part may be encoded, even plain text messages, so check everything.
94 $data = quoted_printable_decode($data);
95 elseif ($p->encoding==3)
96 $data = base64_decode($data);
99 // get all parameters, like charset, filenames of attachments, etc.
102 foreach ($p->parameters as $x)
103 $params[strtolower($x->attribute)] = $x->value;
105 foreach ($p->dparameters as $x)
106 $params[strtolower($x->attribute)] = $x->value;
109 // Any part with a filename is an attachment,
110 // so an attached text file (type 0) is not mistaken as the message.
112 if ($params['filename'] || $params['name']) {
113 // filename may be given as 'Filename' or 'Name' or both
114 $filename = ($params['filename'])? $params['filename'] : $params['name'];
115 // filename may be encoded, so see imap_mime_header_decode()
116 $attachments[$filename] = $data; // this is a problem if two files have same name
120 if ($p->type == 0 && $data) {
121 // Messages may be split in different parts because of inline attachments,
122 // so append parts together with blank row.
123 if (strtolower($p->subtype)=='plain')
124 return (trim($data) ."\n\n");
128 // $htmlmsg .= $data ."<br><br>";
129 $charset = $params['charset']; // assume all parts are same charset
133 // Many bounce notifications embed the original message as type 2,
134 // but AOL uses type 1 (multipart), which is not handled here.
135 // There are no PHP functions to parse embedded messages,
136 // so this just appends the raw source to the main message.
137 // elseif ($p->type==2 && $data) {
138 // $plainmsg .= $data."\n\n";
143 foreach ($p->parts as $partno0=>$p2) {
144 $x = email_get_part($mbox,$uid,$p2,$partno . '.' . ($partno0+1)); // 1.2, 1.2.1, etc.
153 function email_header_encode($in_str, $charset) {
155 if ($out_str && $charset) {
157 // define start delimimter, end delimiter and spacer
159 $start = "=?" . $charset . "?B?";
160 $spacer = $end . "\r\n " . $start;
162 // determine length of encoded text within chunks
163 // and ensure length is even
164 $length = 75 - strlen($start) - strlen($end);
167 [EDIT BY danbrown AT php DOT net: The following
168 is a bugfix provided by (gardan AT gmx DOT de)
169 on 31-MAR-2005 with the following note:
170 "This means: $length should not be even,
171 but divisible by 4. The reason is that in
172 base64-encoding 3 8-bit-chars are represented
173 by 4 6-bit-chars. These 4 chars must not be
174 split between two encoded words, according
177 $length = $length - ($length % 4);
179 // encode the string and split it into chunks
180 // with spacers after each chunk
181 $out_str = base64_encode($out_str);
182 $out_str = chunk_split($out_str, $length, $spacer);
184 // remove trailing spacer and
185 // add start and end delimiters
186 $spacer = preg_quote($spacer);
187 $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
188 $out_str = $start . $out_str . $end;