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;
24 while (list($localname, $remotename) = each($attachments)) {
25 // This is to make sure noone is giving a filename in another
27 $localname = ereg_replace ("\\/", "", $localname);
29 $fileinfo = fopen ($attachment_dir.$localname.".info", "r");
30 $filetype = fgets ($fileinfo, 8192);
32 $filetype = trim ($filetype);
34 $filetype = "application/octet-stream";
36 fputs ($fp, "--".mimeBoundary()."\n");
37 fputs ($fp, "Content-Type: $filetype\n");
38 fputs ($fp, "Content-Disposition: attachment; filename=\"$remotename\"\n");
39 fputs ($fp, "Content-Transfer-Encoding: base64\n\n");
41 $file = fopen ($attachment_dir.$localname, "r");
42 while ($tmp = fread($file, 57))
43 fputs ($fp, chunk_split(base64_encode($tmp)));
46 unlink ($attachment_dir.$localname);
47 unlink ($attachment_dir.$localname.".info");
51 // Return a nice MIME-boundary
52 function mimeBoundary () {
53 global $mimeBoundaryString, $version, $REMOTE_ADDR, $SERVER_NAME,
56 if ($mimeBoundaryString == "") {
57 $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
59 $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
62 return $mimeBoundaryString;
65 /* Time offset for correct timezone */
66 function timezone () {
67 $diff_second = date("Z");
73 $diff_second = abs($diff_second);
75 $diff_hour = floor ($diff_second / 3600);
76 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
78 $zonename = "(".strftime("%Z").")";
79 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
83 /* Print all the needed RFC822 headers */
84 function write822Header ($fp, $t, $c, $b, $subject) {
85 global $REMOTE_ADDR, $SERVER_NAME;
86 global $data_dir, $username, $domain, $version, $useSendmail;
90 $bcc = parseAddrs($b);
91 $reply_to = getPref($data_dir, $username, "reply_to");
92 $from = getPref($data_dir, $username, "full_name");
93 $from_addr = getPref($data_dir, $username, "email_address");
96 $from_addr = "$username@$domain";
98 $to_list = getLineOfAddrs($to);
99 $cc_list = getLineOfAddrs($cc);
100 $bcc_list = getLineOfAddrs($bcc);
103 $from = "<$from_addr>";
105 $from = $from . " <$from_addr>";
107 /* This creates an RFC 822 date showing GMT */
108 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
110 /* Make an RFC822 Received: line */
111 fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
112 fputs ($fp, "$date\n");
114 /* The rest of the header */
115 fputs ($fp, "Date: $date\n");
116 fputs ($fp, "Subject: $subject\n"); // Subject
117 fputs ($fp, "From: $from\n"); // Subject
118 fputs ($fp, "To: $to_list\n"); // Who it's TO
121 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
125 fputs($fp, "Reply-To: $reply_to\n");
129 // BCCs is removed from header by sendmail
130 fputs($fp, "Bcc: $bcc_list\n");
134 fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
137 fputs($fp, "MIME-Version: 1.0\n");
140 fputs ($fp, "Content-Type: multipart/mixed; boundary=\"");
141 fputs ($fp, mimeBoundary());
144 fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
145 fputs($fp, "Content-Transfer-Encoding: 8bit\n");
150 function writeBody ($fp, $body) {
152 fputs ($fp, "--".mimeBoundary()."\n");
153 fputs ($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
154 fputs ($fp, "Content-Transfer-Encoding: 8bit\n\n");
155 fputs ($fp, stripslashes($body) . "\n");
157 fputs ($fp, "\n--".mimeBoundary()."--\n");
159 fputs ($fp, stripslashes($body) . "\n");
163 // Send mail using the sendmail command
164 function sendSendmail($t, $c, $b, $subject, $body) {
165 global $sendmail_path, $username, $domain;
167 // open pipe to sendmail
168 $fp = popen (escapeshellcmd("$sendmail_path -odb -oi -t -f$username@$domain"), "w");
170 write822Header ($fp, $t, $c, $b, $subject);
171 writeBody($fp, $body);
176 function smtpReadData($smtpConnection) {
177 $read = fgets($smtpConnection, 1024);
181 $data[$counter] = $read;
182 $read = fgets($smtpConnection, 1024);
187 function sendSMTP($t, $c, $b, $subject, $body) {
188 global $username, $domain, $version, $smtpServerAddress, $smtpPort,
191 $to = parseAddrs($t);
192 $cc = parseAddrs($c);
193 $bcc = parseAddrs($b);
194 $from_addr = getPref($data_dir, $username, "email_address");
196 if ($from_addr == "")
197 $from_addr = "$username@$domain";
199 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
200 if (!$smtpConnection) {
201 echo "Error connecting to SMTP Server.<br>";
202 echo "$errorNumber : $errorString<br>";
205 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
208 $to_list = getLineOfAddrs($to);
209 $cc_list = getLineOfAddrs($cc);
211 /** Lets introduce ourselves */
212 fputs($smtpConnection, "HELO $domain\n");
213 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
216 /** Ok, who is sending the message? */
217 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
218 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
221 /** send who the recipients are */
222 for ($i = 0; $i < count($to); $i++
) {
223 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
224 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
227 for ($i = 0; $i < count($cc); $i++
) {
228 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
229 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
232 for ($i = 0; $i < count($bcc); $i++
) {
233 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
234 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
238 /** Lets start sending the actual message */
239 fputs($smtpConnection, "DATA\n");
240 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
243 write822Header ($smtpConnection, $t, $c, $b, $subject);
245 writeBody($smtpConnection, $body); // send the body of the message
247 fputs($smtpConnection, ".\n"); // end the DATA part
248 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
249 $num = errorCheck($tmp);
251 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
254 fputs($smtpConnection, "QUIT\n"); // log off
256 fclose($smtpConnection);
260 function errorCheck($line) {
264 $err_num = substr($line, 0, strpos($line, " "));
266 case 500: $message = "Syntax error; command not recognized";
269 case 501: $message = "Syntax error in parameters or arguments";
272 case 502: $message = "Command not implemented";
275 case 503: $message = "Bad sequence of commands";
278 case 504: $message = "Command parameter not implemented";
283 case 211: $message = "System status, or system help reply";
286 case 214: $message = "Help message";
291 case 220: $message = "Service ready";
294 case 221: $message = "Service closing transmission channel";
297 case 421: $message = "Service not available, closing chanel";
302 case 250: $message = "Requested mail action okay, completed";
305 case 251: $message = "User not local; will forward";
308 case 450: $message = "Requested mail action not taken: mailbox unavailable";
311 case 550: $message = "Requested action not taken: mailbox unavailable";
314 case 451: $message = "Requested action aborted: error in processing";
317 case 551: $message = "User not local; please try forwarding";
320 case 452: $message = "Requested action not taken: insufficient system storage";
323 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
326 case 553: $message = "Requested action not taken: mailbox name not allowed";
329 case 354: $message = "Start mail input; end with .";
332 case 554: $message = "Transaction failed";
335 default: $message = "Unknown response: $line";
342 echo "<HTML><BODY BGCOLOR=FFFFFF>";
344 echo "<BR><B>ERROR</B><BR><BR>";
345 echo " <B>Error Number: </B>$err_num<BR>";
346 echo " <B>Reason: </B>$message<BR>";
347 echo "<B>Server Response: </B>$line<BR>";
348 echo "<BR>MAIL NOT SENT";
349 echo "</TT></BODY></HTML>";
355 function sendMessage($t, $c, $b, $subject, $body) {
358 if ($useSendmail==true) {
359 sendSendmail($t, $c, $b, $subject, $body);
361 sendSMTP($t, $c, $b, $subject, $body);