X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=functions%2Fsmtp.php;h=04e92bf0e050717688167f3e7977c327fcfbc0f0;hb=0d273153834be125b51772ca39ffdcbda213b7e1;hp=9ca0091821ba80207b37bbc8177f1c630ae60a28;hpb=c99c5b31ea7ce697fb71dfbfa2b4bb449e53260f;p=squirrelmail.git diff --git a/functions/smtp.php b/functions/smtp.php index 9ca00918..04e92bf0 100644 --- a/functions/smtp.php +++ b/functions/smtp.php @@ -5,72 +5,240 @@ ** an smtp server or sendmail. **/ - function sendMessage($t, $c, $b, $subject, $body) { - global $useSendmail; + $smtp_php = true; - if ($useSendmail==true) { - sendSendmail($t, $c, $b, $subject, $body); - } else { - sendSMTP($t, $c, $b, $subject, $body); + // Returns true only if this message is multipart + function isMultipart () { + global $attachments; + + if (count($attachments)>0) + return true; + else + return false; + } + + // Attach the files that are due to be attached + function attachFiles ($fp) { + global $attachments, $attachment_dir; + + $length = 0; + + if (isMultipart()) { + reset($attachments); + while (list($localname, $remotename) = each($attachments)) { + // This is to make sure noone is giving a filename in another + // directory + $localname = ereg_replace ("\\/", "", $localname); + + $fileinfo = fopen ($attachment_dir.$localname.".info", "r"); + $filetype = fgets ($fileinfo, 8192); + fclose ($fileinfo); + $filetype = trim ($filetype); + if ($filetype=="") + $filetype = "application/octet-stream"; + + $header = "--".mimeBoundary()."\r\n"; + $header .= "Content-Type: $filetype\r\n"; + $header .= "Content-Disposition: attachment; filename=\"$remotename\"\r\n"; + $header .= "Content-Transfer-Encoding: base64\r\n\r\n"; + fputs ($fp, $header); + $length += strlen($header); + + $file = fopen ($attachment_dir.$localname, "r"); + while ($tmp = fread($file, 570)) { + $encoded = chunk_split(base64_encode($tmp)); + $length += strlen($encoded); + fputs ($fp, $encoded); + } + fclose ($file); + } } - + + return $length; } - function write822Header ($fp, $t, $c, $b, $subject) { - global $REMOTE_ADDR, $SERVER_NAME; - global $data_dir, $username, $domain, $version; + // Delete files that are uploaded for attaching + function deleteAttachments() { + global $attachments, $attachment_dir; + + if (isMultipart()) { + reset($attachments); + while (list($localname, $remotename) = each($attachments)) { + if (!ereg ("\\/", $localname)) { + unlink ($attachment_dir.$localname); + unlink ($attachment_dir.$localname.".info"); + } + } + } + } - $to = parseAddrs($t); - $cc = parseAddrs($c); - $bcc = parseAddrs($b); - $from_addr = "$username@$domain"; - $reply_to = getPref($data_dir, $username, "reply_to"); - $from = getPref($data_dir, $username, "full_name"); + // Return a nice MIME-boundary + function mimeBoundary () { + global $version, $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT; - $to_list = getLineOfAddrs($to); - $cc_list = getLineOfAddrs($cc); - $bcc_list = getLineOfAddrs($bcc); + static $mimeBoundaryString; + + if ($mimeBoundaryString == "") { + $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME. + $REMOTE_PORT; + $mimeBoundaryString = "=-_+".substr(md5($temp),1,20); + } + + return $mimeBoundaryString; + } - if ($from == "") - $from = "<$from_addr>"; + /* Time offset for correct timezone */ + function timezone () { + $diff_second = date("Z"); + if ($diff_second > 0) + $sign = "+"; else - $from = $from . " <$from_addr>"; + $sign = "-"; - $date = date("D, j M Y H:i:s +0000", gmmktime()); + $diff_second = abs($diff_second); - /* Make a RFC822 Received: line */ - fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; "); - fputs ($fp, "$date\n"); + $diff_hour = floor ($diff_second / 3600); + $diff_minute = floor (($diff_second-3600*$diff_hour) / 60); - fputs ($fp, "Date: $date\n"); - fputs ($fp, "Subject: $subject\n"); // Subject - fputs ($fp, "From: $from\n"); // Subject - fputs ($fp, "To: $to_list\n"); // Who it's TO + $zonename = "(".strftime("%Z").")"; + $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename); + return ($result); + } - if ($cc_list) { - fputs($fp, "Cc: $cc_list\n"); // Who the CCs are - } - if ($bcc_list) { - fputs($fp, "Bcc: $bcc_list\n"); // BCCs is removed from header by sendmail + /* Print all the needed RFC822 headers */ + function write822Header ($fp, $t, $c, $b, $subject) { + global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT; + global $data_dir, $username, $domain, $version, $useSendmail; + global $default_charset; + + // Storing the header to make sure the header is the same + // everytime the header is printed. + static $header, $headerlength; + + if ($header == "") { + $to = parseAddrs($t); + $cc = parseAddrs($c); + $bcc = parseAddrs($b); + $reply_to = getPref($data_dir, $username, "reply_to"); + $from = getPref($data_dir, $username, "full_name"); + $from_addr = getPref($data_dir, $username, "email_address"); + + if ($from_addr == "") + $from_addr = "$username@$domain"; + + $to_list = getLineOfAddrs($to); + $cc_list = getLineOfAddrs($cc); + $bcc_list = getLineOfAddrs($bcc); + + if ($from == "") + $from = "<$from_addr>"; + else + $from = $from . " <$from_addr>"; + + /* This creates an RFC 822 date */ + $date = date("D, j M Y H:i:s ", mktime()) . timezone(); + + /* Create a message-id */ + $message_id = "<" . $REMOTE_PORT . "." . $REMOTE_ADDR . "."; + $message_id .= time() . "@" . $SERVER_NAME .">"; + + /* Make an RFC822 Received: line */ + $header = "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; "; + $header .= "$date\r\n"; + + /* Insert the rest of the header fields */ + $header .= "Message-ID: $message_id\r\n"; + $header .= "Date: $date\r\n"; + $header .= "Subject: $subject\r\n"; + $header .= "From: $from\r\n"; + $header .= "To: $to_list \r\n"; // Who it's TO + + if ($cc_list) { + $header .= "Cc: $cc_list\r\n"; // Who the CCs are + } + + if ($reply_to != "") + $header .= "Reply-To: $reply_to\r\n"; + + if ($useSendmail) { + if ($bcc_list) { + // BCCs is removed from header by sendmail + $header .= "Bcc: $bcc_list\r\n"; + } + } + + $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail + + // Do the MIME-stuff + $header .= "MIME-Version: 1.0\r\n"; + + if (isMultipart()) { + $header .= "Content-Type: multipart/mixed; boundary=\""; + $header .= mimeBoundary(); + $header .= "\"\r\n"; + } else { + if ($default_charset != "") + $header .= "Content-Type: text/plain; charset=$default_charset\r\n"; + else + $header .= "Content-Type: text/plain;\r\n"; + $header .= "Content-Transfer-Encoding: 8bit\r\n"; + } + $header .= "\r\n"; // One blank line to separate header and body + + $headerlength = strlen($header); + } + + // Write the header + fputs ($fp, $header); + + return $headerlength; + } + + // Send the body + function writeBody ($fp, $passedBody) { + global $default_charset; + + $attachmentlength = 0; + + if (isMultipart()) { + $body = "--".mimeBoundary()."\r\n"; + + if ($default_charset != "") + $body .= "Content-Type: text/plain; charset=$default_charset\r\n"; + else + $body .= "Content-Type: text/plain\r\n"; + + $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; + $body .= stripslashes($passedBody) . "\r\n"; + fputs ($fp, $body); + + $attachmentlength = attachFiles($fp); + + $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n"; + fputs ($fp, $postbody); + } else { + $body = stripslashes($passedBody) . "\r\n"; + fputs ($fp, $body); + $postbody = "\r\n"; + fputs ($fp, $postbody); } - fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail - fputs($fp, "MIME-Version: 1.0\n"); - fputs($fp, "Content-Type: text/plain\n"); - if ($reply_to != "") - fputs($fp, "Reply-To: $reply_to\n"); + + return (strlen($body) + strlen($postbody) + $attachmentlength); } // Send mail using the sendmail command function sendSendmail($t, $c, $b, $subject, $body) { global $sendmail_path, $username, $domain; - + // open pipe to sendmail - $fp = popen ("$sendmail_path -t -f$username@$domain", "w"); + $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w"); - write822Header ($fp, $t, $c, $b, $subject); - fputs($fp, "\n$body\n"); // send the body of the message + $headerlength = write822Header ($fp, $t, $c, $b, $subject); + $bodylength = writeBody($fp, $body); pclose($fp); + + return ($headerlength + $bodylenght); } function smtpReadData($smtpConnection) { @@ -85,12 +253,16 @@ } function sendSMTP($t, $c, $b, $subject, $body) { - global $username, $domain, $version, $smtpServerAddress, $smtpPort; + global $username, $domain, $version, $smtpServerAddress, $smtpPort, + $data_dir, $color; $to = parseAddrs($t); $cc = parseAddrs($c); $bcc = parseAddrs($b); - $from_addr = "$username@$domain"; + $from_addr = getPref($data_dir, $username, "email_address"); + + if ($from_addr == "") + $from_addr = "$username@$domain"; $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString); if (!$smtpConnection) { @@ -105,55 +277,58 @@ $cc_list = getLineOfAddrs($cc); /** Lets introduce ourselves */ - fputs($smtpConnection, "HELO $domain\n"); + fputs($smtpConnection, "HELO $domain\r\n"); $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); errorCheck($tmp); /** Ok, who is sending the message? */ - fputs($smtpConnection, "MAIL FROM:<$from_addr>\n"); + fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n"); $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); errorCheck($tmp); /** send who the recipients are */ for ($i = 0; $i < count($to); $i++) { - fputs($smtpConnection, "RCPT TO:<$to[$i]>\n"); + fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n"); $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); errorCheck($tmp); } for ($i = 0; $i < count($cc); $i++) { - fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n"); + fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n"); $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); errorCheck($tmp); } for ($i = 0; $i < count($bcc); $i++) { - fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n"); + fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n"); $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); errorCheck($tmp); } /** Lets start sending the actual message */ - fputs($smtpConnection, "DATA\n"); + fputs($smtpConnection, "DATA\r\n"); $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); errorCheck($tmp); - write822Header ($smtpConnection, $t, $c, $b, $subject); - - fputs($smtpConnection, "$body\n"); // send the body of the message + // Send the message + $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject); + $bodylength = writeBody($smtpConnection, $body); - fputs($smtpConnection, ".\n"); // end the DATA part + fputs($smtpConnection, ".\r\n"); // end the DATA part $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); $num = errorCheck($tmp); if ($num != 250) { - echo "ERROR
Message not sent!
Reason given: $tmp
"; + echo "ERROR
Message not sent!
Reason given: $tmp
"; } - fputs($smtpConnection, "QUIT\n"); // log off + fputs($smtpConnection, "QUIT\r\n"); // log off fclose($smtpConnection); + + return ($headerlength + $bodylength); } function errorCheck($line) { + global $color; // Status: 0 = fatal // 5 = ok @@ -235,7 +410,7 @@ } if ($status == 0) { - echo ""; + echo ""; echo ""; echo "
ERROR

"; echo "   Error Number: $err_num
"; @@ -247,4 +422,27 @@ } return $err_num; } + + function sendMessage($t, $c, $b, $subject, $body) { + global $useSendmail; + global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort; + + if ($useSendmail==true) { + $length = sendSendmail($t, $c, $b, $subject, $body); + } else { + $length = sendSMTP($t, $c, $b, $subject, $body); + } + + $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1); + sqimap_append ($imap_stream, $sent_folder, $length); + write822Header ($imap_stream, $t, $c, $b, $subject); + writeBody ($imap_stream, $body); + sqimap_append_done ($imap_stream); + + + // Delete the files uploaded for attaching (if any). + deleteAttachments(); + + } + ?>