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);
91 foreach($struc->parts as $ptop => $p) {
92 $x = email_get_part($mbox,$uid,$p,$ptop + 1);
100 // At the moment - only return plain/text.
101 // Later we'll repackage inline images as data url's and make the HTML safe
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;
111 ? @imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
112 : @imap_body($mbox,$uid,FT_UID|FT_PEEK);
114 // for testing purposes: Collect imported mails
115 // $file = tempnam("/tmp/friendica2/", "mail-body-");
116 // file_put_contents($file, $data);
118 // Any part may be encoded, even plain text messages, so check everything.
120 $data = quoted_printable_decode($data);
121 elseif ($p->encoding==3)
122 $data = base64_decode($data);
125 // get all parameters, like charset, filenames of attachments, etc.
128 foreach ($p->parameters as $x)
129 $params[strtolower($x->attribute)] = $x->value;
131 foreach ($p->dparameters as $x)
132 $params[strtolower($x->attribute)] = $x->value;
135 // Any part with a filename is an attachment,
136 // so an attached text file (type 0) is not mistaken as the message.
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
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");
154 // $htmlmsg .= $data ."<br><br>";
155 $charset = $params['charset']; // assume all parts are same charset
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";
169 foreach ($p->parts as $partno0=>$p2) {
170 $x = email_get_part($mbox,$uid,$p2,$partno . '.' . ($partno0+1)); // 1.2, 1.2.1, etc.
179 function email_header_encode($in_str, $charset) {
181 $need_to_convert = false;
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;
189 if(! $need_to_convert)
192 if ($out_str && $charset) {
194 // define start delimimter, end delimiter and spacer
196 $start = "=?" . $charset . "?B?";
197 $spacer = $end . "\r\n " . $start;
199 // determine length of encoded text within chunks
200 // and ensure length is even
201 $length = 75 - strlen($start) - strlen($end);
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
214 $length = $length - ($length % 4);
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);
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;
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";
236 $part = uniqid("", true);
238 $html = prepare_body($item);
240 $headers .= "Mime-Version: 1.0\n";
241 $headers .= 'Content-Type: multipart/alternative; boundary="=_'.$part.'"'."\n\n";
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";
247 $body .= html2plain($html)."\n";
249 $body .= "--=_".$part."\n";
250 $body .= "Content-Transfer-Encoding: 8bit\n";
251 $body .= "Content-Type: text/html; charset=utf-8\n\n";
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";
255 $body .= "--=_".$part."--";
257 //$message = '<html><body>' . $html . '</body></html>';
258 //$message = html2plain($html);
259 logger('notifier: email delivery to ' . $addr);
260 mail($addr, $subject, $body, $headers);
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);
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);