Пример #1
0
function errSend()
{
    /* check for valid email settings */
    $settings = new dbSelect("esettings", "cubit");
    $settings->run();
    if ($settings->num_rows() <= 0) {
        r2sListSet("emailsettings");
        header("Location: email-settings.php");
        exit;
    }
    $settings->fetch_array();
    $server = $settings->d["smtp_host"];
    $from = $settings->d["fromname"];
    $reply = $settings->d["reply"];
    /* build the email */
    $data = errData($_GET["id"]);
    $msg = new clsMailMsg();
    $msg->newMessage($from, $reply, "Error Report: {$data['errtime']}", "Error report file attached.");
    $msg->addAttachment("application/octet-stream", "error{$_GET['id']}-{$data['errtime']}.cer", $data["errdata"]);
    $md = $msg->getNewMessage();
    /* send the email */
    /**
     * ok, so lets stop catching errors because if the email sending fails
     * we are just going to go back to "an error has occured"
     */
    disableErrorNet();
    $smtp = new clsSMTPMail();
    $smtp->sendMessages($server, 25, false, false, false, ERRORNET_EMAIL, $md["from"], $md["subject"], $md["body"], $md["headers"]);
    $OUTPUT = "<h3>Error Report</h3>";
    if ($smtp->bool_success !== true) {
        $OUTPUT .= "Error sending report. Please save report and email it\n\t\t\tto <a class='nav' href='mailto: " . ERRORNET_EMAIL . "'>" . ERRORNET_EMAIL . "</a><br />\n\t\t\t<br />\n\t\t\t<input type='button' value='Save Error Report'\n\t\t\t\tonClick='document.location.href=\"" . relpath("geterror.php") . "?id={$_GET['id']}\";' />";
    } else {
        $OUTPUT .= "Successfully sent report. Thank You.";
    }
    return $OUTPUT;
}
function send_mails()
{
    /* check for valid settings */
    $settings = new dbSelect("esettings", "cubit");
    $settings->run();
    if ($settings->num_rows() <= 0) {
        r2sListSet("emailsettings");
        header("Location: email-settings.php");
        exit;
    }
    /* send them */
    extract($_POST);
    require_lib("mail.smtp");
    $send = new clsSMTPMail();
    $settings->fetch_array();
    $server = $settings->d["smtp_host"];
    $from = $settings->d["fromname"];
    $reply = $settings->d["reply"];
    $content = chunk_split($emailsavepage_content);
    $boundary = md5($content) . "=:" . strlen($content);
    $headers = array();
    $headers[] = "From: {$from}";
    $headers[] = "Reply-To: {$reply}";
    $headers[] = "Content-Type: multipart/mixed; boundary=\"{$boundary}\"";
    $headers[] = "MIME-Version: 1.0";
    if (!isset($emailsavepage_mime)) {
        $attachmime = "text/html";
        $ext = ".html";
    } else {
        $attachmime = $emailsavepage_mime;
        if ($attachmime == "text/plain") {
            $ext = ".txt";
        } else {
            $ext = "";
        }
    }
    if ($emailsavepage_name == "") {
        $filename = "attachment{$ext}";
    } else {
        $filename = preg_replace("/.php\$/", "", $emailsavepage_name) . $ext;
    }
    // company image
    $get_img = "SELECT img, imgtype FROM compinfo LIMIT 1";
    $run_img = db_exec($get_img) or errDie("Unable to get company image information.");
    if (pg_numrows($run_img) > 0) {
        $carr = pg_fetch_array($run_img);
        // hack to limit a header line to 64 chars
        $temp = $carr['img'];
        $carr['img'] = "";
        $cnt = 0;
        for ($x = 0; $x <= strlen($temp); $x++) {
            $cnt++;
            $carr['img'] .= substr($temp, $x, 1);
            if ($cnt == 64) {
                $carr['img'] .= "\n";
                $cnt = 0;
            }
        }
        if (strlen($carr['img']) > 10) {
            if ($carr['imgtype'] == "image/jpeg") {
                $imgfilename = "logo.jpg";
            } elseif ($carr['imgtype'] == "image/png") {
                $imgfilename = "logo.png";
            } elseif ($carr['imgtype'] == "image/gif") {
                $imgfilename = "logo.gif";
            }
            $imagemsg = "Content-Type: {$carr['imgtype']}; charset=UTF-8\r\n" . "Content-Transfer-Encoding: base64\r\n" . "Content-Disposition: attachment; filename=\"{$imgfilename}\"" . "\r\n\r\n" . "{$carr['img']}\n";
            $content = base64_encode(str_replace("compinfo/getimg.php", "{$imgfilename}", base64_decode($content)));
        }
    }
    // hack to limit a header line to 64 chars
    $temp = $content;
    $content = "";
    $cnt = 0;
    for ($x = 0; $x <= strlen($temp); $x++) {
        $cnt++;
        $content .= substr($temp, $x, 1);
        if ($cnt == 64) {
            $content .= "\n";
            $cnt = 0;
        }
    }
    // the actual page
    $pagecontent = "Content-Type: {$attachmime}; charset=UTF-8\r\n" . "Content-Transfer-Encoding: base64\r\n" . "Content-Disposition: attachment; filename=\"{$filename}\"" . "\r\n\r\n" . "{$content}";
    $msg = "--{$boundary}\n" . "Content-Type: text/plain; charset=UTF-8\r\n\nDocument Attached\n\n" . "--{$boundary}\n" . "{$pagecontent}\n\n" . "--{$boundary}\n";
    if (isset($imagemsg) and strlen($imagemsg) > 0) {
        $msg .= "{$imagemsg}\n" . "--{$boundary}--\n";
    }
    $OUT = "\n\t<table " . TMPL_tblDflts . ">\n\t\t<tr>\n\t\t\t<th>Customer</th>\n\t\t\t<th>Email Status</th>\n\t\t</tr>";
    foreach ($emailcust as $cusnum => $email) {
        $custheaders = implode("\r\n", $headers);
        $custheaders .= "\r\nTo: \"{$surnames[$cusnum]}\" <{$email}>";
        $ret = $send->sendMessages($server, 25, "", "", "", $email, $from, $emailsavepage_subject, $msg, $custheaders);
        $redir = "";
        if ($cusnum == "custom_address") {
            $redir = "\n\t\t\t\t<td valign='center'>\n\t\t\t\t\t<form action='customers-new.php' method='POST'>\n\t\t\t\t\t\t<input type='hidden' name='surname' value='{$surnames[$cusnum]}'>\n\t\t\t\t\t\t<input type='hidden' name='email' value='{$email}'>\n\t\t\t\t\t\t<input type='submit' value='Add As Customer'>\n\t\t\t\t\t</form>\n\t\t\t\t</td>";
        }
        $OUT .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$surnames[$cusnum]}</td>\n\t\t\t\t<td>{$ret}</td>\n\t\t\t\t{$redir}\n\t\t\t</tr>";
    }
    $OUT .= "</table><br>" . mkQuickLinks();
    return $OUT;
}
/**
 * sends a trh message.
 *
 * if you dont pass the trh config variable, it will be fetched automatically.
 *
 * @param string $who must be "supp" or "cust", what other side is too me
 * @param int $id customer/supplier id
 * @param string $msgtype 6 character message type
 * @param string $data what to send
 * @param array $config optionally the companies trh configuration
 * @return bool
 */
function send_trhmsg($who, $id, $email, $msgtype, $data, $config = false)
{
    if (defined("CONSOLE")) {
        print "sending response:\n";
        print "\twho: {$who}\n";
        print "\tid: {$id}\n";
        print "\tmsgtype: {$msgtype}\n";
        print "\tdata: {$data}\n";
    }
    require_lib("mail.smtp");
    require_lib("mail.msg");
    /* configuration */
    if ($config === false) {
        $config = getTrhConfig();
    }
    extract($config);
    /* determines new message parameters */
    $subject = "trh_{$msgtype}";
    $body = array();
    $attachments = array();
    $headers = array();
    $compinfo = qryCompInfo();
    /* determine my ip address */
    if (empty($_SERVER["SERVER_NAME"])) {
        $myipaddr = "0.0.0.0";
    } else {
        $myipaddr = gethostbyname($_SERVER['SERVER_NAME']);
    }
    /* determine key request function */
    if ($who == "supp") {
        $keyFunc = "trhKeySupp";
    } else {
        $keyFunc = "trhKeyCust";
    }
    /* standard message body details */
    $body[] = "compname: {$compinfo['compname']}";
    $body[] = "ipaddr: {$myipaddr}";
    $body[] = "bustel: {$compinfo['tel']}";
    $body[] = "fromwho: " . ($who == "supp" ? "cust" : "supp");
    $body[] = "email: {$SMTP_FROM}";
    switch ($msgtype) {
        /* request a new key */
        case "reqkey":
            $subject .= " {$data}";
            break;
            /* respond to a newly requested key */
        /* respond to a newly requested key */
        case "rspkey":
            /* get my key from this guy */
            $qry = new dbSelect("keys", "trh", grp(m("cols", "(key).send_key AS mykey"), m("where", "{$who}id='{$id}'")));
            $qry->run();
            if ($qry->num_rows() <= 0) {
                return false;
            }
            $d = $qry->fetch_array();
            $mykey = $d["mykey"];
            $subject .= " {$data}";
            $body[] = "mykey: {$mykey}";
            break;
            /* requests a new purchase */
        /* requests a new purchase */
        case "reqpur":
            $keys = $keyFunc($id, "recv_key, send_key");
            $subject .= " {$keys['send_key']}";
            $body[] = "yourkey: {$keys['recv_key']}";
            $attachments["data.xml"] = $data;
            break;
            /* responds to a new purchase request */
        /* responds to a new purchase request */
        case "rsppur":
            $keys = $keyFunc($id, "recv_key, send_key");
            $subject .= " {$keys['send_key']}";
            $body[] = "yourkey: {$keys['recv_key']}";
            $body[] = "purid: {$data['purid']}";
            $body[] = "status: {$data['status']}";
            break;
    }
    /* create new mail message */
    $msg = new clsMailMsg();
    $msg->newMessage($SMTP_FROM, $SMTP_FROM, $subject, implode("\n", $body), $headers, true);
    if (count($attachments)) {
        foreach ($attachments as $fname => $contents) {
            $msg->addAttachment("text/plain", $fname, $data);
        }
    }
    $smtp = new clsSMTPMail();
    $ret = $smtp->sendMessages($SMTP_SERVER, 25, !empty($SMTP_USER), $SMTP_USER, $SMTP_PASS, $email, $msg->getNewMessage());
    if ($smtp->bool_success !== true) {
        return $ret;
    }
    return true;
}