d6bf69934dab5d3690361fd47cbcea839bde1135
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) {
24 global $attachments, $attachment_dir;
26 while (list($localname, $remotename) = each($attachments)) {
27 $fileinfo = fopen ($attachment_dir.$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 ($attachment_dir.$localname, "r");
40 while ($tmp = fread($file, 57))
41 fputs ($fp, chunk_split(base64_encode($tmp)));
44 unlink ($attachment_dir.$localname);
45 unlink ($attachment_dir.$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 /* Time offset for correct timezone */
64 function timezone () {
65 $diff_second = date("Z");
71 $diff_second = abs($diff_second);
73 $diff_hour = floor ($diff_second / 3600);
74 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
76 $zonename = "(".strftime("%Z").")";
77 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
81 /* Print all the needed RFC822 headers */
82 function write822Header ($fp, $t, $c, $b, $subject) {
83 global $REMOTE_ADDR, $SERVER_NAME;
84 global $data_dir, $username, $domain, $version, $useSendmail;
88 $bcc = parseAddrs($b);
89 $from_addr = "$username@$domain";
90 $reply_to = getPref($data_dir, $username, "reply_to");
91 $from = getPref($data_dir, $username, "full_name");
93 $to_list = getLineOfAddrs($to);
94 $cc_list = getLineOfAddrs($cc);
95 $bcc_list = getLineOfAddrs($bcc);
98 $from = "<$from_addr>";
100 $from = $from . " <$from_addr>";
102 /* This creates an RFC 822 date showing GMT */
103 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
105 /* Make an RFC822 Received: line */
106 fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
107 fputs ($fp, "$date\n");
109 /* The rest of the header */
110 fputs ($fp, "Date: $date\n");
111 fputs ($fp, "Subject: $subject\n"); // Subject
112 fputs ($fp, "From: $from\n"); // Subject
113 fputs ($fp, "To: $to_list\n"); // Who it's TO
116 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
120 fputs($fp, "Reply-To: $reply_to\n");
124 // BCCs is removed from header by sendmail
125 fputs($fp, "Bcc: $bcc_list\n");
129 fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
132 fputs($fp, "MIME-Version: 1.0\n");
135 fputs ($fp, "Content-Type: multipart/mixed; boundary=\"");
136 fputs ($fp, mimeBoundary());
139 fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
140 fputs($fp, "Content-Transfer-Encoding: 8bit\n");
145 function writeBody ($fp, $body) {
147 fputs ($fp, "--".mimeBoundary()."\n");
148 fputs ($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
149 fputs ($fp, "Content-Transfer-Encoding: 8bit\n\n");
150 fputs ($fp, stripslashes($body) . "\n");
152 fputs ($fp, "\n--".mimeBoundary()."--\n");
154 fputs ($fp, stripslashes($body) . "\n");
158 // Send mail using the sendmail command
159 function sendSendmail($t, $c, $b, $subject, $body) {
160 global $sendmail_path, $username, $domain;
162 // open pipe to sendmail
163 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
165 write822Header ($fp, $t, $c, $b, $subject);
166 writeBody($fp, $body);
171 function smtpReadData($smtpConnection) {
172 $read = fgets($smtpConnection, 1024);
176 $data[$counter] = $read;
177 $read = fgets($smtpConnection, 1024);
182 function sendSMTP($t, $c, $b, $subject, $body) {
183 global $username, $domain, $version, $smtpServerAddress, $smtpPort;
185 $to = parseAddrs($t);
186 $cc = parseAddrs($c);
187 $bcc = parseAddrs($b);
188 $from_addr = "$username@$domain";
190 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
191 if (!$smtpConnection) {
192 echo "Error connecting to SMTP Server.<br>";
193 echo "$errorNumber : $errorString<br>";
196 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
199 $to_list = getLineOfAddrs($to);
200 $cc_list = getLineOfAddrs($cc);
202 /** Lets introduce ourselves */
203 fputs($smtpConnection, "HELO $domain\n");
204 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
207 /** Ok, who is sending the message? */
208 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
209 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
212 /** send who the recipients are */
213 for ($i = 0; $i < count($to); $i++
) {
214 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
215 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
218 for ($i = 0; $i < count($cc); $i++
) {
219 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
220 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
223 for ($i = 0; $i < count($bcc); $i++
) {
224 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
225 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
229 /** Lets start sending the actual message */
230 fputs($smtpConnection, "DATA\n");
231 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
234 write822Header ($smtpConnection, $t, $c, $b, $subject);
236 writeBody($smtpConnection, $body); // send the body of the message
238 fputs($smtpConnection, ".\n"); // end the DATA part
239 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
240 $num = errorCheck($tmp);
242 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
245 fputs($smtpConnection, "QUIT\n"); // log off
247 fclose($smtpConnection);
251 function errorCheck($line) {
255 $err_num = substr($line, 0, strpos($line, " "));
257 case 500: $message = "Syntax error; command not recognized";
260 case 501: $message = "Syntax error in parameters or arguments";
263 case 502: $message = "Command not implemented";
266 case 503: $message = "Bad sequence of commands";
269 case 504: $message = "Command parameter not implemented";
274 case 211: $message = "System status, or system help reply";
277 case 214: $message = "Help message";
282 case 220: $message = "Service ready";
285 case 221: $message = "Service closing transmission channel";
288 case 421: $message = "Service not available, closing chanel";
293 case 250: $message = "Requested mail action okay, completed";
296 case 251: $message = "User not local; will forward";
299 case 450: $message = "Requested mail action not taken: mailbox unavailable";
302 case 550: $message = "Requested action not taken: mailbox unavailable";
305 case 451: $message = "Requested action aborted: error in processing";
308 case 551: $message = "User not local; please try forwarding";
311 case 452: $message = "Requested action not taken: insufficient system storage";
314 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
317 case 553: $message = "Requested action not taken: mailbox name not allowed";
320 case 354: $message = "Start mail input; end with .";
323 case 554: $message = "Transaction failed";
326 default: $message = "Unknown response: $line";
333 echo "<HTML><BODY BGCOLOR=FFFFFF>";
335 echo "<BR><B>ERROR</B><BR><BR>";
336 echo " <B>Error Number: </B>$err_num<BR>";
337 echo " <B>Reason: </B>$message<BR>";
338 echo "<B>Server Response: </B>$line<BR>";
339 echo "<BR>MAIL NOT SENT";
340 echo "</TT></BODY></HTML>";
346 function sendMessage($t, $c, $b, $subject, $body) {
349 if ($useSendmail==true) {
350 sendSendmail($t, $c, $b, $subject, $body);
352 sendSMTP($t, $c, $b, $subject, $body);