<?php   

include("global.inc.php");

function removecrlf($string)
{
    return strtr($string, "\015\012", "  ");
}

function cleanaddress($adr)
{
    global $domain;
    $a = stripslashes($adr);
    $a = removecrlf($a);
    if (!strstr($a, "@"))
    {
        $a .= "@" . $domain;
    }
    return $a;
}

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 (SERVER_SUFFIX)
    $domain = SERVER_SUFFIX;
else
    $domain = IMAP_SERVER;

if (FORCE_FROM)
{
    $mailheaders .= "From: $user@$domain" . "\r\n";
}
if ($to != "")
    $mailheaders .= "To: ". cleanaddress($to) . "\r\n";
if ($cc != "")
    $mailheaders .= "Cc: " . cleanaddress($cc) . "\r\n";
if ($bcc != "")
    $mailheaders .= "Bcc: " . cleanaddress($bcc) . "\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";

$from_user = FORCE_FROM ? "$user@$domain" : $user;

mailfrom($from_user, $msg_body, $mailheaders);

// add message to the sent mail folder
if (SENT_MAIL)
{
    $mailbox = mailbox_log_in($user, $pass, 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($user, $pass, SENT_MAIL);
    }

    imap_append($mailbox, IMAP_STR. "$snt_folder", $mailheaders . $msg_body);
}

if (is_array($msglist))
{
    $url = "message.php?folder=" . urlencode($folder);
    foreach ($msglist as $m)   
    {
        $url .= "&msglist[]=$m";
    }
    if ($msgnum != "")
    {
        $url .= "#$msgnum";
    }
    header("Location: $url");  
}
else
{
    header("Location: index.php?folder=" . $folder);  
}
?>
