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