4 ** This contains all the functions needed to send messages through
5 ** an smtp server or sendmail.
9 /* These next 2 functions are stub functions for implementations of
12 // Returns true only if this message is multipart
13 function isMultipart () {
16 if (count($attachments)>0)
22 // Attach the files that are due to be attached
23 function attachFiles ($fp) {
26 while (list($localname, $remotename) = each($attachments)) {
27 $fileinfo = fopen ($localname.".info", "r");
28 $filetype = fgets ($fileinfo, 8192);
30 $filetype = trim ($filetype);
32 $filetype = "application/octet-stream";
34 fputs ($fp, "--".mimeBoundary()."\n");
35 fputs ($fp, "Content-Type: $filetype\n");
36 fputs ($fp, "Content-Disposition: attachment; filename=\"$remotename\"\n");
37 fputs ($fp, "Content-Transfer-Encoding: base64\n\n");
39 $file = fopen ($localname, "r");
40 while ($tmp = fread($file, 57))
41 fputs ($fp, chunk_split(base64_encode($tmp)));
45 unlink ($localname.".info");
49 // Return a nice MIME-boundary
50 function mimeBoundary () {
51 global $mimeBoundaryString, $version, $REMOTE_ADDR, $SERVER_NAME,
54 if ($mimeBoundaryString == "") {
55 $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
57 $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
60 return $mimeBoundaryString;
63 /* Print all the needed RFC822 headers */
64 function write822Header ($fp, $t, $c, $b, $subject) {
65 global $REMOTE_ADDR, $SERVER_NAME;
66 global $data_dir, $username, $domain, $version, $useSendmail;
70 $bcc = parseAddrs($b);
71 $from_addr = "$username@$domain";
72 $reply_to = getPref($data_dir, $username, "reply_to");
73 $from = getPref($data_dir, $username, "full_name");
75 $to_list = getLineOfAddrs($to);
76 $cc_list = getLineOfAddrs($cc);
77 $bcc_list = getLineOfAddrs($bcc);
80 $from = "<$from_addr>";
82 $from = $from . " <$from_addr>";
84 /* This creates an RFC 822 date showing GMT */
85 $date = date("D, j M Y H:i:s +0000", gmmktime());
87 /* Make an RFC822 Received: line */
88 fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
89 fputs ($fp, "$date\n");
91 /* The rest of the header */
92 fputs ($fp, "Date: $date\n");
93 fputs ($fp, "Subject: $subject\n"); // Subject
94 fputs ($fp, "From: $from\n"); // Subject
95 fputs ($fp, "To: $to_list\n"); // Who it's TO
98 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
102 fputs($fp, "Reply-To: $reply_to\n");
106 // BCCs is removed from header by sendmail
107 fputs($fp, "Bcc: $bcc_list\n");
111 fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
114 fputs($fp, "MIME-Version: 1.0\n");
117 fputs ($fp, "Content-Type: multipart/mixed; boundary=\"");
118 fputs ($fp, mimeBoundary());
121 fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
122 fputs($fp, "Content-Transfer-Encoding: 8bit\n");
127 function writeBody ($fp, $body) {
129 fputs ($fp, "--".mimeBoundary()."\n");
130 fputs ($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
131 fputs ($fp, "Content-Transfer-Encoding: 8bit\n\n");
132 fputs ($fp, stripslashes($body) . "\n");
134 fputs ($fp, "\n--".mimeBoundary()."--\n");
136 fputs ($fp, stripslashes($body) . "\n");
140 // Send mail using the sendmail command
141 function sendSendmail($t, $c, $b, $subject, $body) {
142 global $sendmail_path, $username, $domain;
144 // open pipe to sendmail
145 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
147 write822Header ($fp, $t, $c, $b, $subject);
148 writeBody($fp, $body);
153 function smtpReadData($smtpConnection) {
154 $read = fgets($smtpConnection, 1024);
158 $data[$counter] = $read;
159 $read = fgets($smtpConnection, 1024);
164 function sendSMTP($t, $c, $b, $subject, $body) {
165 global $username, $domain, $version, $smtpServerAddress, $smtpPort;
167 $to = parseAddrs($t);
168 $cc = parseAddrs($c);
169 $bcc = parseAddrs($b);
170 $from_addr = "$username@$domain";
172 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
173 if (!$smtpConnection) {
174 echo "Error connecting to SMTP Server.<br>";
175 echo "$errorNumber : $errorString<br>";
178 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
181 $to_list = getLineOfAddrs($to);
182 $cc_list = getLineOfAddrs($cc);
184 /** Lets introduce ourselves */
185 fputs($smtpConnection, "HELO $domain\n");
186 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
189 /** Ok, who is sending the message? */
190 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
191 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
194 /** send who the recipients are */
195 for ($i = 0; $i < count($to); $i++
) {
196 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
197 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
200 for ($i = 0; $i < count($cc); $i++
) {
201 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
202 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
205 for ($i = 0; $i < count($bcc); $i++
) {
206 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
207 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
211 /** Lets start sending the actual message */
212 fputs($smtpConnection, "DATA\n");
213 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
216 write822Header ($smtpConnection, $t, $c, $b, $subject);
218 writeBody($smtpConnection, $body); // send the body of the message
220 fputs($smtpConnection, ".\n"); // end the DATA part
221 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
222 $num = errorCheck($tmp);
224 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
227 fputs($smtpConnection, "QUIT\n"); // log off
229 fclose($smtpConnection);
233 function errorCheck($line) {
237 $err_num = substr($line, 0, strpos($line, " "));
239 case 500: $message = "Syntax error; command not recognized";
242 case 501: $message = "Syntax error in parameters or arguments";
245 case 502: $message = "Command not implemented";
248 case 503: $message = "Bad sequence of commands";
251 case 504: $message = "Command parameter not implemented";
256 case 211: $message = "System status, or system help reply";
259 case 214: $message = "Help message";
264 case 220: $message = "Service ready";
267 case 221: $message = "Service closing transmission channel";
270 case 421: $message = "Service not available, closing chanel";
275 case 250: $message = "Requested mail action okay, completed";
278 case 251: $message = "User not local; will forward";
281 case 450: $message = "Requested mail action not taken: mailbox unavailable";
284 case 550: $message = "Requested action not taken: mailbox unavailable";
287 case 451: $message = "Requested action aborted: error in processing";
290 case 551: $message = "User not local; please try forwarding";
293 case 452: $message = "Requested action not taken: insufficient system storage";
296 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
299 case 553: $message = "Requested action not taken: mailbox name not allowed";
302 case 354: $message = "Start mail input; end with .";
305 case 554: $message = "Transaction failed";
308 default: $message = "Unknown response: $line";
315 echo "<HTML><BODY BGCOLOR=FFFFFF>";
317 echo "<BR><B>ERROR</B><BR><BR>";
318 echo " <B>Error Number: </B>$err_num<BR>";
319 echo " <B>Reason: </B>$message<BR>";
320 echo "<B>Server Response: </B>$line<BR>";
321 echo "<BR>MAIL NOT SENT";
322 echo "</TT></BODY></HTML>";
328 function sendMessage($t, $c, $b, $subject, $body) {
331 if ($useSendmail==true) {
332 sendSendmail($t, $c, $b, $subject, $body);
334 sendSMTP($t, $c, $b, $subject, $body);