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 /* Encoding 8-bit characters */
139 $subject = encodeHeader($subject);
140 $from = encodeHeader($from);
142 /* This creates an RFC 822 date */
143 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
145 /* Create a message-id */
146 $message_id = "<" . $REMOTE_PORT . "." . $REMOTE_ADDR . ".";
147 $message_id .= time() . "@" . $SERVER_NAME .">";
149 /* Make an RFC822 Received: line */
150 $header = "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ";
151 $header .= "$date\r\n";
153 /* Insert the rest of the header fields */
154 $header .= "Message-ID: $message_id\r\n";
155 $header .= "Date: $date\r\n";
156 $header .= "Subject: $subject\r\n";
157 $header .= "From: $from\r\n";
158 $header .= "To: $to_list \r\n"; // Who it's TO
161 $header .= "Cc: $cc_list\r\n"; // Who the CCs are
165 $header .= "Reply-To: $reply_to\r\n";
169 // BCCs is removed from header by sendmail
170 $header .= "Bcc: $bcc_list\r\n";
174 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
177 $header .= "MIME-Version: 1.0\r\n";
180 $header .= "Content-Type: multipart/mixed; boundary=\"";
181 $header .= mimeBoundary();
184 if ($default_charset != "")
185 $header .= "Content-Type: text/plain; charset=$default_charset\r\n";
187 $header .= "Content-Type: text/plain;\r\n";
188 $header .= "Content-Transfer-Encoding: 8bit\r\n";
190 $header .= "\r\n"; // One blank line to separate header and body
192 $headerlength = strlen($header);
196 fputs ($fp, $header);
198 return $headerlength;
202 function writeBody ($fp, $passedBody) {
203 global $default_charset;
205 $attachmentlength = 0;
208 $body = "--".mimeBoundary()."\r\n";
210 if ($default_charset != "")
211 $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
213 $body .= "Content-Type: text/plain\r\n";
215 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
216 $body .= stripslashes($passedBody) . "\r\n";
219 $attachmentlength = attachFiles($fp);
221 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
222 fputs ($fp, $postbody);
224 $body = stripslashes($passedBody) . "\r\n";
227 fputs ($fp, $postbody);
230 return (strlen($body) +
strlen($postbody) +
$attachmentlength);
233 // Send mail using the sendmail command
234 function sendSendmail($t, $c, $b, $subject, $body) {
235 global $sendmail_path, $username, $domain;
237 // open pipe to sendmail
238 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
240 $headerlength = write822Header ($fp, $t, $c, $b, $subject);
241 $bodylength = writeBody($fp, $body);
245 return ($headerlength +
$bodylenght);
248 function smtpReadData($smtpConnection) {
249 $read = fgets($smtpConnection, 1024);
253 $data[$counter] = $read;
254 $read = fgets($smtpConnection, 1024);
259 function sendSMTP($t, $c, $b, $subject, $body) {
260 global $username, $domain, $version, $smtpServerAddress, $smtpPort,
263 $to = parseAddrs($t);
264 $cc = parseAddrs($c);
265 $bcc = parseAddrs($b);
266 $from_addr = getPref($data_dir, $username, "email_address");
268 if ($from_addr == "")
269 $from_addr = "$username@$domain";
271 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
272 if (!$smtpConnection) {
273 echo "Error connecting to SMTP Server.<br>";
274 echo "$errorNumber : $errorString<br>";
277 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
280 $to_list = getLineOfAddrs($to);
281 $cc_list = getLineOfAddrs($cc);
283 /** Lets introduce ourselves */
284 fputs($smtpConnection, "HELO $domain\r\n");
285 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
288 /** Ok, who is sending the message? */
289 fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n");
290 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
293 /** send who the recipients are */
294 for ($i = 0; $i < count($to); $i++
) {
295 fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n");
296 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
299 for ($i = 0; $i < count($cc); $i++
) {
300 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n");
301 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
304 for ($i = 0; $i < count($bcc); $i++
) {
305 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n");
306 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
310 /** Lets start sending the actual message */
311 fputs($smtpConnection, "DATA\r\n");
312 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
316 $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject);
317 $bodylength = writeBody($smtpConnection, $body);
319 fputs($smtpConnection, ".\r\n"); // end the DATA part
320 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
321 $num = errorCheck($tmp);
323 echo "ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
326 fputs($smtpConnection, "QUIT\r\n"); // log off
328 fclose($smtpConnection);
330 return ($headerlength +
$bodylength);
334 function errorCheck($line) {
339 $err_num = substr($line, 0, strpos($line, " "));
341 case 500: $message = "Syntax error; command not recognized";
344 case 501: $message = "Syntax error in parameters or arguments";
347 case 502: $message = "Command not implemented";
350 case 503: $message = "Bad sequence of commands";
353 case 504: $message = "Command parameter not implemented";
358 case 211: $message = "System status, or system help reply";
361 case 214: $message = "Help message";
366 case 220: $message = "Service ready";
369 case 221: $message = "Service closing transmission channel";
372 case 421: $message = "Service not available, closing chanel";
377 case 250: $message = "Requested mail action okay, completed";
380 case 251: $message = "User not local; will forward";
383 case 450: $message = "Requested mail action not taken: mailbox unavailable";
386 case 550: $message = "Requested action not taken: mailbox unavailable";
389 case 451: $message = "Requested action aborted: error in processing";
392 case 551: $message = "User not local; please try forwarding";
395 case 452: $message = "Requested action not taken: insufficient system storage";
398 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
401 case 553: $message = "Requested action not taken: mailbox name not allowed";
404 case 354: $message = "Start mail input; end with .";
407 case 554: $message = "Transaction failed";
410 default: $message = "Unknown response: $line";
417 echo "<HTML><BODY BGCOLOR=#ffffff>";
419 echo "<BR><B>ERROR</B><BR><BR>";
420 echo " <B>Error Number: </B>$err_num<BR>";
421 echo " <B>Reason: </B>$message<BR>";
422 echo "<B>Server Response: </B>$line<BR>";
423 echo "<BR>MAIL NOT SENT";
424 echo "</TT></BODY></HTML>";
430 function sendMessage($t, $c, $b, $subject, $body) {
431 global $useSendmail, $msg_id, $is_reply;
432 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
434 if ($useSendmail==true) {
435 $length = sendSendmail($t, $c, $b, $subject, $body);
437 $length = sendSMTP($t, $c, $b, $subject, $body);
440 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
441 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
442 sqimap_append ($imap_stream, $sent_folder, $length);
443 write822Header ($imap_stream, $t, $c, $b, $subject);
444 writeBody ($imap_stream, $body);
445 sqimap_append_done ($imap_stream);
448 // Delete the files uploaded for attaching (if any).