2 require_once('include/html2plain.php');
3 require_once('include/msgclean.php');
4 require_once('include/quoteconvert.php');
6 function email_connect($mailbox,$username,$password) {
7 if(! function_exists('imap_open'))
10 $mbox = @imap_open($mailbox,$username,$password);
15 function email_poll($mbox,$email_addr) {
17 if(! ($mbox && $email_addr))
20 $search1 = @imap_search($mbox,'FROM "' . $email_addr . '"', SE_UID);
24 $search2 = @imap_search($mbox,'TO "' . $email_addr . '"', SE_UID);
28 $search3 = @imap_search($mbox,'CC "' . $email_addr . '"', SE_UID);
32 $search4 = @imap_search($mbox,'BCC "' . $email_addr . '"', SE_UID);
36 $res = array_unique(array_merge($search1,$search2,$search3,$search4));
42 function construct_mailbox_name($mailacct) {
43 $ret = '{' . $mailacct['server'] . ((intval($mailacct['port'])) ? ':' . $mailacct['port'] : '');
44 $ret .= (($mailacct['ssltype']) ? '/' . $mailacct['ssltype'] . '/novalidate-cert' : '');
45 $ret .= '}' . $mailacct['mailbox'];
50 function email_msg_meta($mbox,$uid) {
51 $ret = (($mbox && $uid) ? @imap_fetch_overview($mbox,$uid,FT_UID) : array(array()));
52 return ((count($ret)) ? $ret[0] : array());
55 function email_msg_headers($mbox,$uid) {
56 $raw_header = (($mbox && $uid) ? @imap_fetchheader($mbox,$uid,FT_UID) : '');
57 $raw_header = str_replace("\r",'',$raw_header);
59 $h = explode("\n",$raw_header);
61 foreach($h as $line ) {
62 if (preg_match("/^[a-zA-Z]/", $line)) {
63 $key = substr($line,0,strpos($line,':'));
64 $value = substr($line,strpos($line,':')+1);
66 $last_entry = strtolower($key);
67 $ret[$last_entry] = trim($value);
70 $ret[$last_entry] .= ' ' . trim($line);
77 function email_get_msg($mbox,$uid, $reply) {
80 $struc = (($mbox && $uid) ? @imap_fetchstructure($mbox,$uid,FT_UID) : null);
85 // for testing purposes: Collect imported mails
86 // $file = tempnam("/tmp/friendica2/", "mail-in-");
87 // file_put_contents($file, json_encode($struc));
90 $ret['body'] = email_get_part($mbox,$uid,$struc,0, 'html');
93 if (trim($ret['body']) == '')
94 $ret['body'] = email_get_part($mbox,$uid,$struc,0, 'plain');
96 $ret['body'] = html2bbcode($ret['body']);
101 foreach($struc->parts as $ptop => $p) {
102 $x = email_get_part($mbox,$uid,$p,$ptop + 1, 'plain');
105 $x = email_get_part($mbox,$uid,$p,$ptop + 1, 'html');
108 if (trim($html) != '')
109 $ret['body'] = html2bbcode($html);
111 $ret['body'] = $text;
114 $ret['body'] = removegpg($ret['body']);
115 $msg = removesig($ret['body']);
116 $ret['body'] = $msg['body'];
117 $ret['body'] = convertquote($ret['body'], $reply);
119 if (trim($html) != '')
120 $ret['body'] = removelinebreak($ret['body']);
122 $ret['body'] = unifyattributionline($ret['body']);
127 // At the moment - only return plain/text.
128 // Later we'll repackage inline images as data url's and make the HTML safe
130 function email_get_part($mbox,$uid,$p,$partno, $subtype) {
131 // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
132 global $htmlmsg,$plainmsg,$charset,$attachments;
138 ? @imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
139 : @imap_body($mbox,$uid,FT_UID|FT_PEEK);
141 // for testing purposes: Collect imported mails
142 // $file = tempnam("/tmp/friendica2/", "mail-body-");
143 // file_put_contents($file, $data);
145 // Any part may be encoded, even plain text messages, so check everything.
147 $data = quoted_printable_decode($data);
148 elseif ($p->encoding==3)
149 $data = base64_decode($data);
152 // get all parameters, like charset, filenames of attachments, etc.
155 foreach ($p->parameters as $x)
156 $params[strtolower($x->attribute)] = $x->value;
157 if (isset($p->dparameters) and $p->dparameters)
158 foreach ($p->dparameters as $x)
159 $params[strtolower($x->attribute)] = $x->value;
162 // Any part with a filename is an attachment,
163 // so an attached text file (type 0) is not mistaken as the message.
165 if ((isset($params['filename']) and $params['filename']) || (isset($params['name']) and $params['name'])) {
166 // filename may be given as 'Filename' or 'Name' or both
167 $filename = ($params['filename'])? $params['filename'] : $params['name'];
168 // filename may be encoded, so see imap_mime_header_decode()
169 $attachments[$filename] = $data; // this is a problem if two files have same name
173 if ($p->type == 0 && $data) {
174 // Messages may be split in different parts because of inline attachments,
175 // so append parts together with blank row.
176 if (strtolower($p->subtype)==$subtype) {
177 $data = iconv($params['charset'], 'UTF-8//IGNORE', $data);
178 return (trim($data) ."\n\n");
182 // $htmlmsg .= $data ."<br><br>";
183 $charset = $params['charset']; // assume all parts are same charset
187 // Many bounce notifications embed the original message as type 2,
188 // but AOL uses type 1 (multipart), which is not handled here.
189 // There are no PHP functions to parse embedded messages,
190 // so this just appends the raw source to the main message.
191 // elseif ($p->type==2 && $data) {
192 // $plainmsg .= $data."\n\n";
196 if (isset($p->parts) and $p->parts) {
198 foreach ($p->parts as $partno0=>$p2) {
199 $x .= email_get_part($mbox,$uid,$p2,$partno . '.' . ($partno0+1), $subtype); // 1.2, 1.2.1, etc.
209 function email_header_encode($in_str, $charset) {
211 $need_to_convert = false;
213 for($x = 0; $x < strlen($in_str); $x ++) {
214 if((ord($in_str[$x]) == 0) || ((ord($in_str[$x]) > 128))) {
215 $need_to_convert = true;
219 if(! $need_to_convert)
222 if ($out_str && $charset) {
224 // define start delimimter, end delimiter and spacer
226 $start = "=?" . $charset . "?B?";
227 $spacer = $end . "\r\n " . $start;
229 // determine length of encoded text within chunks
230 // and ensure length is even
231 $length = 75 - strlen($start) - strlen($end);
234 [EDIT BY danbrown AT php DOT net: The following
235 is a bugfix provided by (gardan AT gmx DOT de)
236 on 31-MAR-2005 with the following note:
237 "This means: $length should not be even,
238 but divisible by 4. The reason is that in
239 base64-encoding 3 8-bit-chars are represented
240 by 4 6-bit-chars. These 4 chars must not be
241 split between two encoded words, according
244 $length = $length - ($length % 4);
246 // encode the string and split it into chunks
247 // with spacers after each chunk
248 $out_str = base64_encode($out_str);
249 $out_str = chunk_split($out_str, $length, $spacer);
251 // remove trailing spacer and
252 // add start and end delimiters
253 $spacer = preg_quote($spacer,'/');
254 $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
255 $out_str = $start . $out_str . $end;
260 function email_send($addr, $subject, $headers, $item) {
261 //$headers .= 'MIME-Version: 1.0' . "\n";
262 //$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
263 //$headers .= 'Content-Type: text/plain; charset=UTF-8' . "\n";
264 //$headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
266 $part = uniqid("", true);
268 $html = prepare_body($item);
270 $headers .= "Mime-Version: 1.0\n";
271 $headers .= 'Content-Type: multipart/alternative; boundary="=_'.$part.'"'."\n\n";
273 $body = "\n--=_".$part."\n";
274 $body .= "Content-Transfer-Encoding: 8bit\n";
275 $body .= "Content-Type: text/plain; charset=utf-8; format=flowed\n\n";
277 $body .= html2plain($html)."\n";
279 $body .= "--=_".$part."\n";
280 $body .= "Content-Transfer-Encoding: 8bit\n";
281 $body .= "Content-Type: text/html; charset=utf-8\n\n";
283 $body .= '<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">'.$html."</body></html>\n";
285 $body .= "--=_".$part."--";
287 //$message = '<html><body>' . $html . '</body></html>';
288 //$message = html2plain($html);
289 logger('notifier: email delivery to ' . $addr);
290 mail($addr, $subject, $body, $headers);
293 function iri2msgid($iri) {
294 if (!strpos($iri, "@"))
295 $msgid = preg_replace("/urn:(\S+):(\S+)\.(\S+):(\d+):(\S+)/i", "urn!$1!$4!$5@$2.$3", $iri);
301 function msgid2iri($msgid) {
302 if (strpos($msgid, "@"))
303 $iri = preg_replace("/urn!(\S+)!(\d+)!(\S+)@(\S+)\.(\S+)/i", "urn:$1:$4.$5:$2:$3", $msgid);