3 function email_connect($mailbox,$username,$password) {
4 if(! (local_user() && 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'] . (($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());
37 function getmsg($mbox,$mid) {
38 // input $mbox = IMAP stream, $mid = message id
39 // output all the following:
40 global $charset,$htmlmsg,$plainmsg,$attachments;
41 $htmlmsg = $plainmsg = $charset = '';
42 $attachments = array();
45 $h = imap_header($mbox,$mid);
46 // add code here to get date, from, to, cc, subject...
49 $s = imap_fetchstructure($mbox,$mid);
50 if (!$s->parts) // simple
51 getpart($mbox,$mid,$s,0); // pass 0 as part-number
52 else { // multipart: cycle through each part
53 foreach ($s->parts as $partno0=>$p)
54 getpart($mbox,$mid,$p,$partno0+1);
58 function getpart($mbox,$mid,$p,$partno) {
59 // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
60 global $htmlmsg,$plainmsg,$charset,$attachments;
64 imap_fetchbody($mbox,$mid,$partno): // multipart
65 imap_body($mbox,$mid); // simple
66 // Any part may be encoded, even plain text messages, so check everything.
68 $data = quoted_printable_decode($data);
69 elseif ($p->encoding==3)
70 $data = base64_decode($data);
73 // get all parameters, like charset, filenames of attachments, etc.
76 foreach ($p->parameters as $x)
77 $params[strtolower($x->attribute)] = $x->value;
79 foreach ($p->dparameters as $x)
80 $params[strtolower($x->attribute)] = $x->value;
83 // Any part with a filename is an attachment,
84 // so an attached text file (type 0) is not mistaken as the message.
85 if ($params['filename'] || $params['name']) {
86 // filename may be given as 'Filename' or 'Name' or both
87 $filename = ($params['filename'])? $params['filename'] : $params['name'];
88 // filename may be encoded, so see imap_mime_header_decode()
89 $attachments[$filename] = $data; // this is a problem if two files have same name
93 if ($p->type==0 && $data) {
94 // Messages may be split in different parts because of inline attachments,
95 // so append parts together with blank row.
96 if (strtolower($p->subtype)=='plain')
97 $plainmsg .= trim($data) ."\n\n";
99 $htmlmsg .= $data ."<br><br>";
100 $charset = $params['charset']; // assume all parts are same charset
104 // Many bounce notifications embed the original message as type 2,
105 // but AOL uses type 1 (multipart), which is not handled here.
106 // There are no PHP functions to parse embedded messages,
107 // so this just appends the raw source to the main message.
108 elseif ($p->type==2 && $data) {
109 $plainmsg .= $data."\n\n";
114 foreach ($p->parts as $partno0=>$p2)
115 getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1)); // 1.2, 1.2.1, etc.