88bfa9ee1225df3cc5da639b2584a66ddd5efdf3
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 static $mimeBoundaryString;
79 if ($mimeBoundaryString == "") {
80 $mimeBoundaryString = GenerateRandomString(70, '\'()+,-./:=?_', 7);
83 return $mimeBoundaryString;
86 /* Time offset for correct timezone */
87 function timezone () {
88 $diff_second = date("Z");
90 $diff_second = - $diff_second;
96 $diff_second = abs($diff_second);
98 $diff_hour = floor ($diff_second / 3600);
99 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
101 $zonename = "(".strftime("%Z").")";
102 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
106 /* Print all the needed RFC822 headers */
107 function write822Header ($fp, $t, $c, $b, $subject, $more_headers) {
108 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
109 global $data_dir, $username, $domain, $version, $useSendmail;
110 global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
113 // Storing the header to make sure the header is the same
114 // everytime the header is printed.
115 static $header, $headerlength;
118 $to = parseAddrs($t);
119 $cc = parseAddrs($c);
120 $bcc = parseAddrs($b);
121 $reply_to = getPref($data_dir, $username, "reply_to");
122 $from = getPref($data_dir, $username, "full_name");
123 $from_addr = getPref($data_dir, $username, "email_address");
125 if ($from_addr == "")
126 $from_addr = "$username@$domain";
128 $to_list = getLineOfAddrs($to);
129 $cc_list = getLineOfAddrs($cc);
130 $bcc_list = getLineOfAddrs($bcc);
132 /* Encoding 8-bit characters and making from line */
133 $subject = sqStripSlashes(encodeHeader($subject));
135 $from = "<$from_addr>";
137 $from = "\"" . encodeHeader($from) . "\" <$from_addr>";
139 /* This creates an RFC 822 date */
140 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
142 /* Create a message-id */
143 $message_id = "<" . $REMOTE_PORT . "." . $REMOTE_ADDR . ".";
144 $message_id .= time() . ".squirrel@" . $SERVER_NAME .">";
146 /* Make an RFC822 Received: line */
147 if (isset($REMOTE_HOST))
148 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
150 $received_from = $REMOTE_ADDR;
152 if (isset($HTTP_VIA) ||
isset ($HTTP_X_FORWARDED_FOR)) {
153 if ($HTTP_X_FORWARDED_FOR == "")
154 $HTTP_X_FORWARDED_FOR = "unknown";
155 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
158 $header = "Received: from $received_from\r\n";
159 $header .= " (SquirrelMail authenticated user $username)\r\n";
160 $header .= " by $SERVER_NAME with HTTP;\r\n";
161 $header .= " $date\r\n";
163 /* Insert the rest of the header fields */
164 $header .= "Message-ID: $message_id\r\n";
165 $header .= "Date: $date\r\n";
166 $header .= "Subject: $subject\r\n";
167 $header .= "From: $from\r\n";
168 $header .= "To: $to_list \r\n"; // Who it's TO
170 /* Insert headers from the $more_headers array */
171 if(is_array($more_headers)) {
172 reset($more_headers);
173 while(list($h_name, $h_val) = each($more_headers)) {
174 $header .= sprintf("%s: %s\r\n", $h_name, $h_val);
179 $header .= "Cc: $cc_list\r\n"; // Who the CCs are
183 $header .= "Reply-To: $reply_to\r\n";
187 // BCCs is removed from header by sendmail
188 $header .= "Bcc: $bcc_list\r\n";
192 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
195 $header .= "MIME-Version: 1.0\r\n";
198 $header .= "Content-Type: multipart/mixed; boundary=\"";
199 $header .= mimeBoundary();
202 if ($default_charset != "")
203 $header .= "Content-Type: text/plain; charset=$default_charset\r\n";
205 $header .= "Content-Type: text/plain;\r\n";
206 $header .= "Content-Transfer-Encoding: 8bit\r\n";
208 $header .= "\r\n"; // One blank line to separate header and body
210 $headerlength = strlen($header);
214 fputs ($fp, $header);
216 return $headerlength;
220 function writeBody ($fp, $passedBody) {
221 global $default_charset;
223 $attachmentlength = 0;
226 $body = "--".mimeBoundary()."\r\n";
228 if ($default_charset != "")
229 $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
231 $body .= "Content-Type: text/plain\r\n";
233 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
234 $body .= sqStripSlashes($passedBody) . "\r\n";
237 $attachmentlength = attachFiles($fp);
239 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
240 fputs ($fp, $postbody);
242 $body = sqStripSlashes($passedBody) . "\r\n";
245 fputs ($fp, $postbody);
248 return (strlen($body) +
strlen($postbody) +
$attachmentlength);
251 // Send mail using the sendmail command
252 function sendSendmail($t, $c, $b, $subject, $body, $more_headers) {
253 global $sendmail_path, $username, $domain;
255 // Build envelope sender address. Make sure it doesn't contain
256 // spaces or other "weird" chars that would allow a user to
257 // exploit the shell/pipe it is used in.
258 $envelopefrom = "$username@$domain";
259 $envelopefrom = ereg_replace("[[:blank:]]","", $envelopefrom);
260 $envelopefrom = ereg_replace("[[:space:]]","", $envelopefrom);
261 $envelopefrom = ereg_replace("[[:cntrl:]]","", $envelopefrom);
263 // open pipe to sendmail
264 $fp = popen (escapeshellcmd("$sendmail_path -t -f$envelopefrom"), "w");
266 $headerlength = write822Header ($fp, $t, $c, $b, $subject, $more_headers);
267 $bodylength = writeBody($fp, $body);
271 return ($headerlength +
$bodylength);
274 function smtpReadData($smtpConnection) {
275 $read = fgets($smtpConnection, 1024);
279 $data[$counter] = $read;
280 $read = fgets($smtpConnection, 1024);
285 function sendSMTP($t, $c, $b, $subject, $body, $more_headers) {
286 global $username, $domain, $version, $smtpServerAddress, $smtpPort,
289 $to = parseAddrs($t);
290 $cc = parseAddrs($c);
291 $bcc = parseAddrs($b);
292 $from_addr = getPref($data_dir, $username, "email_address");
296 * A patch from Bill Thousand <billyt@claritytech.com>
298 * "I don't know if anyone else needs this or not, but it totally makes squirrelmail usable for us.
299 * This quick patch checks the username and from address for the domain information. We use
300 * a virtual domain patch for our imap server that allows multiple domains by using username@domain.com
301 * as the login username."
303 if ($from_addr == "") {
304 if (strstr($username, "@")) {
305 $from_addr = $username;
306 $address_pieces = explode("@",$username);
307 $domain = $address_pieces[1];
309 $from_addr = "$username@$domain";
312 // If the From Address is specified, use the domain in the from
313 // address if it's there.
314 if (strstr($from_addr, "@")) {
315 $address_pieces = explode("@", $from_addr);
316 $domain = $address_pieces[1];
320 * End patch from Bill Thousand
324 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
325 if (!$smtpConnection) {
326 echo "Error connecting to SMTP Server.<br>";
327 echo "$errorNumber : $errorString<br>";
330 $tmp = fgets($smtpConnection, 1024);
331 errorCheck($tmp, $smtpConnection);
333 $to_list = getLineOfAddrs($to);
334 $cc_list = getLineOfAddrs($cc);
336 /** Lets introduce ourselves */
337 fputs($smtpConnection, "HELO $domain\r\n");
338 $tmp = fgets($smtpConnection, 1024);
339 errorCheck($tmp, $smtpConnection);
341 /** Ok, who is sending the message? */
342 fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n");
343 $tmp = fgets($smtpConnection, 1024);
344 errorCheck($tmp, $smtpConnection);
346 /** send who the recipients are */
347 for ($i = 0; $i < count($to); $i++
) {
348 fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n");
349 $tmp = fgets($smtpConnection, 1024);
350 errorCheck($tmp, $smtpConnection);
352 for ($i = 0; $i < count($cc); $i++
) {
353 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n");
354 $tmp = fgets($smtpConnection, 1024);
355 errorCheck($tmp, $smtpConnection);
357 for ($i = 0; $i < count($bcc); $i++
) {
358 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n");
359 $tmp = fgets($smtpConnection, 1024);
360 errorCheck($tmp, $smtpConnection);
363 /** Lets start sending the actual message */
364 fputs($smtpConnection, "DATA\r\n");
365 $tmp = fgets($smtpConnection, 1024);
366 errorCheck($tmp, $smtpConnection);
369 $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject, $more_headers);
370 $bodylength = writeBody($smtpConnection, $body);
372 fputs($smtpConnection, ".\r\n"); // end the DATA part
373 $tmp = fgets($smtpConnection, 1024);
374 $num = errorCheck($tmp, $smtpConnection);
376 $tmp = nl2br(htmlspecialchars($tmp));
377 echo "ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
380 fputs($smtpConnection, "QUIT\r\n"); // log off
382 fclose($smtpConnection);
384 return ($headerlength +
$bodylength);
388 function errorCheck($line, $smtpConnection) {
389 global $page_header_php;
391 if (!isset($page_header_php)) {
392 include "../functions/page_header.php";
395 // Read new lines on a multiline response
397 while(ereg("^[0-9]+-", $line)) {
398 $line = fgets($smtpConnection, 1024);
405 $err_num = substr($line, 0, strpos($line, " "));
407 case 500: $message = "Syntax error; command not recognized";
410 case 501: $message = "Syntax error in parameters or arguments";
413 case 502: $message = "Command not implemented";
416 case 503: $message = "Bad sequence of commands";
419 case 504: $message = "Command parameter not implemented";
424 case 211: $message = "System status, or system help reply";
427 case 214: $message = "Help message";
432 case 220: $message = "Service ready";
435 case 221: $message = "Service closing transmission channel";
438 case 421: $message = "Service not available, closing chanel";
443 case 250: $message = "Requested mail action okay, completed";
446 case 251: $message = "User not local; will forward";
449 case 450: $message = "Requested mail action not taken: mailbox unavailable";
452 case 550: $message = "Requested action not taken: mailbox unavailable";
455 case 451: $message = "Requested action aborted: error in processing";
458 case 551: $message = "User not local; please try forwarding";
461 case 452: $message = "Requested action not taken: insufficient system storage";
464 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
467 case 553: $message = "Requested action not taken: mailbox name not allowed";
470 case 354: $message = "Start mail input; end with .";
473 case 554: $message = "Transaction failed";
476 default: $message = "Unknown response: ". nl2br(htmlspecialchars($lines));
483 displayPageHeader($color, "None");
485 echo "<br><b><font color=\"$color[1]\">ERROR</font></b><br><br>";
486 echo " <B>Error Number: </B>$err_num<BR>";
487 echo " <B>Reason: </B>$message<BR>";
488 $lines = nl2br(htmlspecialchars($lines));
489 echo "<B>Server Response: </B>$lines<BR>";
490 echo "<BR>MAIL NOT SENT";
491 echo "</TT></BODY></HTML>";
497 function sendMessage($t, $c, $b, $subject, $body, $reply_id) {
498 global $useSendmail, $msg_id, $is_reply, $mailbox;
499 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
500 $more_headers = Array();
502 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
505 sqimap_mailbox_select ($imap_stream, $mailbox);
506 sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, "Answered");
508 // Insert In-Reply-To and References headers if the
509 // message-id of the message we reply to is set (longer than "<>")
510 // The References header should really be the old Referenced header
511 // with the message ID appended, but it can be only the message ID too.
512 $hdr = sqimap_get_small_header ($imap_stream, $reply_id, false);
513 if(strlen($hdr->message_id
) > 2) {
514 $more_headers["In-Reply-To"] = $hdr->message_id
;
515 $more_headers["References"] = $hdr->message_id
;
517 sqimap_mailbox_close($imap_stream);
520 if ($useSendmail==true) {
521 $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers);
523 $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers);
526 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
527 sqimap_append ($imap_stream, $sent_folder, $length);
528 write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers);
529 writeBody ($imap_stream, $body);
530 sqimap_append_done ($imap_stream);
532 sqimap_logout($imap_stream);
533 // Delete the files uploaded for attaching (if any).