7cfcba7e3297188fe4ddb1e9e596d9280dbe3c5c
4 ** This contains all the functions needed to send messages through
5 ** an smtp server or sendmail.
10 // Returns true only if this message is multipart
11 function isMultipart () {
14 if (count($attachments)>0)
20 // Attach the files that are due to be attached
21 function attachFiles ($fp) {
22 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 $header = "--".mimeBoundary()."\r\n";
39 $header .= "Content-Type: $filetype\n";
40 $header .= "Content-Disposition: attachment; filename=\"$remotename\"\r\n";
41 $header .= "Content-Transfer-Encoding: base64\r\n\r\n";
43 $length +
= strlen($header);
45 $file = fopen ($attachment_dir.$localname, "r");
46 while ($tmp = fread($file, 570)) {
47 $encoded = chunk_split(base64_encode($tmp));
48 $length +
= strlen($encoded);
49 fputs ($fp, $encoded);
53 unlink ($attachment_dir.$localname);
54 unlink ($attachment_dir.$localname.".info");
60 // Return a nice MIME-boundary
61 function mimeBoundary () {
62 global $version, $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
64 static $mimeBoundaryString;
66 if ($mimeBoundaryString == "") {
67 $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
69 $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
72 return $mimeBoundaryString;
75 /* Time offset for correct timezone */
76 function timezone () {
77 $diff_second = date("Z");
83 $diff_second = abs($diff_second);
85 $diff_hour = floor ($diff_second / 3600);
86 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
88 $zonename = "(".strftime("%Z").")";
89 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
93 /* Print all the needed RFC822 headers */
94 function write822Header ($fp, $t, $c, $b, $subject) {
95 global $REMOTE_ADDR, $SERVER_NAME;
96 global $data_dir, $username, $domain, $version, $useSendmail;
98 // Storing the header to make sure the header is the same
99 // everytime the header is printed.
100 static $header, $headerlength;
103 $to = parseAddrs($t);
104 $cc = parseAddrs($c);
105 $bcc = parseAddrs($b);
106 $reply_to = getPref($data_dir, $username, "reply_to");
107 $from = getPref($data_dir, $username, "full_name");
108 $from_addr = getPref($data_dir, $username, "email_address");
110 if ($from_addr == "")
111 $from_addr = "$username@$domain";
113 $to_list = getLineOfAddrs($to);
114 $cc_list = getLineOfAddrs($cc);
115 $bcc_list = getLineOfAddrs($bcc);
118 $from = "<$from_addr>";
120 $from = $from . " <$from_addr>";
122 /* This creates an RFC 822 date showing GMT */
123 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
125 /* Make an RFC822 Received: line */
126 $header = "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ";
127 $header .= "$date\n";
129 /* The rest of the header */
130 $header .= "Date: $date\r\n";
131 $header .= "Subject: $subject\r\n";
132 $header .= "From: $from\r\n";
133 $header .= "To: $to_list \r\n"; // Who it's TO
136 $header .= "Cc: $cc_list\r\n"; // Who the CCs are
140 $header .= "Reply-To: $reply_to\r\n";
144 // BCCs is removed from header by sendmail
145 $header .= "Bcc: $bcc_list\r\n";
149 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
152 $header .= "MIME-Version: 1.0\n";
155 $header .= "Content-Type: multipart/mixed; boundary=\"";
156 $header .= mimeBoundary();
159 $header .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
160 $header .= "Content-Transfer-Encoding: 8bit\r\n";
162 $header .= "\r\n"; // One blank line to separate header and body
164 $headerlength = strlen($header);
168 fputs ($fp, $header);
170 return $headerlength;
174 function writeBody ($fp, $passedBody) {
175 $attachmentlength = 0;
178 $body = "--".mimeBoundary()."\r\n";
179 $body .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
180 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
181 $body .= stripslashes($passedBody) . "\r\n";
184 $attachmentlenght = attachFiles($fp);
186 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
187 fputs ($fp, $postbody);
189 $body = stripslashes($passedBody) . "\r\n";
192 fputs ($fp, $postbody);
195 return (strlen($body) +
strlen($postbody) +
$attachmentlength);
198 // Send mail using the sendmail command
199 function sendSendmail($t, $c, $b, $subject, $body) {
200 global $sendmail_path, $username, $domain;
202 // open pipe to sendmail
203 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
205 $headerlength = write822Header ($fp, $t, $c, $b, $subject);
206 $bodylength = writeBody($fp, $body);
210 return ($headerlength +
$bodylenght);
213 function smtpReadData($smtpConnection) {
214 $read = fgets($smtpConnection, 1024);
218 $data[$counter] = $read;
219 $read = fgets($smtpConnection, 1024);
224 function sendSMTP($t, $c, $b, $subject, $body) {
225 global $username, $domain, $version, $smtpServerAddress, $smtpPort,
228 $to = parseAddrs($t);
229 $cc = parseAddrs($c);
230 $bcc = parseAddrs($b);
231 $from_addr = getPref($data_dir, $username, "email_address");
233 if ($from_addr == "")
234 $from_addr = "$username@$domain";
236 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
237 if (!$smtpConnection) {
238 echo "Error connecting to SMTP Server.<br>";
239 echo "$errorNumber : $errorString<br>";
242 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
245 $to_list = getLineOfAddrs($to);
246 $cc_list = getLineOfAddrs($cc);
248 /** Lets introduce ourselves */
249 fputs($smtpConnection, "HELO $domain\r\n");
250 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
253 /** Ok, who is sending the message? */
254 fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n");
255 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
258 /** send who the recipients are */
259 for ($i = 0; $i < count($to); $i++
) {
260 fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n");
261 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
264 for ($i = 0; $i < count($cc); $i++
) {
265 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n");
266 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
269 for ($i = 0; $i < count($bcc); $i++
) {
270 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n");
271 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
275 /** Lets start sending the actual message */
276 fputs($smtpConnection, "DATA\r\n");
277 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
281 $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject);
282 $bodylength = writeBody($smtpConnection, $body);
284 fputs($smtpConnection, ".\r\n"); // end the DATA part
285 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
286 $num = errorCheck($tmp);
288 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
291 fputs($smtpConnection, "QUIT\r\n"); // log off
293 fclose($smtpConnection);
295 return ($headerlength +
$bodylength);
299 function errorCheck($line) {
303 $err_num = substr($line, 0, strpos($line, " "));
305 case 500: $message = "Syntax error; command not recognized";
308 case 501: $message = "Syntax error in parameters or arguments";
311 case 502: $message = "Command not implemented";
314 case 503: $message = "Bad sequence of commands";
317 case 504: $message = "Command parameter not implemented";
322 case 211: $message = "System status, or system help reply";
325 case 214: $message = "Help message";
330 case 220: $message = "Service ready";
333 case 221: $message = "Service closing transmission channel";
336 case 421: $message = "Service not available, closing chanel";
341 case 250: $message = "Requested mail action okay, completed";
344 case 251: $message = "User not local; will forward";
347 case 450: $message = "Requested mail action not taken: mailbox unavailable";
350 case 550: $message = "Requested action not taken: mailbox unavailable";
353 case 451: $message = "Requested action aborted: error in processing";
356 case 551: $message = "User not local; please try forwarding";
359 case 452: $message = "Requested action not taken: insufficient system storage";
362 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
365 case 553: $message = "Requested action not taken: mailbox name not allowed";
368 case 354: $message = "Start mail input; end with .";
371 case 554: $message = "Transaction failed";
374 default: $message = "Unknown response: $line";
381 echo "<HTML><BODY BGCOLOR=FFFFFF>";
383 echo "<BR><B>ERROR</B><BR><BR>";
384 echo " <B>Error Number: </B>$err_num<BR>";
385 echo " <B>Reason: </B>$message<BR>";
386 echo "<B>Server Response: </B>$line<BR>";
387 echo "<BR>MAIL NOT SENT";
388 echo "</TT></BODY></HTML>";
394 function sendMessage($t, $c, $b, $subject, $body) {
396 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress;
398 if ($useSendmail==true) {
399 $length = sendSendmail($t, $c, $b, $subject, $body);
401 $length = sendSMTP($t, $c, $b, $subject, $body);
404 // This is a proposed interface to save messages in the sent folder
407 // $imap_stream = sqimap_login($username, $key, $imapServerAddress, 1);
408 // sqimap_append ($imap_stream, $sent_folder, $length);
409 // write822Header ($imap_stream, .....);
410 // writeBody ($imap_stream, ....);
411 // sqimap_append_done($imap_stream);
413 // Or something like that...
415 //$imap_stream = sqimap_login($username, $key, $imapServerAddress, 1);
416 //sqimap_append ($imap_stream, $sent_folder, $body, $t, $c, $b, $subject, $data_dir, $username, $domain, $version);