4 ** This contains all the functions needed to send messages through
5 ** an smtp server or sendmail.
10 // Returns true only if this message is multipart
11 function isMultipart () {
14 if (count($attachments)>0)
20 // Attach the files that are due to be attached
21 function attachFiles ($fp) {
22 global $attachments, $attachment_dir;
28 while (list($localname, $remotename) = each($attachments)) {
29 // This is to make sure noone is giving a filename in another
31 $localname = ereg_replace ("\\/", "", $localname);
33 $fileinfo = fopen ($attachment_dir.$localname.".info", "r");
34 $filetype = fgets ($fileinfo, 8192);
36 $filetype = trim ($filetype);
38 $filetype = "application/octet-stream";
40 $header = "--".mimeBoundary()."\r\n";
41 $header .= "Content-Type: $filetype\r\n";
42 $header .= "Content-Disposition: attachment; filename=\"$remotename\"\r\n";
43 $header .= "Content-Transfer-Encoding: base64\r\n\r\n";
45 $length +
= strlen($header);
47 $file = fopen ($attachment_dir.$localname, "r");
48 while ($tmp = fread($file, 570)) {
49 $encoded = chunk_split(base64_encode($tmp));
50 $length +
= strlen($encoded);
51 fputs ($fp, $encoded);
60 // Delete files that are uploaded for attaching
61 function deleteAttachments() {
62 global $attachments, $attachment_dir;
66 while (list($localname, $remotename) = each($attachments)) {
67 if (!ereg ("\\/", $localname)) {
68 unlink ($attachment_dir.$localname);
69 unlink ($attachment_dir.$localname.".info");
75 // Return a nice MIME-boundary
76 function mimeBoundary () {
77 global $version, $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
79 static $mimeBoundaryString;
81 if ($mimeBoundaryString == "") {
82 $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
84 $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
87 return $mimeBoundaryString;
90 /* Time offset for correct timezone */
91 function timezone () {
92 $diff_second = date("Z");
98 $diff_second = abs($diff_second);
100 $diff_hour = floor ($diff_second / 3600);
101 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
103 $zonename = "(".strftime("%Z").")";
104 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
108 /* Print all the needed RFC822 headers */
109 function write822Header ($fp, $t, $c, $b, $subject, $more_headers) {
110 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
111 global $data_dir, $username, $domain, $version, $useSendmail;
112 global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
115 // Storing the header to make sure the header is the same
116 // everytime the header is printed.
117 static $header, $headerlength;
120 $to = parseAddrs($t);
121 $cc = parseAddrs($c);
122 $bcc = parseAddrs($b);
123 $reply_to = getPref($data_dir, $username, "reply_to");
124 $from = getPref($data_dir, $username, "full_name");
125 $from_addr = getPref($data_dir, $username, "email_address");
127 if ($from_addr == "")
128 $from_addr = "$username@$domain";
130 $to_list = getLineOfAddrs($to);
131 $cc_list = getLineOfAddrs($cc);
132 $bcc_list = getLineOfAddrs($bcc);
134 /* Encoding 8-bit characters and making from line */
135 $subject = sqStripSlashes(encodeHeader($subject));
137 $from = "<$from_addr>";
139 $from = "\"" . encodeHeader($from) . "\" <$from_addr>";
141 /* This creates an RFC 822 date */
142 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
144 /* Create a message-id */
145 $message_id = "<" . $REMOTE_PORT . "." . $REMOTE_ADDR . ".";
146 $message_id .= time() . ".squirrel@" . $SERVER_NAME .">";
148 /* Make an RFC822 Received: line */
149 if (isset($REMOTE_HOST))
150 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
152 $received_from = $REMOTE_ADDR;
154 if (isset($HTTP_VIA) ||
isset ($HTTP_X_FORWARDED_FOR)) {
155 if ($HTTP_X_FORWARDED_FOR == "")
156 $HTTP_X_FORWARDED_FOR = "unknown";
157 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
160 $header = "Received: from $received_from\r\n";
161 $header .= " (SquirrelMail authenticated user $username)\r\n";
162 $header .= " by $SERVER_NAME with HTTP;\r\n";
163 $header .= " $date\r\n";
165 /* Insert the rest of the header fields */
166 $header .= "Message-ID: $message_id\r\n";
167 $header .= "Date: $date\r\n";
168 $header .= "Subject: $subject\r\n";
169 $header .= "From: $from\r\n";
170 $header .= "To: $to_list \r\n"; // Who it's TO
172 /* Insert headers from the $more_headers array */
173 if(is_array($more_headers)) {
174 reset($more_headers);
175 while(list($h_name, $h_val) = each($more_headers)) {
176 $header .= sprintf("%s: %s\r\n", $h_name, $h_val);
181 $header .= "Cc: $cc_list\r\n"; // Who the CCs are
185 $header .= "Reply-To: $reply_to\r\n";
189 // BCCs is removed from header by sendmail
190 $header .= "Bcc: $bcc_list\r\n";
194 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
197 $header .= "MIME-Version: 1.0\r\n";
200 $header .= "Content-Type: multipart/mixed; boundary=\"";
201 $header .= mimeBoundary();
204 if ($default_charset != "")
205 $header .= "Content-Type: text/plain; charset=$default_charset\r\n";
207 $header .= "Content-Type: text/plain;\r\n";
208 $header .= "Content-Transfer-Encoding: 8bit\r\n";
210 $header .= "\r\n"; // One blank line to separate header and body
212 $headerlength = strlen($header);
216 fputs ($fp, $header);
218 return $headerlength;
222 function writeBody ($fp, $passedBody) {
223 global $default_charset;
225 $attachmentlength = 0;
228 $body = "--".mimeBoundary()."\r\n";
230 if ($default_charset != "")
231 $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
233 $body .= "Content-Type: text/plain\r\n";
235 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
236 $body .= sqStripSlashes($passedBody) . "\r\n";
239 $attachmentlength = attachFiles($fp);
241 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
242 fputs ($fp, $postbody);
244 $body = sqStripSlashes($passedBody) . "\r\n";
247 fputs ($fp, $postbody);
250 return (strlen($body) +
strlen($postbody) +
$attachmentlength);
253 // Send mail using the sendmail command
254 function sendSendmail($t, $c, $b, $subject, $body, $more_headers) {
255 global $sendmail_path, $username, $domain;
257 // open pipe to sendmail
258 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
260 $headerlength = write822Header ($fp, $t, $c, $b, $subject, $more_headers);
261 $bodylength = writeBody($fp, $body);
265 return ($headerlength +
$bodylength);
268 function smtpReadData($smtpConnection) {
269 $read = fgets($smtpConnection, 1024);
273 $data[$counter] = $read;
274 $read = fgets($smtpConnection, 1024);
279 function sendSMTP($t, $c, $b, $subject, $body, $more_headers) {
280 global $username, $domain, $version, $smtpServerAddress, $smtpPort,
283 $to = parseAddrs($t);
284 $cc = parseAddrs($c);
285 $bcc = parseAddrs($b);
286 $from_addr = getPref($data_dir, $username, "email_address");
290 * A patch from Bill Thousand <billyt@claritytech.com>
292 * "I don't know if anyone else needs this or not, but it totally makes squirrelmail usable for us.
293 * This quick patch checks the username and from address for the domain information. We use
294 * a virtual domain patch for our imap server that allows multiple domains by using username@domain.com
295 * as the login username."
297 if ($from_addr == "") {
298 if (strstr($username, "@")) {
299 $from_addr = $username;
300 $address_pieces = explode("@",$username);
301 $domain = $address_pieces[1];
303 $from_addr = "$username@$domain";
306 // If the From Address is specified, use the domain in the from
307 // address if it's there.
308 if (strstr($from_addr, "@")) {
309 $address_pieces = explode("@", $from_addr);
310 $domain = $address_pieces[1];
314 * End patch from Bill Thousand
318 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
319 if (!$smtpConnection) {
320 echo "Error connecting to SMTP Server.<br>";
321 echo "$errorNumber : $errorString<br>";
324 $tmp = fgets($smtpConnection, 1024);
325 errorCheck($tmp, $smtpConnection);
327 $to_list = getLineOfAddrs($to);
328 $cc_list = getLineOfAddrs($cc);
330 /** Lets introduce ourselves */
331 fputs($smtpConnection, "HELO $domain\r\n");
332 $tmp = fgets($smtpConnection, 1024);
333 errorCheck($tmp, $smtpConnection);
335 /** Ok, who is sending the message? */
336 fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n");
337 $tmp = fgets($smtpConnection, 1024);
338 errorCheck($tmp, $smtpConnection);
340 /** send who the recipients are */
341 for ($i = 0; $i < count($to); $i++
) {
342 fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n");
343 $tmp = fgets($smtpConnection, 1024);
344 errorCheck($tmp, $smtpConnection);
346 for ($i = 0; $i < count($cc); $i++
) {
347 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n");
348 $tmp = fgets($smtpConnection, 1024);
349 errorCheck($tmp, $smtpConnection);
351 for ($i = 0; $i < count($bcc); $i++
) {
352 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n");
353 $tmp = fgets($smtpConnection, 1024);
354 errorCheck($tmp, $smtpConnection);
357 /** Lets start sending the actual message */
358 fputs($smtpConnection, "DATA\r\n");
359 $tmp = fgets($smtpConnection, 1024);
360 errorCheck($tmp, $smtpConnection);
363 $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject, $more_headers);
364 $bodylength = writeBody($smtpConnection, $body);
366 fputs($smtpConnection, ".\r\n"); // end the DATA part
367 $tmp = fgets($smtpConnection, 1024);
368 $num = errorCheck($tmp, $smtpConnection);
370 $tmp = nl2br(htmlspecialchars($tmp));
371 echo "ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
374 fputs($smtpConnection, "QUIT\r\n"); // log off
376 fclose($smtpConnection);
378 return ($headerlength +
$bodylength);
382 function errorCheck($line, $smtpConnection) {
383 global $page_header_php;
385 if (!isset($page_header_php)) {
386 include "../functions/page_header.php";
389 // Read new lines on a multiline response
391 while(ereg("^[0-9]+-", $line)) {
392 $line = fgets($smtpConnection, 1024);
399 $err_num = substr($line, 0, strpos($line, " "));
401 case 500: $message = "Syntax error; command not recognized";
404 case 501: $message = "Syntax error in parameters or arguments";
407 case 502: $message = "Command not implemented";
410 case 503: $message = "Bad sequence of commands";
413 case 504: $message = "Command parameter not implemented";
418 case 211: $message = "System status, or system help reply";
421 case 214: $message = "Help message";
426 case 220: $message = "Service ready";
429 case 221: $message = "Service closing transmission channel";
432 case 421: $message = "Service not available, closing chanel";
437 case 250: $message = "Requested mail action okay, completed";
440 case 251: $message = "User not local; will forward";
443 case 450: $message = "Requested mail action not taken: mailbox unavailable";
446 case 550: $message = "Requested action not taken: mailbox unavailable";
449 case 451: $message = "Requested action aborted: error in processing";
452 case 551: $message = "User not local; please try forwarding";
455 case 452: $message = "Requested action not taken: insufficient system storage";
458 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
461 case 553: $message = "Requested action not taken: mailbox name not allowed";
464 case 354: $message = "Start mail input; end with .";
467 case 554: $message = "Transaction failed";
470 default: $message = "Unknown response: ". nl2br(htmlspecialchars($lines));
477 displayPageHeader($color, "None");
479 echo "<br><b><font color=\"$color[1]\">ERROR</font></b><br><br>";
480 echo " <B>Error Number: </B>$err_num<BR>";
481 echo " <B>Reason: </B>$message<BR>";
482 $lines = nl2br(htmlspecialchars($lines));
483 echo "<B>Server Response: </B>$lines<BR>";
484 echo "<BR>MAIL NOT SENT";
485 echo "</TT></BODY></HTML>";
491 function sendMessage($t, $c, $b, $subject, $body, $reply_id) {
492 global $useSendmail, $msg_id, $is_reply, $mailbox;
493 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
494 $more_headers = Array();
496 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
499 sqimap_mailbox_select ($imap_stream, $mailbox);
500 sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, "Answered");
502 // Insert In-Reply-To and References headers if the
503 // message-id of the message we reply to is set (longer than "<>")
504 // The References header should really be the old Referenced header
505 // with the message ID appended, but it can be only the message ID too.
506 $hdr = sqimap_get_small_header ($imap_stream, $reply_id, false);
507 if(strlen($hdr->message_id
) > 2) {
508 $more_headers["In-Reply-To"] = $hdr->message_id
;
509 $more_headers["References"] = $hdr->message_id
;
511 sqimap_mailbox_close($imap_stream);
514 if ($useSendmail==true) {
515 $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers);
517 $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers);
520 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
521 sqimap_append ($imap_stream, $sent_folder, $length);
522 write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers);
523 writeBody ($imap_stream, $body);
524 sqimap_append_done ($imap_stream);
526 sqimap_logout($imap_stream);
527 // Delete the files uploaded for attaching (if any).