/** * function sends missed calls of user to given email address * * @param string $uid * @param string $email_address * @return bool TRUE on success, FALSE on error */ function send_mail_with_missed_calls($uid, $email_address, $mail_from) { global $config, $data, $lang_set; /* get missed calls */ if (false === ($missed_calls = $data->get_missed_calls_of_yesterday($uid, null))) { return false; } if (!count($missed_calls)) { return true; } //there are no missed calls - nothing to do /* check if imap extension is loaded */ if (!function_exists('imap_mail_compose')) { ErrorHandler::add_errors("Can not send mail. IMAP extension for PHP is not installed."); return false; } /* * Create table of missed calls */ $table = '<html><body><table border="1" cellspacing="0" cellpadding="1">' . "\n"; $table .= '<tr>'; $table .= '<th>calling subscriber</th>'; $table .= '<th>time</th>'; $table .= '<th>reply status</th>'; $table .= '</tr>' . "\n"; foreach ($missed_calls as $row) { $table .= '<tr>'; $table .= '<td>' . $row->from_uri . ' </td>'; $table .= '<td>' . $row->request_timestamp . ' </td>'; $table .= '<td>' . $row->sip_status . ' </td>'; $table .= '</tr>' . "\n"; } $table .= '</table></body></html>' . "\n"; /* * Get language of user */ if (false === ($lang = Attributes::get_attribute($config->attr_names['lang'], array("uid" => $uid)))) { return false; } $lang = lang_detect($lang, 3); //translate $lang to be a index into $available_languages array if (!$lang) { $lang = $config->default_lang; } /* * Read file containing the mail body */ $mail_file = multidomain_get_lang_file("mail_missed_calls.txt", "txt", $lang); $m = read_txt_file($mail_file, array()); if ($m === false) { ErrorHandler::add_error("Can't read file with mail body."); return false; } /* get charset */ $charset = null; if (isset($m['headers']['content-type']) and eregi("charset=([-a-z0-9]+)", $m['headers']['content-type'], $regs)) { $charset = $regs[1]; } /* add information about charset to the header */ if ($charset) { $m['headers']['subject'] = "=?" . $charset . "?Q?" . imap_8bit($m['headers']['subject']) . "?="; } /* * Compose the mail message */ if ($mail_from) { $envelope["from"] = $mail_from; } else { $envelope["from"] = $config->mail_header_from; } $envelope["to"] = $email_address; $part1["type"] = TYPEMULTIPART; $part1["subtype"] = "mixed"; $part2["type"] = TYPETEXT; $part2["subtype"] = "plain"; $part2["contents.data"] = $m['body']; if ($charset) { $part2["charset"] = $charset; } $part3["type"] = TYPETEXT; $part3["subtype"] = "html"; $part3["contents.data"] = $table; $part3["charset"] = $lang_set['charset']; $body[1] = $part1; $body[2] = $part2; $body[3] = $part3; $mail = imap_mail_compose($envelope, $body); list($m_header, $m_body) = split("\r\n\r\n", $mail, 2); /* * Send mail */ if (!mail($email_address, $m['headers']['subject'], $m_body, $m_header)) { $errors[] = "can't send missed calls to " . $email_address; } return true; }
/** * Read txt file in specified language mutation, parse and saparate to headers and body and do replacements. * * For more info about choice txt file read {@link multidomain_get_lang_file} * Txt files in serweb (as emails, terms and conditions etc.) are stored in * special format. At the beginning (but only at beginning) of these files may * be comments. Lines with comments begins by "#". Rest of file is separated * into two parts separated by empty line: headers and body. * * Each header contain header name and header value. Each header must be on * own line. Header name and header value is separated by ":". * * Body is the rest of txt file after first empty line. * * When txt file is readed, function replace all strings in form #@#some_name#@# * by replacement. The parametr $replacements is array of pairs. First element * of each pair is name of replacement and second element from pair is value * by which is replaced. * * Function's finding replacements in body and in header values. * * Function return array with two keys: "headers" and "body". Body is only * string. Headers contain associative array with header names as keys. * * @param string $filename name of file is searching for * @param string $ddir subdirectory in of domain dir * @param string $lang language in "official" ISO 639 language code see {@link config_lang.php} for more info * @param array $replacements see above * @return array parsed file or false on error */ function read_lang_txt_file($filename, $ddir, $lang, $replacements) { $f = multidomain_get_lang_file($filename, $ddir, $lang); if (!$f) { sw_log("Can't find txt file " . $filename . ", subdir:" . $ddir . ", lang:" . $lang, PEAR_LOG_ERR); return false; } return read_txt_file($f, $replacements); }