]> git.mxchange.org Git - friendica.git/blob - include/email.php
4fc241f248022291aa8d035e41da4532aaee16ec
[friendica.git] / include / email.php
1 <?php
2 require_once('include/html2plain.php');
3
4 function email_connect($mailbox,$username,$password) {
5         if(! function_exists('imap_open'))
6                 return false;
7
8         $mbox = @imap_open($mailbox,$username,$password);
9
10         return $mbox;
11 }
12
13 function email_poll($mbox,$email_addr) {
14
15         if(! ($mbox && $email_addr))
16                 return array();
17
18         $search1 = @imap_search($mbox,'FROM "' . $email_addr . '"', SE_UID);
19         if(! $search1)
20                 $search1 = array();
21
22         $search2 = @imap_search($mbox,'TO "' . $email_addr . '"', SE_UID);
23         if(! $search2)
24                 $search2 = array();
25
26         $search3 = @imap_search($mbox,'CC "' . $email_addr . '"', SE_UID);
27         if(! $search3)
28                 $search3 = array();
29
30         $search4 = @imap_search($mbox,'BCC "' . $email_addr . '"', SE_UID);
31         if(! $search4)
32                 $search4 = array();
33
34         $res = array_unique(array_merge($search1,$search2,$search3,$search4));
35
36         return $res;
37 }
38
39
40 function construct_mailbox_name($mailacct) {
41         $ret = '{' . $mailacct['server'] . ((intval($mailacct['port'])) ? ':' . $mailacct['port'] : '');
42         $ret .= (($mailacct['ssltype']) ?  '/' . $mailacct['ssltype'] . '/novalidate-cert' : '');
43         $ret .= '}' . $mailacct['mailbox'];
44         return $ret;
45 }
46
47
48 function email_msg_meta($mbox,$uid) {
49         $ret = (($mbox && $uid) ? @imap_fetch_overview($mbox,$uid,FT_UID) : array(array()));
50         return ((count($ret)) ? $ret[0] : array());
51 }
52
53 function email_msg_headers($mbox,$uid) {
54         $raw_header = (($mbox && $uid) ? @imap_fetchheader($mbox,$uid,FT_UID) : '');
55         $raw_header = str_replace("\r",'',$raw_header);
56         $ret = array();
57         $h = split("\n",$raw_header);
58         if(count($h))
59         foreach($h as $line ) {
60             if (preg_match("/^[a-zA-Z]/", $line)) {
61                         $key = substr($line,0,strpos($line,':'));
62                         $value = substr($line,strpos($line,':')+1);
63
64                         $last_entry = strtolower($key);
65                         $ret[$last_entry] = trim($value);
66                 }
67                 else {
68                         $ret[$last_entry] .= ' ' . trim($line);
69         }
70         }
71         return $ret;
72 }
73
74
75 function email_get_msg($mbox,$uid) {
76         $ret = array();
77
78         $struc = (($mbox && $uid) ? @imap_fetchstructure($mbox,$uid,FT_UID) : null);
79
80         if(! $struc)
81                 return $ret;
82
83         // for testing purposes: Collect imported mails
84         // $file = tempnam("/tmp/friendica2/", "mail-in-");
85         // file_put_contents($file, json_encode($struc));
86
87         if(! $struc->parts) {
88                 $ret['body'] = email_get_part($mbox,$uid,$struc,0);
89         }
90         else {
91                 foreach($struc->parts as $ptop => $p) {
92                         $x = email_get_part($mbox,$uid,$p,$ptop + 1);
93                         if($x)
94                                 $ret['body'] = $x;
95                 }
96         }
97         return $ret;
98 }
99
100 // At the moment - only return plain/text.
101 // Later we'll repackage inline images as data url's and make the HTML safe
102
103 function email_get_part($mbox,$uid,$p,$partno) {
104     // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
105     global $htmlmsg,$plainmsg,$charset,$attachments;
106
107         //echo $partno;
108
109     // DECODE DATA
110     $data = ($partno)
111                 ? @imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
112         : @imap_body($mbox,$uid,FT_UID|FT_PEEK);
113
114         // for testing purposes: Collect imported mails
115         // $file = tempnam("/tmp/friendica2/", "mail-body-");
116         // file_put_contents($file, $data);
117
118     // Any part may be encoded, even plain text messages, so check everything.
119     if ($p->encoding==4)
120         $data = quoted_printable_decode($data);
121     elseif ($p->encoding==3)
122         $data = base64_decode($data);
123
124     // PARAMETERS
125     // get all parameters, like charset, filenames of attachments, etc.
126     $params = array();
127     if ($p->parameters)
128         foreach ($p->parameters as $x)
129             $params[strtolower($x->attribute)] = $x->value;
130     if ($p->dparameters)
131         foreach ($p->dparameters as $x)
132             $params[strtolower($x->attribute)] = $x->value;
133
134     // ATTACHMENT
135     // Any part with a filename is an attachment,
136     // so an attached text file (type 0) is not mistaken as the message.
137
138     if ($params['filename'] || $params['name']) {
139         // filename may be given as 'Filename' or 'Name' or both
140         $filename = ($params['filename'])? $params['filename'] : $params['name'];
141         // filename may be encoded, so see imap_mime_header_decode()
142         $attachments[$filename] = $data;  // this is a problem if two files have same name
143     }
144
145     // TEXT
146     if ($p->type == 0 && $data) {
147         // Messages may be split in different parts because of inline attachments,
148         // so append parts together with blank row.
149         if (strtolower($p->subtype)=='plain')
150             return (trim($data) ."\n\n");
151         else
152                         $data = '';
153
154  //           $htmlmsg .= $data ."<br><br>";
155         $charset = $params['charset'];  // assume all parts are same charset
156     }
157
158     // EMBEDDED MESSAGE
159     // Many bounce notifications embed the original message as type 2,
160     // but AOL uses type 1 (multipart), which is not handled here.
161     // There are no PHP functions to parse embedded messages,
162     // so this just appends the raw source to the main message.
163 //    elseif ($p->type==2 && $data) {
164 //        $plainmsg .= $data."\n\n";
165 //    }
166
167     // SUBPART RECURSION
168     if ($p->parts) {
169         foreach ($p->parts as $partno0=>$p2) {
170             $x =  email_get_part($mbox,$uid,$p2,$partno . '.' . ($partno0+1));  // 1.2, 1.2.1, etc.
171                         if($x)
172                                 return $x;
173                 }
174     }
175 }
176
177
178
179 function email_header_encode($in_str, $charset) {
180     $out_str = $in_str;
181         $need_to_convert = false;
182
183         for($x = 0; $x < strlen($in_str); $x ++) {
184                 if((ord($in_str[$x]) == 0) || ((ord($in_str[$x]) > 128))) {
185                         $need_to_convert = true;
186                 }
187         }
188
189         if(! $need_to_convert)
190                 return $in_str;
191
192     if ($out_str && $charset) {
193
194         // define start delimimter, end delimiter and spacer
195         $end = "?=";
196         $start = "=?" . $charset . "?B?";
197         $spacer = $end . "\r\n " . $start;
198
199         // determine length of encoded text within chunks
200         // and ensure length is even
201         $length = 75 - strlen($start) - strlen($end);
202
203         /*
204             [EDIT BY danbrown AT php DOT net: The following
205             is a bugfix provided by (gardan AT gmx DOT de)
206             on 31-MAR-2005 with the following note:
207             "This means: $length should not be even,
208             but divisible by 4. The reason is that in
209             base64-encoding 3 8-bit-chars are represented
210             by 4 6-bit-chars. These 4 chars must not be
211             split between two encoded words, according
212             to RFC-2047.
213         */
214         $length = $length - ($length % 4);
215
216         // encode the string and split it into chunks
217         // with spacers after each chunk
218         $out_str = base64_encode($out_str);
219         $out_str = chunk_split($out_str, $length, $spacer);
220
221         // remove trailing spacer and
222         // add start and end delimiters
223         $spacer = preg_quote($spacer);
224         $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
225         $out_str = $start . $out_str . $end;
226     }
227     return $out_str;
228 }
229
230 function email_send($addr, $subject, $headers, $item) {
231         //$headers .= 'MIME-Version: 1.0' . "\n";
232         //$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
233         //$headers .= 'Content-Type: text/plain; charset=UTF-8' . "\n";
234         //$headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
235
236         $part = uniqid("", true);
237
238         $html    = prepare_body($item);
239
240         $headers .= "Mime-Version: 1.0\n";
241         $headers .= 'Content-Type: multipart/alternative; boundary="=_'.$part.'"'."\n\n";
242
243         $body = "\n--=_".$part."\n";
244         $body .= "Content-Transfer-Encoding: 8bit\n";
245         $body .= "Content-Type: text/plain; charset=utf-8; format=flowed\n\n";
246
247         $body .= html2plain($html)."\n";
248
249         $body .= "--=_".$part."\n";
250         $body .= "Content-Transfer-Encoding: 8bit\n";
251         $body .= "Content-Type: text/html; charset=utf-8\n\n";
252
253         $body .= '<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">'.$html."</body></html>\n";
254
255         $body .= "--=_".$part."--";
256
257         //$message = '<html><body>' . $html . '</body></html>';
258         //$message = html2plain($html);
259         logger('notifier: email delivery to ' . $addr);
260         mail($addr, $subject, $body, $headers);
261 }
262
263 function iri2msgid($iri) {
264         if (!strpos($iri, "@"))
265                 $msgid = preg_replace("/urn:(\S+):(\S+)\.(\S+):(\d+):(\S+)/i", "urn!$1!$4!$5@$2.$3", $iri);
266         else
267                 $msgid = $iri;
268         return($msgid);
269 }
270
271 function msgid2iri($msgid) {
272         if (strpos($msgid, "@"))
273                 $iri = preg_replace("/urn!(\S+)!(\d+)!(\S+)@(\S+)\.(\S+)/i", "urn:$1:$4.$5:$2:$3", $msgid);
274         else
275                 $iri = $msgid;
276         return($iri);
277 }