]> git.mxchange.org Git - friendica.git/blob - include/email.php
more work on email contacts
[friendica.git] / include / email.php
1 <?php
2
3 function email_connect($mailbox,$username,$password) {
4         if(! (local_user() && function_exists('imap_open')))
5                 return false;
6
7         $mbox = imap_open($mailbox,$username,$password);
8
9         return $mbox;
10 }
11
12 function email_poll($mbox,$email_addr) {
13
14         if(! ($mbox && $email_addr))
15                 return false;
16
17         $search = imap_search($mbox,'FROM "' . $email_addr . '"', SE_UID);
18         return $search;
19 }
20
21
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'];
26         return $ret;
27 }
28
29
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();
36
37     // HEADER
38     $h = imap_header($mbox,$mid);
39     // add code here to get date, from, to, cc, subject...
40
41     // BODY
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);
48     }
49 }
50
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;
54
55     // DECODE DATA
56     $data = ($partno)?
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.
60     if ($p->encoding==4)
61         $data = quoted_printable_decode($data);
62     elseif ($p->encoding==3)
63         $data = base64_decode($data);
64
65     // PARAMETERS
66     // get all parameters, like charset, filenames of attachments, etc.
67     $params = array();
68     if ($p->parameters)
69         foreach ($p->parameters as $x)
70             $params[strtolower($x->attribute)] = $x->value;
71     if ($p->dparameters)
72         foreach ($p->dparameters as $x)
73             $params[strtolower($x->attribute)] = $x->value;
74
75     // ATTACHMENT
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
83     }
84
85     // TEXT
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";
91         else
92             $htmlmsg .= $data ."<br><br>";
93         $charset = $params['charset'];  // assume all parts are same charset
94     }
95
96     // EMBEDDED MESSAGE
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";
103     }
104
105     // SUBPART RECURSION
106     if ($p->parts) {
107         foreach ($p->parts as $partno0=>$p2)
108             getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1));  // 1.2, 1.2.1, etc.
109     }
110 }