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 // This is to make sure noone is giving a filename in another
29 $localname = ereg_replace ("\\/", "", $localname);
31 $fileinfo = fopen ($attachment_dir.$localname.".info", "r");
32 $filetype = fgets ($fileinfo, 8192);
34 $filetype = trim ($filetype);
36 $filetype = "application/octet-stream";
38 fputs ($fp, "--".mimeBoundary()."\n");
39 fputs ($fp, "Content-Type: $filetype\n");
40 fputs ($fp, "Content-Disposition: attachment; filename=\"$remotename\"\n");
41 fputs ($fp, "Content-Transfer-Encoding: base64\n\n");
43 $file = fopen ($attachment_dir.$localname, "r");
44 while ($tmp = fread($file, 57))
45 fputs ($fp, chunk_split(base64_encode($tmp)));
48 unlink ($attachment_dir.$localname);
49 unlink ($attachment_dir.$localname.".info");
53 // Return a nice MIME-boundary
54 function mimeBoundary () {
55 global $mimeBoundaryString, $version, $REMOTE_ADDR, $SERVER_NAME,
58 if ($mimeBoundaryString == "") {
59 $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
61 $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
64 return $mimeBoundaryString;
67 /* Time offset for correct timezone */
68 function timezone () {
69 $diff_second = date("Z");
75 $diff_second = abs($diff_second);
77 $diff_hour = floor ($diff_second / 3600);
78 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
80 $zonename = "(".strftime("%Z").")";
81 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
85 /* Print all the needed RFC822 headers */
86 function write822Header ($fp, $t, $c, $b, $subject) {
87 global $REMOTE_ADDR, $SERVER_NAME;
88 global $data_dir, $username, $domain, $version, $useSendmail;
92 $bcc = parseAddrs($b);
93 $from_addr = "$username@$domain";
94 $reply_to = getPref($data_dir, $username, "reply_to");
95 $from = getPref($data_dir, $username, "full_name");
97 $to_list = getLineOfAddrs($to);
98 $cc_list = getLineOfAddrs($cc);
99 $bcc_list = getLineOfAddrs($bcc);
102 $from = "<$from_addr>";
104 $from = $from . " <$from_addr>";
106 /* This creates an RFC 822 date showing GMT */
107 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
109 /* Make an RFC822 Received: line */
110 fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
111 fputs ($fp, "$date\n");
113 /* The rest of the header */
114 fputs ($fp, "Date: $date\n");
115 fputs ($fp, "Subject: $subject\n"); // Subject
116 fputs ($fp, "From: $from\n"); // Subject
117 fputs ($fp, "To: $to_list\n"); // Who it's TO
120 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
124 fputs($fp, "Reply-To: $reply_to\n");
128 // BCCs is removed from header by sendmail
129 fputs($fp, "Bcc: $bcc_list\n");
133 fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
136 fputs($fp, "MIME-Version: 1.0\n");
139 fputs ($fp, "Content-Type: multipart/mixed; boundary=\"");
140 fputs ($fp, mimeBoundary());
143 fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
144 fputs($fp, "Content-Transfer-Encoding: 8bit\n");
149 function writeBody ($fp, $body) {
151 fputs ($fp, "--".mimeBoundary()."\n");
152 fputs ($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
153 fputs ($fp, "Content-Transfer-Encoding: 8bit\n\n");
154 fputs ($fp, stripslashes($body) . "\n");
156 fputs ($fp, "\n--".mimeBoundary()."--\n");
158 fputs ($fp, stripslashes($body) . "\n");
162 // Send mail using the sendmail command
163 function sendSendmail($t, $c, $b, $subject, $body) {
164 global $sendmail_path, $username, $domain;
166 // open pipe to sendmail
167 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
169 write822Header ($fp, $t, $c, $b, $subject);
170 writeBody($fp, $body);
175 function smtpReadData($smtpConnection) {
176 $read = fgets($smtpConnection, 1024);
180 $data[$counter] = $read;
181 $read = fgets($smtpConnection, 1024);
186 function sendSMTP($t, $c, $b, $subject, $body) {
187 global $username, $domain, $version, $smtpServerAddress, $smtpPort;
189 $to = parseAddrs($t);
190 $cc = parseAddrs($c);
191 $bcc = parseAddrs($b);
192 $from_addr = "$username@$domain";
194 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
195 if (!$smtpConnection) {
196 echo "Error connecting to SMTP Server.<br>";
197 echo "$errorNumber : $errorString<br>";
200 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
203 $to_list = getLineOfAddrs($to);
204 $cc_list = getLineOfAddrs($cc);
206 /** Lets introduce ourselves */
207 fputs($smtpConnection, "HELO $domain\n");
208 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
211 /** Ok, who is sending the message? */
212 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
213 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
216 /** send who the recipients are */
217 for ($i = 0; $i < count($to); $i++
) {
218 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
219 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
222 for ($i = 0; $i < count($cc); $i++
) {
223 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
224 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
227 for ($i = 0; $i < count($bcc); $i++
) {
228 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
229 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
233 /** Lets start sending the actual message */
234 fputs($smtpConnection, "DATA\n");
235 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
238 write822Header ($smtpConnection, $t, $c, $b, $subject);
240 writeBody($smtpConnection, $body); // send the body of the message
242 fputs($smtpConnection, ".\n"); // end the DATA part
243 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
244 $num = errorCheck($tmp);
246 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
249 fputs($smtpConnection, "QUIT\n"); // log off
251 fclose($smtpConnection);
255 function errorCheck($line) {
259 $err_num = substr($line, 0, strpos($line, " "));
261 case 500: $message = "Syntax error; command not recognized";
264 case 501: $message = "Syntax error in parameters or arguments";
267 case 502: $message = "Command not implemented";
270 case 503: $message = "Bad sequence of commands";
273 case 504: $message = "Command parameter not implemented";
278 case 211: $message = "System status, or system help reply";
281 case 214: $message = "Help message";
286 case 220: $message = "Service ready";
289 case 221: $message = "Service closing transmission channel";
292 case 421: $message = "Service not available, closing chanel";
297 case 250: $message = "Requested mail action okay, completed";
300 case 251: $message = "User not local; will forward";
303 case 450: $message = "Requested mail action not taken: mailbox unavailable";
306 case 550: $message = "Requested action not taken: mailbox unavailable";
309 case 451: $message = "Requested action aborted: error in processing";
312 case 551: $message = "User not local; please try forwarding";
315 case 452: $message = "Requested action not taken: insufficient system storage";
318 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
321 case 553: $message = "Requested action not taken: mailbox name not allowed";
324 case 354: $message = "Start mail input; end with .";
327 case 554: $message = "Transaction failed";
330 default: $message = "Unknown response: $line";
337 echo "<HTML><BODY BGCOLOR=FFFFFF>";
339 echo "<BR><B>ERROR</B><BR><BR>";
340 echo " <B>Error Number: </B>$err_num<BR>";
341 echo " <B>Reason: </B>$message<BR>";
342 echo "<B>Server Response: </B>$line<BR>";
343 echo "<BR>MAIL NOT SENT";
344 echo "</TT></BODY></HTML>";
350 function sendMessage($t, $c, $b, $subject, $body) {
353 if ($useSendmail==true) {
354 sendSendmail($t, $c, $b, $subject, $body);
356 sendSMTP($t, $c, $b, $subject, $body);