3 * @file src/Protocol/Email.php
5 namespace Friendica\Protocol;
7 require_once 'include/html2plain.php';
8 require_once 'include/msgclean.php';
9 require_once 'include/quoteconvert.php';
13 public static function emailConnect($mailbox, $username, $password)
15 if (! function_exists('imap_open')) {
19 $mbox = @imap_open($mailbox, $username, $password);
24 public static function emailPoll($mbox, $email_addr)
26 if (! ($mbox && $email_addr))
29 $search1 = @imap_search($mbox,'FROM "' . $email_addr . '"', SE_UID);
33 logger("Found mails from ".$email_addr, LOGGER_DEBUG);
36 $search2 = @imap_search($mbox,'TO "' . $email_addr . '"', SE_UID);
40 logger("Found mails to ".$email_addr, LOGGER_DEBUG);
43 $search3 = @imap_search($mbox,'CC "' . $email_addr . '"', SE_UID);
47 logger("Found mails cc ".$email_addr, LOGGER_DEBUG);
50 $res = array_unique(array_merge($search1, $search2, $search3));
55 public static function constructMailboxName($mailacct)
57 $ret = '{' . $mailacct['server'] . ((intval($mailacct['port'])) ? ':' . $mailacct['port'] : '');
58 $ret .= (($mailacct['ssltype']) ? '/' . $mailacct['ssltype'] . '/novalidate-cert' : '');
59 $ret .= '}' . $mailacct['mailbox'];
63 public static function emailMsgMeta($mbox, $uid)
65 $ret = (($mbox && $uid) ? @imap_fetch_overview($mbox,$uid,FT_UID) : array(array())); // POSSIBLE CLEANUP --> array(array()) is probably redundant now
66 return ((count($ret)) ? $ret : array());
70 * @brief Check addons, not called from main friendica project
72 function email_msg_headers($mbox, $uid) {
73 $raw_header = (($mbox && $uid) ? @imap_fetchheader($mbox,$uid,FT_UID) : '');
74 $raw_header = str_replace("\r",'',$raw_header);
76 $h = explode("\n",$raw_header);
78 foreach ($h as $line ) {
79 if (preg_match("/^[a-zA-Z]/", $line)) {
80 $key = substr($line,0,strpos($line,':'));
81 $value = substr($line,strpos($line,':')+1);
83 $last_entry = strtolower($key);
84 $ret[$last_entry] = trim($value);
87 $ret[$last_entry] .= ' ' . trim($line);
93 public static function emailGetMsg($mbox, $uid, $reply)
97 $struc = (($mbox && $uid) ? @imap_fetchstructure($mbox,$uid,FT_UID) : null);
102 if (! $struc->parts) {
103 $ret['body'] = self::emailGetPart($mbox,$uid,$struc,0, 'html');
104 $html = $ret['body'];
106 if (trim($ret['body']) == '')
107 $ret['body'] = self::emailGetPart($mbox,$uid,$struc,0, 'plain');
109 $ret['body'] = html2bbcode($ret['body']);
114 foreach ($struc->parts as $ptop => $p) {
115 $x = self::emailGetPart($mbox,$uid,$p,$ptop + 1, 'plain');
120 $x = self::emailGetPart($mbox,$uid,$p,$ptop + 1, 'html');
125 if (trim($html) != '') {
126 $ret['body'] = html2bbcode($html);
128 $ret['body'] = $text;
132 $ret['body'] = removegpg($ret['body']);
133 $msg = removesig($ret['body']);
134 $ret['body'] = $msg['body'];
135 $ret['body'] = convertquote($ret['body'], $reply);
137 if (trim($html) != '') {
138 $ret['body'] = removelinebreak($ret['body']);
141 $ret['body'] = unifyattributionline($ret['body']);
146 // At the moment - only return plain/text.
147 // Later we'll repackage inline images as data url's and make the HTML safe
149 private static function emailGetPart($mbox, $uid, $p, $partno, $subtype)
151 // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
152 global $htmlmsg,$plainmsg,$charset,$attachments;
158 ? @imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
159 : @imap_body($mbox,$uid,FT_UID|FT_PEEK);
161 // Any part may be encoded, even plain text messages, so check everything.
163 $data = quoted_printable_decode($data);
164 elseif ($p->encoding==3)
165 $data = base64_decode($data);
168 // get all parameters, like charset, filenames of attachments, etc.
171 foreach ($p->parameters as $x)
172 $params[strtolower($x->attribute)] = $x->value;
173 if (isset($p->dparameters) && $p->dparameters)
174 foreach ($p->dparameters as $x)
175 $params[strtolower($x->attribute)] = $x->value;
178 // Any part with a filename is an attachment,
179 // so an attached text file (type 0) is not mistaken as the message.
181 if ((isset($params['filename']) && $params['filename']) || (isset($params['name']) && $params['name'])) {
182 // filename may be given as 'Filename' or 'Name' or both
183 $filename = ($params['filename'])? $params['filename'] : $params['name'];
184 // filename may be encoded, so see imap_mime_header_decode()
185 $attachments[$filename] = $data; // this is a problem if two files have same name
189 if ($p->type == 0 && $data) {
190 // Messages may be split in different parts because of inline attachments,
191 // so append parts together with blank row.
192 if (strtolower($p->subtype)==$subtype) {
193 $data = iconv($params['charset'], 'UTF-8//IGNORE', $data);
194 return (trim($data) ."\n\n");
198 // $htmlmsg .= $data ."<br><br>";
199 $charset = $params['charset']; // assume all parts are same charset
203 // Many bounce notifications embed the original message as type 2,
204 // but AOL uses type 1 (multipart), which is not handled here.
205 // There are no PHP functions to parse embedded messages,
206 // so this just appends the raw source to the main message.
207 // elseif ($p->type==2 && $data) {
208 // $plainmsg .= $data."\n\n";
212 if (isset($p->parts) && $p->parts) {
214 foreach ($p->parts as $partno0=>$p2) {
215 $x .= self::emailGetPart($mbox,$uid,$p2,$partno . '.' . ($partno0+1), $subtype); // 1.2, 1.2.1, etc.
224 public static function emailHeaderEncode($in_str, $charset)
227 $need_to_convert = false;
229 for ($x = 0; $x < strlen($in_str); $x ++) {
230 if ((ord($in_str[$x]) == 0) || ((ord($in_str[$x]) > 128))) {
231 $need_to_convert = true;
235 if (! $need_to_convert)
238 if ($out_str && $charset) {
240 // define start delimimter, end delimiter and spacer
242 $start = "=?" . $charset . "?B?";
243 $spacer = $end . "\r\n " . $start;
245 // determine length of encoded text within chunks
246 // and ensure length is even
247 $length = 75 - strlen($start) - strlen($end);
250 [EDIT BY danbrown AT php DOT net: The following
251 is a bugfix provided by (gardan AT gmx DOT de)
252 on 31-MAR-2005 with the following note:
253 "This means: $length should not be even,
254 but divisible by 4. The reason is that in
255 base64-encoding 3 8-bit-chars are represented
256 by 4 6-bit-chars. These 4 chars must not be
257 split between two encoded words, according
260 $length = $length - ($length % 4);
262 // encode the string and split it into chunks
263 // with spacers after each chunk
264 $out_str = base64_encode($out_str);
265 $out_str = chunk_split($out_str, $length, $spacer);
267 // remove trailing spacer and
268 // add start and end delimiters
269 $spacer = preg_quote($spacer,'/');
270 $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
271 $out_str = $start . $out_str . $end;
277 * emailSend is used by NETWORK_EMAIL and NETWORK_EMAIL2 code
278 * (not to notify the user, but to send items to email contacts)
280 * @todo This could be changed to use the Emailer class
282 public static function emailSend($addr, $subject, $headers, $item)
284 //$headers .= 'MIME-Version: 1.0' . "\n";
285 //$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
286 //$headers .= 'Content-Type: text/plain; charset=UTF-8' . "\n";
287 //$headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
289 $part = uniqid("", true);
291 $html = prepare_body($item);
293 $headers .= "Mime-Version: 1.0\n";
294 $headers .= 'Content-Type: multipart/alternative; boundary="=_'.$part.'"'."\n\n";
296 $body = "\n--=_".$part."\n";
297 $body .= "Content-Transfer-Encoding: 8bit\n";
298 $body .= "Content-Type: text/plain; charset=utf-8; format=flowed\n\n";
300 $body .= html2plain($html)."\n";
302 $body .= "--=_".$part."\n";
303 $body .= "Content-Transfer-Encoding: 8bit\n";
304 $body .= "Content-Type: text/html; charset=utf-8\n\n";
306 $body .= '<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">'.$html."</body></html>\n";
308 $body .= "--=_".$part."--";
310 //$message = '<html><body>' . $html . '</body></html>';
311 //$message = html2plain($html);
312 logger('notifier: email delivery to ' . $addr);
313 mail($addr, $subject, $body, $headers);
316 public static function iri2msgid($iri)
318 if (!strpos($iri, "@"))
319 $msgid = preg_replace("/urn:(\S+):(\S+)\.(\S+):(\d+):(\S+)/i", "urn!$1!$4!$5@$2.$3", $iri);
325 public static function msgid2iri($msgid)
327 if (strpos($msgid, "@"))
328 $iri = preg_replace("/urn!(\S+)!(\d+)!(\S+)@(\S+)\.(\S+)/i", "urn:$1:$4.$5:$2:$3", $msgid);