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;
28 while (list($localname, $remotename) = each($attachments)) {
29 // This is to make sure noone is giving a filename in another
31 $localname = ereg_replace ("\\/", "", $localname);
33 $fileinfo = fopen ($attachment_dir.$localname.".info", "r");
34 $filetype = fgets ($fileinfo, 8192);
36 $filetype = trim ($filetype);
38 $filetype = "application/octet-stream";
40 $header = "--".mimeBoundary()."\r\n";
41 $header .= "Content-Type: $filetype\r\n";
42 $header .= "Content-Disposition: attachment; filename=\"$remotename\"\r\n";
43 $header .= "Content-Transfer-Encoding: base64\r\n\r\n";
45 $length +
= strlen($header);
47 $file = fopen ($attachment_dir.$localname, "r");
48 while ($tmp = fread($file, 570)) {
49 $encoded = chunk_split(base64_encode($tmp));
50 $length +
= strlen($encoded);
51 fputs ($fp, $encoded);
60 // Delete files that are uploaded for attaching
61 function deleteAttachments() {
62 global $attachments, $attachment_dir;
66 while (list($localname, $remotename) = each($attachments)) {
67 if (!ereg ("\\/", $localname)) {
68 unlink ($attachment_dir.$localname);
69 unlink ($attachment_dir.$localname.".info");
75 // Return a nice MIME-boundary
76 function mimeBoundary () {
77 global $version, $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
79 static $mimeBoundaryString;
81 if ($mimeBoundaryString == "") {
82 $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
84 $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
87 return $mimeBoundaryString;
90 /* Time offset for correct timezone */
91 function timezone () {
92 $diff_second = date("Z");
98 $diff_second = abs($diff_second);
100 $diff_hour = floor ($diff_second / 3600);
101 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
103 $zonename = "(".strftime("%Z").")";
104 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
108 /* Print all the needed RFC822 headers */
109 function write822Header ($fp, $t, $c, $b, $subject) {
110 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
111 global $data_dir, $username, $domain, $version, $useSendmail;
112 global $default_charset;
114 // Storing the header to make sure the header is the same
115 // everytime the header is printed.
116 static $header, $headerlength;
119 $to = parseAddrs($t);
120 $cc = parseAddrs($c);
121 $bcc = parseAddrs($b);
122 $reply_to = getPref($data_dir, $username, "reply_to");
123 $from = getPref($data_dir, $username, "full_name");
124 $from_addr = getPref($data_dir, $username, "email_address");
126 if ($from_addr == "")
127 $from_addr = "$username@$domain";
129 $to_list = getLineOfAddrs($to);
130 $cc_list = getLineOfAddrs($cc);
131 $bcc_list = getLineOfAddrs($bcc);
134 $from = "<$from_addr>";
136 $from = $from . " <$from_addr>";
138 /* This creates an RFC 822 date */
139 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
141 /* Create a message-id */
142 $message_id = "<" . $REMOTE_PORT . "." . $REMOTE_ADDR . ".";
143 $message_id .= time() . "@" . $SERVER_NAME .">";
145 /* Make an RFC822 Received: line */
146 $header = "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ";
147 $header .= "$date\r\n";
149 /* Insert the rest of the header fields */
150 $header .= "Message-ID: $message_id\r\n";
151 $header .= "Date: $date\r\n";
152 $header .= "Subject: $subject\r\n";
153 $header .= "From: $from\r\n";
154 $header .= "To: $to_list \r\n"; // Who it's TO
157 $header .= "Cc: $cc_list\r\n"; // Who the CCs are
161 $header .= "Reply-To: $reply_to\r\n";
165 // BCCs is removed from header by sendmail
166 $header .= "Bcc: $bcc_list\r\n";
170 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
173 $header .= "MIME-Version: 1.0\r\n";
176 $header .= "Content-Type: multipart/mixed; boundary=\"";
177 $header .= mimeBoundary();
180 if ($default_charset != "")
181 $header .= "Content-Type: text/plain; charset=$default_charset\r\n";
183 $header .= "Content-Type: text/plain;\r\n";
184 $header .= "Content-Transfer-Encoding: 8bit\r\n";
186 $header .= "\r\n"; // One blank line to separate header and body
188 $headerlength = strlen($header);
192 fputs ($fp, $header);
194 return $headerlength;
198 function writeBody ($fp, $passedBody) {
199 global $default_charset;
201 $attachmentlength = 0;
204 $body = "--".mimeBoundary()."\r\n";
206 if ($default_charset != "")
207 $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
209 $body .= "Content-Type: text/plain\r\n";
211 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
212 $body .= stripslashes($passedBody) . "\r\n";
215 $attachmentlength = attachFiles($fp);
217 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
218 fputs ($fp, $postbody);
220 $body = stripslashes($passedBody) . "\r\n";
223 fputs ($fp, $postbody);
226 return (strlen($body) +
strlen($postbody) +
$attachmentlength);
229 // Send mail using the sendmail command
230 function sendSendmail($t, $c, $b, $subject, $body) {
231 global $sendmail_path, $username, $domain;
233 // open pipe to sendmail
234 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
236 $headerlength = write822Header ($fp, $t, $c, $b, $subject);
237 $bodylength = writeBody($fp, $body);
241 return ($headerlength +
$bodylenght);
244 function smtpReadData($smtpConnection) {
245 $read = fgets($smtpConnection, 1024);
249 $data[$counter] = $read;
250 $read = fgets($smtpConnection, 1024);
255 function sendSMTP($t, $c, $b, $subject, $body) {
256 global $username, $domain, $version, $smtpServerAddress, $smtpPort,
259 $to = parseAddrs($t);
260 $cc = parseAddrs($c);
261 $bcc = parseAddrs($b);
262 $from_addr = getPref($data_dir, $username, "email_address");
264 if ($from_addr == "")
265 $from_addr = "$username@$domain";
267 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
268 if (!$smtpConnection) {
269 echo "Error connecting to SMTP Server.<br>";
270 echo "$errorNumber : $errorString<br>";
273 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
276 $to_list = getLineOfAddrs($to);
277 $cc_list = getLineOfAddrs($cc);
279 /** Lets introduce ourselves */
280 fputs($smtpConnection, "HELO $domain\r\n");
281 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
284 /** Ok, who is sending the message? */
285 fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n");
286 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
289 /** send who the recipients are */
290 for ($i = 0; $i < count($to); $i++
) {
291 fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n");
292 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
295 for ($i = 0; $i < count($cc); $i++
) {
296 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n");
297 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
300 for ($i = 0; $i < count($bcc); $i++
) {
301 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n");
302 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
306 /** Lets start sending the actual message */
307 fputs($smtpConnection, "DATA\r\n");
308 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
312 $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject);
313 $bodylength = writeBody($smtpConnection, $body);
315 fputs($smtpConnection, ".\r\n"); // end the DATA part
316 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
317 $num = errorCheck($tmp);
319 echo "ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
322 fputs($smtpConnection, "QUIT\r\n"); // log off
324 fclose($smtpConnection);
326 return ($headerlength +
$bodylength);
330 function errorCheck($line) {
335 $err_num = substr($line, 0, strpos($line, " "));
337 case 500: $message = "Syntax error; command not recognized";
340 case 501: $message = "Syntax error in parameters or arguments";
343 case 502: $message = "Command not implemented";
346 case 503: $message = "Bad sequence of commands";
349 case 504: $message = "Command parameter not implemented";
354 case 211: $message = "System status, or system help reply";
357 case 214: $message = "Help message";
362 case 220: $message = "Service ready";
365 case 221: $message = "Service closing transmission channel";
368 case 421: $message = "Service not available, closing chanel";
373 case 250: $message = "Requested mail action okay, completed";
376 case 251: $message = "User not local; will forward";
379 case 450: $message = "Requested mail action not taken: mailbox unavailable";
382 case 550: $message = "Requested action not taken: mailbox unavailable";
385 case 451: $message = "Requested action aborted: error in processing";
388 case 551: $message = "User not local; please try forwarding";
391 case 452: $message = "Requested action not taken: insufficient system storage";
394 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
397 case 553: $message = "Requested action not taken: mailbox name not allowed";
400 case 354: $message = "Start mail input; end with .";
403 case 554: $message = "Transaction failed";
406 default: $message = "Unknown response: $line";
413 echo "<HTML><BODY BGCOLOR=ffffff>";
415 echo "<BR><B>ERROR</B><BR><BR>";
416 echo " <B>Error Number: </B>$err_num<BR>";
417 echo " <B>Reason: </B>$message<BR>";
418 echo "<B>Server Response: </B>$line<BR>";
419 echo "<BR>MAIL NOT SENT";
420 echo "</TT></BODY></HTML>";
426 function sendMessage($t, $c, $b, $subject, $body) {
428 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
430 if ($useSendmail==true) {
431 $length = sendSendmail($t, $c, $b, $subject, $body);
433 $length = sendSMTP($t, $c, $b, $subject, $body);
436 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
437 sqimap_append ($imap_stream, $sent_folder, $length);
438 write822Header ($imap_stream, $t, $c, $b, $subject);
439 writeBody ($imap_stream, $body);
440 sqimap_append_done ($imap_stream);
443 // Delete the files uploaded for attaching (if any).