<?php   

    include("global.inc");

    function mailfrom($fromaddress, $body, $headers)
    {
        // -t means get To, CC and BCC from message
        // -i means ignore single dots on line, read till EOF
        // -f forces fromaddress
        $fp = popen("/usr/sbin/sendmail -t -i -f" . $fromaddress, "w");

        fputs($fp, $headers);
        fputs($fp, $body);
        fputs($fp, "\r\n");

        pclose($fp);
    }

    /* A boundary must be unlikely to appear in the body. "=_" can never
       occur in qprint or bas64 encoded parts. The user could still type
       this, but '=' is converted in qprint and will never appear
       in bas64 except in the end.
    */
    $boundary = "=_Message-Boundary_=";
    $mailheaders = "";

    if (FORCE_FROM && is_string(SERVER_SUFFIX)) {
        $mailheaders .= "From: $user@".SERVER_SUFFIX."\r\n";
        //$mailheaders .= "Reply-To: $user@".SERVER_SUFFIX."\r\n";
    }

    $to = stripslashes($to);
    $to = removecrlf($to);
    if (!strstr($to, "@"))
    {
        if (is_string(SERVER_SUFFIX)) {
echo "Using server suffix " . SERVER_SUFFIX . "<br>\n";
            $to .= "@" . SERVER_SUFFIX;
        } else {
echo "Defaulting to IMAP server " . IMAP_SERVER . "<br>\n";
            $to .= "@" . IMAP_SERVER;
        }
    }
    $mailheaders .= "To: ".removecrlf($to)."\r\n";
    
    $cc = stripslashes($cc);
    $mailheaders .= "Cc: ".removecrlf($cc)."\r\n";

    // Do subject earlier to accomodate buggy Mozilla mail summary box,
    // according to Laird Bedore (lmbedore@vectorstar.com) 
    $mailheaders .= "Subject: " . removecrlf(stripslashes($subject)) . "\r\n";

    $mailheaders .= "X-Mailer: " . VERSION . "\r\n";

    $msg_body = stripslashes($body);
    $msg_body = imap_8bit($msg_body);

    if (( $attach != "none" ) && ( $attach != "" ) and (is_uploaded_file($attach)))
    {
        $file = fopen($attach, "r");
        $contents = fread($file, $attach_size);
        $encoded_attach = chunk_split(base64_encode($contents));
        fclose($file);
        
        $mailheaders .= "MIME-version: 1.0\r\n";
        $mailheaders .= "Content-type: multipart/mixed; ";
        $mailheaders .= "boundary=\"$boundary\"\r\n";
        $mailheaders .= "Content-transfer-encoding: 7BIT\r\n";
        $mailheaders .= "X-attachments: $attach_name\r\n";

        $body_top = "--$boundary\r\n";
        $body_top .= "Content-type: text/plain; charset=UTF-8\r\n";
        $body_top .= "Content-transfer-encoding: quoted-printable\r\n";
        $body_top .= "Content-description: Mail message body\r\n\r\n";

        $msg_body = $body_top . $msg_body;

        $msg_body .= "\r\n\r\n--$boundary\r\n";
        $msg_body .= "Content-type: $attach_type; name=\"$attach_name\"\r\n";       
        $msg_body .= "Content-Transfer-Encoding: BASE64\r\n";
        $msg_body .= "Content-disposition: attachment; filename=\"$attach_name\"\r\n\r\n";
        $msg_body .= "$encoded_attach\r\n";
        $msg_body .= "--$boundary--\r\n";
    }
    else
    {
        $mailheaders .= "MIME-version: 1.0\r\n";
        $mailheaders .= "Content-type: text/plain; charset=UTF-8\r\n";
        $mailheaders .= "Content-transfer-encoding: quoted-printable\r\n";
    }

    $mailheaders .= "\r\n";

    if (is_string(SERVER_SUFFIX))
        $from_user = FORCE_FROM ? "$user@" . SERVER_SUFFIX : $user;
    else
        $from_user = FORCE_FROM ? "$user@" . IMAP_SERVER : $user;
    mailfrom($from_user, $msg_body, $mailheaders);

    // add message to the sent mail folder
    if(is_string(SENT_MAIL))
    {
        $mailbox = mailbox_log_in(SENT_MAIL);
        $snt_folder = construct_folder_str(SENT_MAIL);

        if(imap_last_error())
        {
            //if there was an error, it was because there was no sent mail folder
            //we'll just create one here, then re-login

            imap_createmailbox($mailbox, IMAP_STR . "$snt_folder");
            $mailbox = mailbox_log_in(SENT_MAIL);
        }

        if (is_string(SERVER_SUFFIX))
            $waxer = imap_append($mailbox, IMAP_STR. "$snt_folder", "From: $user@" . SERVER_SUFFIX . "\r\n" . $mailheaders . $msg_body);
        else
            $waxer = imap_append($mailbox, IMAP_STR. "$snt_folder", "From: $user@" . IMAP_SERVER . "\r\n" . $mailheaders . $msg_body);
    }

    if ($msgnum != "")
    {
        header("Location: message.php?folder=" . $folder . "&msgnum=" . $msgnum);  
    }
    else
    {
        header("Location: index.php?folder=" . $folder);  
    }
?>
