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\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) {
110 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
111 global $data_dir, $username, $domain, $version, $useSendmail;
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);
133 $from = "<$from_addr>";
135 $from = $from . " <$from_addr>";
137 /* This creates an RFC 822 date */
138 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
140 /* Create a message-id */
141 $message_id = "<" . $REMOTE_PORT . "." . $REMOTE_ADDR . ".";
142 $message_id .= time() . "@" . $SERVER_NAME .">";
144 /* Make an RFC822 Received: line */
145 $header = "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ";
146 $header .= "$date\n";
148 /* Insert the rest of the header fields */
149 $header .= "Message-ID: $message_id\r\n";
150 $header .= "Date: $date\r\n";
151 $header .= "Subject: $subject\r\n";
152 $header .= "From: $from\r\n";
153 $header .= "To: $to_list \r\n"; // Who it's TO
156 $header .= "Cc: $cc_list\r\n"; // Who the CCs are
160 $header .= "Reply-To: $reply_to\r\n";
164 // BCCs is removed from header by sendmail
165 $header .= "Bcc: $bcc_list\r\n";
169 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
172 $header .= "MIME-Version: 1.0\n";
175 $header .= "Content-Type: multipart/mixed; boundary=\"";
176 $header .= mimeBoundary();
179 $header .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
180 $header .= "Content-Transfer-Encoding: 8bit\r\n";
182 $header .= "\r\n"; // One blank line to separate header and body
184 $headerlength = strlen($header);
188 fputs ($fp, $header);
190 return $headerlength;
194 function writeBody ($fp, $passedBody) {
195 $attachmentlength = 0;
198 $body = "--".mimeBoundary()."\r\n";
199 $body .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
200 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
201 $body .= stripslashes($passedBody) . "\r\n";
204 $attachmentlenght = attachFiles($fp);
206 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
207 fputs ($fp, $postbody);
209 $body = stripslashes($passedBody) . "\r\n";
212 fputs ($fp, $postbody);
215 return (strlen($body) +
strlen($postbody) +
$attachmentlength);
218 // Send mail using the sendmail command
219 function sendSendmail($t, $c, $b, $subject, $body) {
220 global $sendmail_path, $username, $domain;
222 // open pipe to sendmail
223 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
225 $headerlength = write822Header ($fp, $t, $c, $b, $subject);
226 $bodylength = writeBody($fp, $body);
230 return ($headerlength +
$bodylenght);
233 function smtpReadData($smtpConnection) {
234 $read = fgets($smtpConnection, 1024);
238 $data[$counter] = $read;
239 $read = fgets($smtpConnection, 1024);
244 function sendSMTP($t, $c, $b, $subject, $body) {
245 global $username, $domain, $version, $smtpServerAddress, $smtpPort,
248 $to = parseAddrs($t);
249 $cc = parseAddrs($c);
250 $bcc = parseAddrs($b);
251 $from_addr = getPref($data_dir, $username, "email_address");
253 if ($from_addr == "")
254 $from_addr = "$username@$domain";
256 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
257 if (!$smtpConnection) {
258 echo "Error connecting to SMTP Server.<br>";
259 echo "$errorNumber : $errorString<br>";
262 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
265 $to_list = getLineOfAddrs($to);
266 $cc_list = getLineOfAddrs($cc);
268 /** Lets introduce ourselves */
269 fputs($smtpConnection, "HELO $domain\r\n");
270 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
273 /** Ok, who is sending the message? */
274 fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n");
275 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
278 /** send who the recipients are */
279 for ($i = 0; $i < count($to); $i++
) {
280 fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n");
281 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
284 for ($i = 0; $i < count($cc); $i++
) {
285 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n");
286 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
289 for ($i = 0; $i < count($bcc); $i++
) {
290 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n");
291 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
295 /** Lets start sending the actual message */
296 fputs($smtpConnection, "DATA\r\n");
297 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
301 $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject);
302 $bodylength = writeBody($smtpConnection, $body);
304 fputs($smtpConnection, ".\r\n"); // end the DATA part
305 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
306 $num = errorCheck($tmp);
308 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
311 fputs($smtpConnection, "QUIT\r\n"); // log off
313 fclose($smtpConnection);
315 return ($headerlength +
$bodylength);
319 function errorCheck($line) {
323 $err_num = substr($line, 0, strpos($line, " "));
325 case 500: $message = "Syntax error; command not recognized";
328 case 501: $message = "Syntax error in parameters or arguments";
331 case 502: $message = "Command not implemented";
334 case 503: $message = "Bad sequence of commands";
337 case 504: $message = "Command parameter not implemented";
342 case 211: $message = "System status, or system help reply";
345 case 214: $message = "Help message";
350 case 220: $message = "Service ready";
353 case 221: $message = "Service closing transmission channel";
356 case 421: $message = "Service not available, closing chanel";
361 case 250: $message = "Requested mail action okay, completed";
364 case 251: $message = "User not local; will forward";
367 case 450: $message = "Requested mail action not taken: mailbox unavailable";
370 case 550: $message = "Requested action not taken: mailbox unavailable";
373 case 451: $message = "Requested action aborted: error in processing";
376 case 551: $message = "User not local; please try forwarding";
379 case 452: $message = "Requested action not taken: insufficient system storage";
382 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
385 case 553: $message = "Requested action not taken: mailbox name not allowed";
388 case 354: $message = "Start mail input; end with .";
391 case 554: $message = "Transaction failed";
394 default: $message = "Unknown response: $line";
401 echo "<HTML><BODY BGCOLOR=FFFFFF>";
403 echo "<BR><B>ERROR</B><BR><BR>";
404 echo " <B>Error Number: </B>$err_num<BR>";
405 echo " <B>Reason: </B>$message<BR>";
406 echo "<B>Server Response: </B>$line<BR>";
407 echo "<BR>MAIL NOT SENT";
408 echo "</TT></BODY></HTML>";
414 function sendMessage($t, $c, $b, $subject, $body) {
416 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress;
418 if ($useSendmail==true) {
419 $length = sendSendmail($t, $c, $b, $subject, $body);
421 $length = sendSMTP($t, $c, $b, $subject, $body);
425 // This is a proposed interface to save messages in the sent folder
428 // $imap_stream = sqimap_login($username, $key, $imapServerAddress, 1);
429 // sqimap_append ($imap_stream, $sent_folder, $length);
430 // write822Header ($imap_stream, .....);
431 // writeBody ($imap_stream, ....);
432 // sqimap_append_done($imap_stream);
434 // Or something like that...
436 // Delete the files uploaded for attaching (if any).