259b698e2a9ae3d8adbb698fb456aab91e290266
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 () {
17 // Attach the files that are due to be attached
18 function attachFile ($fp) {
22 // Return a nice MIME-boundary
23 function mimeBoundary () {
24 global $mimeBoundaryString, $version, $REMOTE_ADDR, $SERVER_NAME,
27 if ($mimeBoundaryString == "") {
28 $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
30 $mimeBoundaryString = "=-=_=-SqMB.".substr(md5($temp),1,15);
33 return $mimeBoundaryString;
36 /* Print all the needed RFC822 headers */
37 function write822Header ($fp, $t, $c, $b, $subject) {
38 global $REMOTE_ADDR, $SERVER_NAME;
39 global $data_dir, $username, $domain, $version, $useSendmail;
43 $bcc = parseAddrs($b);
44 $from_addr = "$username@$domain";
45 $reply_to = getPref($data_dir, $username, "reply_to");
46 $from = getPref($data_dir, $username, "full_name");
48 $to_list = getLineOfAddrs($to);
49 $cc_list = getLineOfAddrs($cc);
50 $bcc_list = getLineOfAddrs($bcc);
53 $from = "<$from_addr>";
55 $from = $from . " <$from_addr>";
57 /* This creates an RFC 822 date showing GMT */
58 $date = date("D, j M Y H:i:s +0000", gmmktime());
60 /* Make an RFC822 Received: line */
61 fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
62 fputs ($fp, "$date\n");
64 /* The rest of the header */
65 fputs ($fp, "Date: $date\n");
66 fputs ($fp, "Subject: $subject\n"); // Subject
67 fputs ($fp, "From: $from\n"); // Subject
68 fputs ($fp, "To: $to_list\n"); // Who it's TO
71 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
75 fputs($fp, "Reply-To: $reply_to\n");
79 // BCCs is removed from header by sendmail
80 fputs($fp, "Bcc: $bcc_list\n");
84 fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
87 fputs($fp, "MIME-Version: 1.0\n");
90 fputs ($fp, "Content-Type: multipart/mixed; boundary=\"");
91 fputs ($fp, mimeBoundary());
94 fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
95 fputs($fp, "Content-Transfer-Encoding: 8bit\n");
100 function writeBody ($fp, $body) {
102 fputs ($fp, "--".mimeBoundary()."\n");
103 fputs ($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
104 fputs ($fp, "Content-Transfer-Encoding: 8bit\n\n");
105 fputs ($fp, "$body\n");
106 fputs ($fp, "\n--".mimeBoundary()."--\n");
108 fputs ($fp, "$body\n");
112 // Send mail using the sendmail command
113 function sendSendmail($t, $c, $b, $subject, $body) {
114 global $sendmail_path, $username, $domain;
116 // open pipe to sendmail
117 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
119 write822Header ($fp, $t, $c, $b, $subject);
120 writeBody($fp, $body);
125 function smtpReadData($smtpConnection) {
126 $read = fgets($smtpConnection, 1024);
130 $data[$counter] = $read;
131 $read = fgets($smtpConnection, 1024);
136 function sendSMTP($t, $c, $b, $subject, $body) {
137 global $username, $domain, $version, $smtpServerAddress, $smtpPort;
139 $to = parseAddrs($t);
140 $cc = parseAddrs($c);
141 $bcc = parseAddrs($b);
142 $from_addr = "$username@$domain";
144 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
145 if (!$smtpConnection) {
146 echo "Error connecting to SMTP Server.<br>";
147 echo "$errorNumber : $errorString<br>";
150 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
153 $to_list = getLineOfAddrs($to);
154 $cc_list = getLineOfAddrs($cc);
156 /** Lets introduce ourselves */
157 fputs($smtpConnection, "HELO $domain\n");
158 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
161 /** Ok, who is sending the message? */
162 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
163 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
166 /** send who the recipients are */
167 for ($i = 0; $i < count($to); $i++
) {
168 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
169 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
172 for ($i = 0; $i < count($cc); $i++
) {
173 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
174 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
177 for ($i = 0; $i < count($bcc); $i++
) {
178 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
179 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
183 /** Lets start sending the actual message */
184 fputs($smtpConnection, "DATA\n");
185 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
188 write822Header ($smtpConnection, $t, $c, $b, $subject);
190 writeBody($smtpConnection, $body); // send the body of the message
192 fputs($smtpConnection, ".\n"); // end the DATA part
193 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
194 $num = errorCheck($tmp);
196 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
199 fputs($smtpConnection, "QUIT\n"); // log off
201 fclose($smtpConnection);
205 function errorCheck($line) {
209 $err_num = substr($line, 0, strpos($line, " "));
211 case 500: $message = "Syntax error; command not recognized";
214 case 501: $message = "Syntax error in parameters or arguments";
217 case 502: $message = "Command not implemented";
220 case 503: $message = "Bad sequence of commands";
223 case 504: $message = "Command parameter not implemented";
228 case 211: $message = "System status, or system help reply";
231 case 214: $message = "Help message";
236 case 220: $message = "Service ready";
239 case 221: $message = "Service closing transmission channel";
242 case 421: $message = "Service not available, closing chanel";
247 case 250: $message = "Requested mail action okay, completed";
250 case 251: $message = "User not local; will forward";
253 case 450: $message = "Requested mail action not taken: mailbox unavailable";
256 case 550: $message = "Requested action not taken: mailbox unavailable";
259 case 451: $message = "Requested action aborted: error in processing";
262 case 551: $message = "User not local; please try forwarding";
265 case 452: $message = "Requested action not taken: insufficient system storage";
268 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
271 case 553: $message = "Requested action not taken: mailbox name not allowed";
274 case 354: $message = "Start mail input; end with .";
277 case 554: $message = "Transaction failed";
280 default: $message = "Unknown response: $line";
287 echo "<HTML><BODY BGCOLOR=FFFFFF>";
289 echo "<BR><B>ERROR</B><BR><BR>";
290 echo " <B>Error Number: </B>$err_num<BR>";
291 echo " <B>Reason: </B>$message<BR>";
292 echo "<B>Server Response: </B>$line<BR>";
293 echo "<BR>MAIL NOT SENT";
294 echo "</TT></BODY></HTML>";
300 function sendMessage($t, $c, $b, $subject, $body) {
303 if ($useSendmail==true) {
304 sendSendmail($t, $c, $b, $subject, $body);
306 sendSMTP($t, $c, $b, $subject, $body);