9859c7edecb9478384f00972ae68c48cb5fc2a05
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 $reply_to = getPref($data_dir, $username, "reply_to");
91 $from = getPref($data_dir, $username, "full_name");
92 $from_addr = getPref($data_dir, $username, "email_address");
95 $from_addr = "$username@$domain";
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 -odb -oi -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,
190 $to = parseAddrs($t);
191 $cc = parseAddrs($c);
192 $bcc = parseAddrs($b);
193 $from_addr = getPref($data_dir, $username, "email_address");
195 if ($from_addr == "")
196 $from_addr = "$username@$domain";
198 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
199 if (!$smtpConnection) {
200 echo "Error connecting to SMTP Server.<br>";
201 echo "$errorNumber : $errorString<br>";
204 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
207 $to_list = getLineOfAddrs($to);
208 $cc_list = getLineOfAddrs($cc);
210 /** Lets introduce ourselves */
211 fputs($smtpConnection, "HELO $domain\n");
212 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
215 /** Ok, who is sending the message? */
216 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
217 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
220 /** send who the recipients are */
221 for ($i = 0; $i < count($to); $i++
) {
222 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
223 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
226 for ($i = 0; $i < count($cc); $i++
) {
227 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
228 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
231 for ($i = 0; $i < count($bcc); $i++
) {
232 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
233 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
237 /** Lets start sending the actual message */
238 fputs($smtpConnection, "DATA\n");
239 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
242 write822Header ($smtpConnection, $t, $c, $b, $subject);
244 writeBody($smtpConnection, $body); // send the body of the message
246 fputs($smtpConnection, ".\n"); // end the DATA part
247 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
248 $num = errorCheck($tmp);
250 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
253 fputs($smtpConnection, "QUIT\n"); // log off
255 fclose($smtpConnection);
259 function errorCheck($line) {
263 $err_num = substr($line, 0, strpos($line, " "));
265 case 500: $message = "Syntax error; command not recognized";
268 case 501: $message = "Syntax error in parameters or arguments";
271 case 502: $message = "Command not implemented";
274 case 503: $message = "Bad sequence of commands";
277 case 504: $message = "Command parameter not implemented";
282 case 211: $message = "System status, or system help reply";
285 case 214: $message = "Help message";
290 case 220: $message = "Service ready";
293 case 221: $message = "Service closing transmission channel";
296 case 421: $message = "Service not available, closing chanel";
301 case 250: $message = "Requested mail action okay, completed";
304 case 251: $message = "User not local; will forward";
307 case 450: $message = "Requested mail action not taken: mailbox unavailable";
310 case 550: $message = "Requested action not taken: mailbox unavailable";
313 case 451: $message = "Requested action aborted: error in processing";
316 case 551: $message = "User not local; please try forwarding";
319 case 452: $message = "Requested action not taken: insufficient system storage";
322 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
325 case 553: $message = "Requested action not taken: mailbox name not allowed";
328 case 354: $message = "Start mail input; end with .";
331 case 554: $message = "Transaction failed";
334 default: $message = "Unknown response: $line";
341 echo "<HTML><BODY BGCOLOR=FFFFFF>";
343 echo "<BR><B>ERROR</B><BR><BR>";
344 echo " <B>Error Number: </B>$err_num<BR>";
345 echo " <B>Reason: </B>$message<BR>";
346 echo "<B>Server Response: </B>$line<BR>";
347 echo "<BR>MAIL NOT SENT";
348 echo "</TT></BODY></HTML>";
354 function sendMessage($t, $c, $b, $subject, $body) {
357 if ($useSendmail==true) {
358 sendSendmail($t, $c, $b, $subject, $body);
360 sendSMTP($t, $c, $b, $subject, $body);