4 ** This contains all the functions needed to send messages through
5 ** an smtp server or sendmail.
9 // Returns true only if this message is multipart
10 function isMultipart () {
13 if (count($attachments)>0)
19 // Attach the files that are due to be attached
20 function attachFiles ($fp) {
21 global $attachments, $attachment_dir;
23 while (list($localname, $remotename) = each($attachments)) {
24 // This is to make sure noone is giving a filename in another
26 $localname = ereg_replace ("\\/", "", $localname);
28 $fileinfo = fopen ($attachment_dir.$localname.".info", "r");
29 $filetype = fgets ($fileinfo, 8192);
31 $filetype = trim ($filetype);
33 $filetype = "application/octet-stream";
35 fputs ($fp, "--".mimeBoundary()."\n");
36 fputs ($fp, "Content-Type: $filetype\n");
37 fputs ($fp, "Content-Disposition: attachment; filename=\"$remotename\"\n");
38 fputs ($fp, "Content-Transfer-Encoding: base64\n\n");
40 $file = fopen ($attachment_dir.$localname, "r");
41 while ($tmp = fread($file, 57))
42 fputs ($fp, chunk_split(base64_encode($tmp)));
45 unlink ($attachment_dir.$localname);
46 unlink ($attachment_dir.$localname.".info");
50 // Return a nice MIME-boundary
51 function mimeBoundary () {
52 global $mimeBoundaryString, $version, $REMOTE_ADDR, $SERVER_NAME,
55 if ($mimeBoundaryString == "") {
56 $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
58 $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
61 return $mimeBoundaryString;
64 /* Time offset for correct timezone */
65 function timezone () {
66 $diff_second = date("Z");
72 $diff_second = abs($diff_second);
74 $diff_hour = floor ($diff_second / 3600);
75 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
77 $zonename = "(".strftime("%Z").")";
78 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
82 /* Print all the needed RFC822 headers */
83 function write822Header ($fp, $t, $c, $b, $subject) {
84 global $REMOTE_ADDR, $SERVER_NAME;
85 global $data_dir, $username, $domain, $version, $useSendmail;
89 $bcc = parseAddrs($b);
90 $from_addr = "$username@$domain";
91 $reply_to = getPref($data_dir, $username, "reply_to");
92 $from = getPref($data_dir, $username, "full_name");
94 $to_list = getLineOfAddrs($to);
95 $cc_list = getLineOfAddrs($cc);
96 $bcc_list = getLineOfAddrs($bcc);
99 $from = "<$from_addr>";
101 $from = $from . " <$from_addr>";
103 /* This creates an RFC 822 date showing GMT */
104 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
106 /* Make an RFC822 Received: line */
107 fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
108 fputs ($fp, "$date\n");
110 /* The rest of the header */
111 fputs ($fp, "Date: $date\n");
112 fputs ($fp, "Subject: $subject\n"); // Subject
113 fputs ($fp, "From: $from\n"); // Subject
114 fputs ($fp, "To: $to_list\n"); // Who it's TO
117 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
121 fputs($fp, "Reply-To: $reply_to\n");
125 // BCCs is removed from header by sendmail
126 fputs($fp, "Bcc: $bcc_list\n");
130 fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
133 fputs($fp, "MIME-Version: 1.0\n");
136 fputs ($fp, "Content-Type: multipart/mixed; boundary=\"");
137 fputs ($fp, mimeBoundary());
140 fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
141 fputs($fp, "Content-Transfer-Encoding: 8bit\n");
146 function writeBody ($fp, $body) {
148 fputs ($fp, "--".mimeBoundary()."\n");
149 fputs ($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
150 fputs ($fp, "Content-Transfer-Encoding: 8bit\n\n");
151 fputs ($fp, stripslashes($body) . "\n");
153 fputs ($fp, "\n--".mimeBoundary()."--\n");
155 fputs ($fp, stripslashes($body) . "\n");
159 // Send mail using the sendmail command
160 function sendSendmail($t, $c, $b, $subject, $body) {
161 global $sendmail_path, $username, $domain;
163 // open pipe to sendmail
164 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
166 write822Header ($fp, $t, $c, $b, $subject);
167 writeBody($fp, $body);
172 function smtpReadData($smtpConnection) {
173 $read = fgets($smtpConnection, 1024);
177 $data[$counter] = $read;
178 $read = fgets($smtpConnection, 1024);
183 function sendSMTP($t, $c, $b, $subject, $body) {
184 global $username, $domain, $version, $smtpServerAddress, $smtpPort;
186 $to = parseAddrs($t);
187 $cc = parseAddrs($c);
188 $bcc = parseAddrs($b);
189 $from_addr = "$username@$domain";
191 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
192 if (!$smtpConnection) {
193 echo "Error connecting to SMTP Server.<br>";
194 echo "$errorNumber : $errorString<br>";
197 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
200 $to_list = getLineOfAddrs($to);
201 $cc_list = getLineOfAddrs($cc);
203 /** Lets introduce ourselves */
204 fputs($smtpConnection, "HELO $domain\n");
205 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
208 /** Ok, who is sending the message? */
209 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
210 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
213 /** send who the recipients are */
214 for ($i = 0; $i < count($to); $i++
) {
215 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
216 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
219 for ($i = 0; $i < count($cc); $i++
) {
220 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
221 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
224 for ($i = 0; $i < count($bcc); $i++
) {
225 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
226 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
230 /** Lets start sending the actual message */
231 fputs($smtpConnection, "DATA\n");
232 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
235 write822Header ($smtpConnection, $t, $c, $b, $subject);
237 writeBody($smtpConnection, $body); // send the body of the message
239 fputs($smtpConnection, ".\n"); // end the DATA part
240 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
241 $num = errorCheck($tmp);
243 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
246 fputs($smtpConnection, "QUIT\n"); // log off
248 fclose($smtpConnection);
252 function errorCheck($line) {
256 $err_num = substr($line, 0, strpos($line, " "));
258 case 500: $message = "Syntax error; command not recognized";
261 case 501: $message = "Syntax error in parameters or arguments";
264 case 502: $message = "Command not implemented";
267 case 503: $message = "Bad sequence of commands";
270 case 504: $message = "Command parameter not implemented";
275 case 211: $message = "System status, or system help reply";
278 case 214: $message = "Help message";
283 case 220: $message = "Service ready";
286 case 221: $message = "Service closing transmission channel";
289 case 421: $message = "Service not available, closing chanel";
294 case 250: $message = "Requested mail action okay, completed";
297 case 251: $message = "User not local; will forward";
300 case 450: $message = "Requested mail action not taken: mailbox unavailable";
303 case 550: $message = "Requested action not taken: mailbox unavailable";
306 case 451: $message = "Requested action aborted: error in processing";
309 case 551: $message = "User not local; please try forwarding";
312 case 452: $message = "Requested action not taken: insufficient system storage";
315 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
318 case 553: $message = "Requested action not taken: mailbox name not allowed";
321 case 354: $message = "Start mail input; end with .";
324 case 554: $message = "Transaction failed";
327 default: $message = "Unknown response: $line";
334 echo "<HTML><BODY BGCOLOR=FFFFFF>";
336 echo "<BR><B>ERROR</B><BR><BR>";
337 echo " <B>Error Number: </B>$err_num<BR>";
338 echo " <B>Reason: </B>$message<BR>";
339 echo "<B>Server Response: </B>$line<BR>";
340 echo "<BR>MAIL NOT SENT";
341 echo "</TT></BODY></HTML>";
347 function sendMessage($t, $c, $b, $subject, $body) {
350 if ($useSendmail==true) {
351 sendSendmail($t, $c, $b, $subject, $body);
353 sendSMTP($t, $c, $b, $subject, $body);