2 require_once('include/html2plain.php');
4 function email_connect($mailbox,$username,$password) {
5 if(! function_exists('imap_open'))
8 $mbox = @imap_open($mailbox,$username,$password);
13 function email_poll($mbox,$email_addr) {
15 if(! ($mbox && $email_addr))
18 $search1 = @imap_search($mbox,'FROM "' . $email_addr . '"', SE_UID);
22 $search2 = @imap_search($mbox,'TO "' . $email_addr . '"', SE_UID);
26 $search3 = @imap_search($mbox,'CC "' . $email_addr . '"', SE_UID);
30 $search4 = @imap_search($mbox,'BCC "' . $email_addr . '"', SE_UID);
34 $res = array_unique(array_merge($search1,$search2,$search3,$search4));
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'];
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());
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);
57 $h = split("\n",$raw_header);
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);
64 $last_entry = strtolower($key);
65 $ret[$last_entry] = trim($value);
68 $ret[$last_entry] .= ' ' . trim($line);
75 function email_get_msg($mbox,$uid) {
78 $struc = (($mbox && $uid) ? @imap_fetchstructure($mbox,$uid,FT_UID) : null);
83 // for testing purposes: Collect imported mails
84 // $file = tempnam("/tmp/friendica2/", "mail-in-");
85 // file_put_contents($file, json_encode($struc));
88 $ret['body'] = email_get_part($mbox,$uid,$struc,0, 'html');
90 if (trim($ret['body']) == '')
91 $ret['body'] = email_get_part($mbox,$uid,$struc,0, 'plain');
93 $ret['body'] = html2bbcode($ret['body']);
98 foreach($struc->parts as $ptop => $p) {
99 $x = email_get_part($mbox,$uid,$p,$ptop + 1, 'plain');
102 $x = email_get_part($mbox,$uid,$p,$ptop + 1, 'html');
105 if (trim($html) != '')
106 $ret['body'] = html2bbcode($html);
108 $ret['body'] = $text;
113 // At the moment - only return plain/text.
114 // Later we'll repackage inline images as data url's and make the HTML safe
116 function email_get_part($mbox,$uid,$p,$partno, $subtype) {
117 // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
118 global $htmlmsg,$plainmsg,$charset,$attachments;
124 ? @imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
125 : @imap_body($mbox,$uid,FT_UID|FT_PEEK);
127 // for testing purposes: Collect imported mails
128 // $file = tempnam("/tmp/friendica2/", "mail-body-");
129 // file_put_contents($file, $data);
131 // Any part may be encoded, even plain text messages, so check everything.
133 $data = quoted_printable_decode($data);
134 elseif ($p->encoding==3)
135 $data = base64_decode($data);
138 // get all parameters, like charset, filenames of attachments, etc.
141 foreach ($p->parameters as $x)
142 $params[strtolower($x->attribute)] = $x->value;
143 if (isset($p->dparameters) and $p->dparameters)
144 foreach ($p->dparameters as $x)
145 $params[strtolower($x->attribute)] = $x->value;
148 // Any part with a filename is an attachment,
149 // so an attached text file (type 0) is not mistaken as the message.
151 if ((isset($params['filename']) and $params['filename']) || (isset($params['name']) and $params['name'])) {
152 // filename may be given as 'Filename' or 'Name' or both
153 $filename = ($params['filename'])? $params['filename'] : $params['name'];
154 // filename may be encoded, so see imap_mime_header_decode()
155 $attachments[$filename] = $data; // this is a problem if two files have same name
159 if ($p->type == 0 && $data) {
160 // Messages may be split in different parts because of inline attachments,
161 // so append parts together with blank row.
162 if (strtolower($p->subtype)==$subtype) {
163 $data = iconv($params['charset'], 'UTF-8//IGNORE', $data);
164 return (trim($data) ."\n\n");
168 // $htmlmsg .= $data ."<br><br>";
169 $charset = $params['charset']; // assume all parts are same charset
173 // Many bounce notifications embed the original message as type 2,
174 // but AOL uses type 1 (multipart), which is not handled here.
175 // There are no PHP functions to parse embedded messages,
176 // so this just appends the raw source to the main message.
177 // elseif ($p->type==2 && $data) {
178 // $plainmsg .= $data."\n\n";
182 if (isset($p->parts) and $p->parts) {
184 foreach ($p->parts as $partno0=>$p2) {
185 $x .= email_get_part($mbox,$uid,$p2,$partno . '.' . ($partno0+1), $subtype); // 1.2, 1.2.1, etc.
195 function email_header_encode($in_str, $charset) {
197 $need_to_convert = false;
199 for($x = 0; $x < strlen($in_str); $x ++) {
200 if((ord($in_str[$x]) == 0) || ((ord($in_str[$x]) > 128))) {
201 $need_to_convert = true;
205 if(! $need_to_convert)
208 if ($out_str && $charset) {
210 // define start delimimter, end delimiter and spacer
212 $start = "=?" . $charset . "?B?";
213 $spacer = $end . "\r\n " . $start;
215 // determine length of encoded text within chunks
216 // and ensure length is even
217 $length = 75 - strlen($start) - strlen($end);
220 [EDIT BY danbrown AT php DOT net: The following
221 is a bugfix provided by (gardan AT gmx DOT de)
222 on 31-MAR-2005 with the following note:
223 "This means: $length should not be even,
224 but divisible by 4. The reason is that in
225 base64-encoding 3 8-bit-chars are represented
226 by 4 6-bit-chars. These 4 chars must not be
227 split between two encoded words, according
230 $length = $length - ($length % 4);
232 // encode the string and split it into chunks
233 // with spacers after each chunk
234 $out_str = base64_encode($out_str);
235 $out_str = chunk_split($out_str, $length, $spacer);
237 // remove trailing spacer and
238 // add start and end delimiters
239 $spacer = preg_quote($spacer);
240 $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
241 $out_str = $start . $out_str . $end;
246 function email_send($addr, $subject, $headers, $item) {
247 //$headers .= 'MIME-Version: 1.0' . "\n";
248 //$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
249 //$headers .= 'Content-Type: text/plain; charset=UTF-8' . "\n";
250 //$headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
252 $part = uniqid("", true);
254 $html = prepare_body($item);
256 $headers .= "Mime-Version: 1.0\n";
257 $headers .= 'Content-Type: multipart/alternative; boundary="=_'.$part.'"'."\n\n";
259 $body = "\n--=_".$part."\n";
260 $body .= "Content-Transfer-Encoding: 8bit\n";
261 $body .= "Content-Type: text/plain; charset=utf-8; format=flowed\n\n";
263 $body .= html2plain($html)."\n";
265 $body .= "--=_".$part."\n";
266 $body .= "Content-Transfer-Encoding: 8bit\n";
267 $body .= "Content-Type: text/html; charset=utf-8\n\n";
269 $body .= '<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">'.$html."</body></html>\n";
271 $body .= "--=_".$part."--";
273 //$message = '<html><body>' . $html . '</body></html>';
274 //$message = html2plain($html);
275 logger('notifier: email delivery to ' . $addr);
276 mail($addr, $subject, $body, $headers);
279 function iri2msgid($iri) {
280 if (!strpos($iri, "@"))
281 $msgid = preg_replace("/urn:(\S+):(\S+)\.(\S+):(\d+):(\S+)/i", "urn!$1!$4!$5@$2.$3", $iri);
287 function msgid2iri($msgid) {
288 if (strpos($msgid, "@"))
289 $iri = preg_replace("/urn!(\S+)!(\d+)!(\S+)@(\S+)\.(\S+)/i", "urn:$1:$4.$5:$2:$3", $msgid);