]> git.mxchange.org Git - friendica.git/blob - include/email.php
4d8c25edfa0e4e9af9ba343fd81f8150d7ee53e0
[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 array();;
16
17         $search = imap_search($mbox,'FROM "' . $email_addr . '"', SE_UID);
18         return (($search) ? $search : array());
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 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());
33 }
34
35
36
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();
43
44     // HEADER
45     $h = imap_header($mbox,$mid);
46     // add code here to get date, from, to, cc, subject...
47
48     // BODY
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);
55     }
56 }
57
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;
61
62     // DECODE DATA
63     $data = ($partno)?
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.
67     if ($p->encoding==4)
68         $data = quoted_printable_decode($data);
69     elseif ($p->encoding==3)
70         $data = base64_decode($data);
71
72     // PARAMETERS
73     // get all parameters, like charset, filenames of attachments, etc.
74     $params = array();
75     if ($p->parameters)
76         foreach ($p->parameters as $x)
77             $params[strtolower($x->attribute)] = $x->value;
78     if ($p->dparameters)
79         foreach ($p->dparameters as $x)
80             $params[strtolower($x->attribute)] = $x->value;
81
82     // ATTACHMENT
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
90     }
91
92     // TEXT
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";
98         else
99             $htmlmsg .= $data ."<br><br>";
100         $charset = $params['charset'];  // assume all parts are same charset
101     }
102
103     // EMBEDDED MESSAGE
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";
110     }
111
112     // SUBPART RECURSION
113     if ($p->parts) {
114         foreach ($p->parts as $partno0=>$p2)
115             getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1));  // 1.2, 1.2.1, etc.
116     }
117 }