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';
17 * @param string $mailbox The mailbox name
18 * @param string $username The username
19 * @param string $password The password
22 public static function connect($mailbox, $username, $password)
24 if (! function_exists('imap_open')) {
28 $mbox = @imap_open($mailbox, $username, $password);
34 * @param object $mbox mailbox
35 * @param string $email_addr email
38 public static function poll($mbox, $email_addr)
40 if (! ($mbox && $email_addr))
43 $search1 = @imap_search($mbox, 'FROM "' . $email_addr . '"', SE_UID);
47 logger("Found mails from ".$email_addr, LOGGER_DEBUG);
50 $search2 = @imap_search($mbox, 'TO "' . $email_addr . '"', SE_UID);
54 logger("Found mails to ".$email_addr, LOGGER_DEBUG);
57 $search3 = @imap_search($mbox, 'CC "' . $email_addr . '"', SE_UID);
61 logger("Found mails cc ".$email_addr, LOGGER_DEBUG);
64 $res = array_unique(array_merge($search1, $search2, $search3));
70 * @param array $mailacct mail account
73 public static function constructMailboxName($mailacct)
75 $ret = '{' . $mailacct['server'] . ((intval($mailacct['port'])) ? ':' . $mailacct['port'] : '');
76 $ret .= (($mailacct['ssltype']) ? '/' . $mailacct['ssltype'] . '/novalidate-cert' : '');
77 $ret .= '}' . $mailacct['mailbox'];
82 * @param object $mbox mailbox
83 * @param integer $uid user id
86 public static function messageMeta($mbox, $uid)
88 $ret = (($mbox && $uid) ? @imap_fetch_overview($mbox, $uid, FT_UID) : array(array())); // POSSIBLE CLEANUP --> array(array()) is probably redundant now
89 return (count($ret)) ? $ret : array();
93 * @param object $mbox mailbox
94 * @param integer $uid user id
95 * @param string $reply reply
98 public static function getMessage($mbox, $uid, $reply)
102 $struc = (($mbox && $uid) ? @imap_fetchstructure($mbox, $uid, FT_UID) : null);
108 if (! $struc->parts) {
109 $ret['body'] = self::messageGetPart($mbox, $uid, $struc, 0, 'html');
110 $html = $ret['body'];
112 if (trim($ret['body']) == '') {
113 $ret['body'] = self::messageGetPart($mbox, $uid, $struc, 0, 'plain');
115 $ret['body'] = html2bbcode($ret['body']);
120 foreach ($struc->parts as $ptop => $p) {
121 $x = self::messageGetPart($mbox, $uid, $p, $ptop + 1, 'plain');
126 $x = self::messageGetPart($mbox, $uid, $p, $ptop + 1, 'html');
131 if (trim($html) != '') {
132 $ret['body'] = html2bbcode($html);
134 $ret['body'] = $text;
138 $ret['body'] = removegpg($ret['body']);
139 $msg = removesig($ret['body']);
140 $ret['body'] = $msg['body'];
141 $ret['body'] = convertquote($ret['body'], $reply);
143 if (trim($html) != '') {
144 $ret['body'] = removelinebreak($ret['body']);
147 $ret['body'] = unifyattributionline($ret['body']);
152 // At the moment - only return plain/text.
153 // Later we'll repackage inline images as data url's and make the HTML safe
155 * @param object $mbox mailbox
156 * @param integer $uid user id
157 * @param object $p parts
158 * @param integer $partno part number
159 * @param string $subtype sub type
162 private static function messageGetPart($mbox, $uid, $p, $partno, $subtype)
164 // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
165 global $htmlmsg,$plainmsg,$charset,$attachments;
171 ? @imap_fetchbody($mbox, $uid, $partno, FT_UID|FT_PEEK)
172 : @imap_body($mbox, $uid, FT_UID|FT_PEEK);
174 // Any part may be encoded, even plain text messages, so check everything.
175 if ($p->encoding==4) {
176 $data = quoted_printable_decode($data);
177 } elseif ($p->encoding==3) {
178 $data = base64_decode($data);
182 // get all parameters, like charset, filenames of attachments, etc.
184 if ($p->parameters) {
185 foreach ($p->parameters as $x) {
186 $params[strtolower($x->attribute)] = $x->value;
190 if (isset($p->dparameters) && $p->dparameters) {
191 foreach ($p->dparameters as $x) {
192 $params[strtolower($x->attribute)] = $x->value;
197 // Any part with a filename is an attachment,
198 // so an attached text file (type 0) is not mistaken as the message.
200 if ((isset($params['filename']) && $params['filename']) || (isset($params['name']) && $params['name'])) {
201 // filename may be given as 'Filename' or 'Name' or both
202 $filename = ($params['filename'])? $params['filename'] : $params['name'];
203 // filename may be encoded, so see imap_mime_header_decode()
204 $attachments[$filename] = $data; // this is a problem if two files have same name
208 if ($p->type == 0 && $data) {
209 // Messages may be split in different parts because of inline attachments,
210 // so append parts together with blank row.
211 if (strtolower($p->subtype)==$subtype) {
212 $data = iconv($params['charset'], 'UTF-8//IGNORE', $data);
213 return (trim($data) ."\n\n");
218 // $htmlmsg .= $data ."<br><br>";
219 $charset = $params['charset']; // assume all parts are same charset
223 // Many bounce notifications embed the original message as type 2,
224 // but AOL uses type 1 (multipart), which is not handled here.
225 // There are no PHP functions to parse embedded messages,
226 // so this just appends the raw source to the main message.
227 // elseif ($p->type==2 && $data) {
228 // $plainmsg .= $data."\n\n";
232 if (isset($p->parts) && $p->parts) {
234 foreach ($p->parts as $partno0 => $p2) {
235 $x .= self::messageGetPart($mbox, $uid, $p2, $partno . '.' . ($partno0+1), $subtype); // 1.2, 1.2.1, etc.
245 * @param string $in_str in string
246 * @param string $charset character set
249 public static function encodeHeader($in_str, $charset)
252 $need_to_convert = false;
254 for ($x = 0; $x < strlen($in_str); $x ++) {
255 if ((ord($in_str[$x]) == 0) || ((ord($in_str[$x]) > 128))) {
256 $need_to_convert = true;
260 if (! $need_to_convert) {
264 if ($out_str && $charset) {
265 // define start delimimter, end delimiter and spacer
267 $start = "=?" . $charset . "?B?";
268 $spacer = $end . "\r\n " . $start;
270 // determine length of encoded text within chunks
271 // and ensure length is even
272 $length = 75 - strlen($start) - strlen($end);
275 [EDIT BY danbrown AT php DOT net: The following
276 is a bugfix provided by (gardan AT gmx DOT de)
277 on 31-MAR-2005 with the following note:
278 "This means: $length should not be even,
279 but divisible by 4. The reason is that in
280 base64-encoding 3 8-bit-chars are represented
281 by 4 6-bit-chars. These 4 chars must not be
282 split between two encoded words, according
285 $length = $length - ($length % 4);
287 // encode the string and split it into chunks
288 // with spacers after each chunk
289 $out_str = base64_encode($out_str);
290 $out_str = chunk_split($out_str, $length, $spacer);
292 // remove trailing spacer and
293 // add start and end delimiters
294 $spacer = preg_quote($spacer, '/');
295 $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
296 $out_str = $start . $out_str . $end;
302 * Function send is used by NETWORK_EMAIL and NETWORK_EMAIL2 code
303 * (not to notify the user, but to send items to email contacts)
305 * @param string $addr address
306 * @param string $subject subject
307 * @param string $headers headers
308 * @param array $item item
312 * @todo This could be changed to use the Emailer class
314 public static function send($addr, $subject, $headers, $item)
316 //$headers .= 'MIME-Version: 1.0' . "\n";
317 //$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
318 //$headers .= 'Content-Type: text/plain; charset=UTF-8' . "\n";
319 //$headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
321 $part = uniqid("", true);
323 $html = prepare_body($item);
325 $headers .= "Mime-Version: 1.0\n";
326 $headers .= 'Content-Type: multipart/alternative; boundary="=_'.$part.'"'."\n\n";
328 $body = "\n--=_".$part."\n";
329 $body .= "Content-Transfer-Encoding: 8bit\n";
330 $body .= "Content-Type: text/plain; charset=utf-8; format=flowed\n\n";
332 $body .= html2plain($html)."\n";
334 $body .= "--=_".$part."\n";
335 $body .= "Content-Transfer-Encoding: 8bit\n";
336 $body .= "Content-Type: text/html; charset=utf-8\n\n";
338 $body .= '<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">'.$html."</body></html>\n";
340 $body .= "--=_".$part."--";
342 //$message = '<html><body>' . $html . '</body></html>';
343 //$message = html2plain($html);
344 logger('notifier: email delivery to ' . $addr);
345 mail($addr, $subject, $body, $headers);
349 * @param string $iri string
352 public static function iri2msgid($iri)
354 if (!strpos($iri, "@")) {
355 $msgid = preg_replace("/urn:(\S+):(\S+)\.(\S+):(\d+):(\S+)/i", "urn!$1!$4!$5@$2.$3", $iri);
364 * @param string $msgid msgid
367 public static function msgid2iri($msgid)
369 if (strpos($msgid, "@")) {
370 $iri = preg_replace("/urn!(\S+)!(\d+)!(\S+)@(\S+)\.(\S+)/i", "urn:$1:$4.$5:$2:$3", $msgid);