6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains all the functions needed to send messages through
17 function mail($message, $stream=false) {
18 $rfc822_header = $message->rfc822_header
;
19 if (count($message->entities
)) {
20 $boundary = $this->mimeBoundary();
21 $rfc822_header->content_type
->properties
['boundary']='"'.$boundary.'"';
25 $reply_rfc822_header = (isset($message->reply_rfc822_header
)
26 ?
$message->reply_rfc822_header
: '');
27 $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header);
28 $raw_length = strlen($header);
30 $this->preWriteToStream($header);
31 $this->writeToStream($stream, $header);
34 $out = htmlspecialchars($header);
35 $out = str_replace("\r\n",'<BR>',$out);
38 $this->writeBody($message, $stream, $raw_length, $boundary);
42 function writeBody($message, $stream, &$length_raw, $boundary='') {
43 if ($boundary && !$message->rfc822_header
) {
44 $s = '--'.$boundary."\r\n";
45 $s .= $this->prepareMIME_Header($message, $boundary);
46 $length_raw +
= strlen($s);
48 $this->preWriteToStream($s);
49 $this->writeToStream($stream, $s);
52 $out = htmlspecialchars($s);
53 $out = str_replace("\r\n",'<BR>',$out);
57 $this->writeBodyPart($message, $stream, $length_raw);
58 $boundary_depth = substr_count($message->entity_id
,'.');
59 if ($boundary_depth) {
60 $boundary .= '_part'.$boundary_depth;
63 for ($i=0, $entCount=count($message->entities
);$i<$entCount;$i++
) {
64 $msg = $this->writeBody($message->entities
[$i], $stream, $length_raw, $boundary);
65 if ($i == $entCount-1) $last = true;
67 if ($boundary && $last) {
68 $s = "\r\n--".$boundary."--\r\n\r\n";
69 $length_raw +
= strlen($s);
71 $this->preWriteToStream($s);
72 $this->writeToStream($stream, $s);
75 $out = htmlspecialchars($s);
76 $out = str_replace("\r\n",'<BR>',$out);
82 function writeBodyPart($message, $stream, &$length) {
83 if ($message->mime_header
) {
84 $type0 = $message->mime_header
->type0
;
86 $type0 = $message->rfc822_header
->content_type
->type0
;
91 if ($message->body_part
) {
92 $body_part = $message->body_part
;
93 $length +
= $this->clean_crlf($body_part);
95 $this->preWriteToStream($body_part);
96 $this->writeToStream($stream, $body_part);
99 $out = htmlspecialchars($tmp);
100 $out = str_replace("\r\n",'<BR>',$out);
103 } elseif ($message->att_local_name
) {
104 $filename = $message->att_local_name
;
105 $file = fopen ($filename, 'rb');
106 while ($tmp = fgets($file, 4096)) {
107 $length +
= $this->clean_crlf($tmp);
109 $this->preWriteToStream($tmp);
110 $this->writeToStream($stream, $tmp);
113 $out = htmlspecialchars($tmp);
114 $out = str_replace("\r\n",'<BR>',$out);
122 if ($message->body_part
) {
123 $body_part = $message->body_part
;
124 $length +
= $this->clean_crlf($body_part);
126 $this->writeToStream($stream, $body_part);
129 $out = htmlspecialchars($body_part);
130 $out = str_replace("\r\n",'<BR>',$out);
133 } elseif ($message->att_local_name
) {
134 $filename = $message->att_local_name
;
135 $file = fopen ($filename, 'rb');
136 while ($tmp = fread($file, 570)) {
137 $encoded = chunk_split(base64_encode($tmp));
140 $this->writeToStream($stream, $encoded);
143 $out = htmlspecialchars($encoded);
144 $out = str_replace("\r\n",'<BR>',$out);
152 $body_part_trailing = "\r\n";
153 $length +
= strlen($body_part_trailing);
155 $this->preWriteToStream($body_part_trailing);
156 $this->writeToStream($stream, $body_part_trailing);
159 $out = htmlspecialchars($body_part_trailing);
160 $out = str_replace("\r\n",'<BR>',$out);
165 function clean_crlf(&$s) {
166 $s = str_replace("\r\n", "\n", $s);
167 $s = str_replace("\r", "\n", $s);
168 $s = str_replace("\n", "\r\n", $s);
172 function preWriteToStream(&$s) {
175 function writeToStream($stream, $data) {
176 fputs($stream, $data);
179 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
183 function getBcc($bcc) {
187 function prepareMIME_Header($message, $boundary) {
188 $mime_header = $message->mime_header
;
192 $contenttype = 'Content-Type: '. $mime_header->type0
.'/'.
194 if (count($message->entities
)) {
195 $contenttype .= ";\r\n " . 'boundary="'.$boundary.'"';
197 if (isset($mime_header->parameters
['name'])) {
198 $contenttype .= '; name="'.
199 encodeHeader($mime_header->parameters
['name']). '"';
201 if (isset($mime_header->parameters
['charset'])) {
202 $charset = $mime_header->parameters
['charset'];
203 $contenttype .= '; charset="'.
204 encodeHeader($charset). '"';
208 $header[] = $contenttype . $rn;
209 if ($mime_header->description
) {
210 $header[] .= 'Content-Description: ' . $mime_header->description
. $rn;
212 if ($mime_header->encoding
) {
213 $encoding = $mime_header->encoding
;
214 $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding
. $rn;
216 if ($mime_header->type0
== 'text' ||
$mime_header->type0
== 'message') {
217 $header[] .= 'Content-Transfer-Encoding: 8bit' . $rn;
219 $header[] .= 'Content-Transfer-Encoding: base64' . $rn;
222 if ($mime_header->id
) {
223 $header[] .= 'Content-ID: ' . $mime_header->id
. $rn;
225 if ($mime_header->disposition
) {
226 $disposition = $mime_header->disposition
;
227 $contentdisp = 'Content-Disposition: ' . $disposition->name
;
228 if ($disposition->getProperty('filename')) {
229 $contentdisp .= '; filename="'.
230 encodeHeader($disposition->getProperty('filename')). '"';
232 $header[] = $contentdisp . $rn;
234 if ($mime_header->md5
) {
235 $header[] .= 'Content-MD5: ' . $mime_header->md5
. $rn;
237 if ($mime_header->language
) {
238 $header[] .= 'Content-Language: ' . $mime_header->language
. $rn;
241 $cnt = count($header);
243 for ($i = 0 ; $i < $cnt ; $i++
) {
244 $hdr_s .= $this->foldLine($header[$i], 78, ' ');
247 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
251 function prepareRFC822_Header($rfc822_header, $reply_rfc822_header) {
252 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
253 global $version, $useSendmail, $username;
254 global $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
257 /* This creates an RFC 822 date */
258 $date = date("D, j M Y H:i:s ", mktime()) . $this->timezone();
259 /* Create a message-id */
260 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
261 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
262 /* Make an RFC822 Received: line */
263 if (isset($REMOTE_HOST)) {
264 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
266 $received_from = $REMOTE_ADDR;
268 if (isset($HTTP_VIA) ||
isset ($HTTP_X_FORWARDED_FOR)) {
269 if ($HTTP_X_FORWARDED_FOR == '') {
270 $HTTP_X_FORWARDED_FOR = 'unknown';
272 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
275 $header[] = "Received: from $received_from" . $rn;
276 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
277 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
278 $header[] = " $date" . $rn;
279 /* Insert the rest of the header fields */
280 $header[] = 'Message-ID: '. $message_id . $rn;
281 if ($reply_rfc822_header->message_id
) {
282 $header[] = 'In-Reply-To: '.$reply_rfc822_header->message_id
. $rn;
283 $references = $this->calculate_references($reply_rfc822_header);
284 $header[] = 'References: '.$references . $rn;
286 $header[] = "Date: $date" . $rn;
287 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject
) . $rn;
288 $header[] = 'From: '. encodeHeader($rfc822_header->getAddr_s('from')) . $rn;
289 /* RFC2822 if from contains more then 1 address */
290 if (count($rfc822_header->from
) > 1) {
291 $header[] = 'Sender: '. encodeHeader($rfc822_header->getAddr_s('sender')) . $rn;
293 $header[] = 'To: '. encodeHeader($rfc822_header->getAddr_s('to')) . $rn; // Who it's TO
294 if (count($rfc822_header->cc
)) {
295 $header[] = 'Cc: '. encodeHeader($rfc822_header->getAddr_s('cc')) . $rn;
297 if (count($rfc822_header->reply_to
)) {
298 $header[] = 'Reply-To: '. encodeHeader($rfc822_header->getAddr_s('reply_to')) . $rn;
300 /* Sendmail should return true. Default = false */
301 $bcc = $this->getBcc();
302 if ($bcc && count($rfc822_header->bcc
)) {
303 $header[] = 'Bcc: '. encodeHeader($rfc822_header->getAddr_s('bcc')) . $rn;
305 /* Identify SquirrelMail */
306 $header[] = "X-Mailer: SquirrelMail (version $version)" . $rn;
307 /* Do the MIME-stuff */
308 $header[] = "MIME-Version: 1.0" . $rn;
309 $contenttype = 'Content-Type: '. $rfc822_header->content_type
->type0
.'/'.
310 $rfc822_header->content_type
->type1
;
311 if (count($rfc822_header->content_type
->properties
)) {
312 foreach ($rfc822_header->content_type
->properties
as $k => $v) {
314 $contenttype .= ';' .$k.'='.$v;
318 $header[] = $contenttype . $rn;
319 if ($rfc822_header->dnt
) {
320 $dnt = $rfc822_header->getAddr_s('dnt');
322 $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
324 $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
326 if ($rfc822_header->priority
) {
327 $prio = $rfc822_header->priority
;
328 $header[] = 'X-Priority: '.$prio. $rn;
330 case 1: $header[] = 'Importance: High'. $rn; break;
331 case 3: $header[] = 'Importance: Normal'. $rn; break;
332 case 5: $header[] = 'Importance: Low'. $rn; break;
336 /* Insert headers from the $more_headers array */
337 if(count($rfc822_header->more_headers
)) {
338 reset($rfc822_header->more_headers
);
339 foreach ($rfc822_header->more_headers
as $k => $v) {
340 $header[] = $k.': '.$v .$rn;
343 $cnt = count($header);
345 for ($i = 0 ; $i < $cnt ; $i++
) {
346 $hdr_s .= $this->foldLine($header[$i], 78, ' ');
349 $header .= $rn; /* One blank line to separate header and body */
354 * function for cleanly folding of headerlines
356 function foldLine($line, $length, $pre='') {
357 $cnt = strlen($line);
359 if ($cnt > $length) {
360 $fold_string = "\r\n " . $pre;
361 $length -=strlen($fold_string);
362 for ($i=0;$i<($cnt-$length);$i++
) {
364 /* first try to fold at delimiters */
365 for ($j=($i+
$length); $j>$i; --$j) {
368 case (';'): $fold_pos = $i = $j; break;
372 if (!$fold_pos) { /* not succeed yet so we try at spaces & = */
373 for ($j=($i+
$length); $j>$i; $j--) {
376 case ('='): $fold_pos = $i = $j; break;
381 if (!$fold_pos) { /* clean folding didn't work */
382 $i = $j = $fold_pos = $i+
$length;
384 $line = substr_replace($line,$line{$fold_pos}.$fold_string,
386 $cnt +
= strlen($fold_string);
387 $i = $j +
strlen($fold_string);
394 function mimeBoundary () {
395 static $mimeBoundaryString;
397 if ( !isset( $mimeBoundaryString ) ||
398 $mimeBoundaryString == '') {
399 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
400 mt_rand( 10000, 99999 );
402 return $mimeBoundaryString;
405 /* Time offset for correct timezone */
406 function timezone () {
409 $diff_second = date('Z');
411 $diff_second = - $diff_second;
413 if ($diff_second > 0) {
418 $diff_second = abs($diff_second);
419 $diff_hour = floor ($diff_second / 3600);
420 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
421 $zonename = '('.strftime('%Z').')';
422 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
427 function calculate_references($hdr) {
428 $refer = $hdr->references
;
429 if (strlen($refer) > 2) {
430 $refer .= ' ' . $hdr->message_id
;
432 if ($hdr->in_reply_to
) {
433 $refer .= $hdr->in_reply_to
. ' ' . $hdr->message_id
;
435 $refer .= $hdr->message_id
;