4106e066f15090ba2dbd1ac3ff3d17f613b348bc
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) {
19 $rfc822_header = $message->rfc822_header
;
20 if (count($message->entities
)) {
21 $boundary = mimeBoundary();
22 $rfc822_header->contenttype
->properties
['boundary']=$boundary;
26 $header = prepareRFC822_Header($rfc822_header);
27 $raw_length = strlen($header);
29 $this->preWriteToStream($s);
30 $this->writeToStream($stream, $s);
32 writeBody($message, $stream, $length_raw, $boundary);
36 function writeBody($message, $stream, &$length_raw, $boundary='') {
38 $s = '--'.$boundary."\r\n";
39 $s .= $this->prepareMIME_Header($message, $boundary);
40 $length_raw +
= strlen($s);
42 $this->preWriteToStream($s);
43 $this->writeToStream($stream, $s);
46 writeBodyPart($message, $stream, $length_raw);
47 $boundary_depth = substr_count($message->entity_id
,'.');
48 if ($boundary_depth) {
49 $boundary .= '_part'.$boundary_depth;
51 for ($i=0, $entCount=count($message->entities
);$i<$entCount;$i++
) {
52 $msg = writeBody($message->entities
[$i], $stream, $length_raw, $boundary);
55 $s = '--'.$boundary."--\r\n";
56 $length_raw +
= strlen($s);
58 $this->preWriteToStream($s);
59 $this->writeToStream($stream, $s);
64 function writeBodyPart($message, $stream, &$length) {
66 switch ($message->type0
) {
69 if ($message->body_part
) {
70 $body_part = $message->body_part
;
71 $length +
= $this->clean_crlf($body_part);
73 $this->preWriteToStream($body_part);
74 $this->writeToStream($stream, $body_part)
76 } elseif ($message->att_local_name
) {
77 $filename = $message->att_local_name
;
78 $file = fopen ($filename, 'rb');
79 while ($tmp = fgets($file, 4096)) {
80 $length +
= $this->clean_crlf($tmp);
82 $this->preWriteToStream($tmp);
83 $this->writeToStream($stream, $tmp);
90 if ($message->body_part
) {
91 $body_part = $message->body_part
;
92 $length +
= $this->clean_crlf($body_part);
94 $this->writeToStream($stream, $body_part)
96 } elseif ($message->att_local_name
) {
97 $filename = $message->att_local_name
;
98 $file = fopen ($filename, 'rb');
99 while ($tmp = fread($file, 1520)) {
100 $encoded = chunk_split(base64_encode($tmp));
101 $length +
= strlen($encoded);
103 $this->writeToStream($stream, $encoded);
112 function clean_crlf(&$s) {
113 $s = str_replace("\r\n", "\n", $s);
114 $s = str_replace("\r", "\n", $s);
115 $s = str_replace("\n", "\r\n", $s);
119 function preWriteToStream(&$s) {
122 function writeToStream($stream, $data) {
125 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
129 function prepareMIME_Header($message, $boundary) {
130 $mime_header = $message->header
;
134 $contenttype = 'Content-Type: '. $mime_header->contenttype
->type0
.'/'.
135 $mime_header->contenttype
->type1
;
136 if (count($message->entities
)) {
137 $contenttype .= ";\r\n " . 'boundary="'.$boundary.'"';
139 if (isset($mime_header->parameters
['name'])) {
140 $contenttype .= ";\r\n " . 'name="'.
141 encodeHeader($mime_header->parameters
['name']). '"';
143 $header[] = $contenttype . $rn;
144 if ($mime_header->description
) {
145 $header[] .= 'Content-Description: ' . $mime_header->description
. $rn;
147 if ($mime_header->encoding
) {
148 $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding
. $rn;
150 if ($mime_header->id
) {
151 $header[] .= 'Content-ID: ' . $mime_header->id
. $rn;
153 if ($mime_header->disposition
) {
154 $contentdisp .= 'Content-Disposition: ' . $mime_header->disposition
;
155 if (isset($mime_header->parameters
['filename'])) {
156 $contentdisp .= ";\r\n " . 'filename="'.
157 encodeHeader($mime_header->parameters
['filename']). '"';
159 $header[] = $contentdisp . $rn;
161 if ($mime_header->md5
) {
162 $header[] .= 'Content-MD5: ' . $mime_header->md5
. $rn;
164 if ($mime_header->language
) {
165 $header[] .= 'Content-Language: ' . $mime_header->language
. $rn;
168 $cnt = count($header);
170 for ($i = 0 ; $i < $cnt ; $i++
) {
171 $hdr_s .= foldLine($header[$i], 78, ' ');
174 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
178 function prepareRFC822_Header($rfc822_header) {
179 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
180 global $version, $useSendmail, $username;
181 global $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
184 /* This creates an RFC 822 date */
185 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
186 /* Create a message-id */
187 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
188 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
189 /* Make an RFC822 Received: line */
190 if (isset($REMOTE_HOST))
192 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
196 $received_from = $REMOTE_ADDR;
198 if (isset($HTTP_VIA) ||
isset ($HTTP_X_FORWARDED_FOR)) {
199 if ($HTTP_X_FORWARDED_FOR == '') {
200 $HTTP_X_FORWARDED_FOR = 'unknown';
202 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
205 $header[] = "Received: from $received_from" . $rn;
206 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
207 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
208 $header[] = " $date" . $rn;
209 /* Insert the rest of the header fields */
210 $header[] = "Message-ID: $message_id" . $rn;
211 $header[] = "Date: $date" . $rn;
212 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject
) . $rn;
213 $header[] = 'From: '. encodeHeader($rfc822_header->getAddr_s('from')) . $rn;
214 if (count($rfc822_header->from
) > 1) /* RFC2822 if from contains
215 more then 1 address */
217 $header[] = 'Sender: '. encodeHeader($rfc822_header->getAddr_s('sender')) . $rn;
219 $header[] = 'To: '. encodeHeader($rfc822_header->getAddr_s('to')) . $rn; // Who it's TO
220 if (count($rfc_header->cc
))
222 $header[] = 'Cc: '. encodeHeader($rfc822_header->getAddr_s('cc')) . $rn;
224 if (count($rfc822_header->$reply_to))
226 $header[] = 'Reply-To: '. encodeHeader($rfc822_header->getAddr_s('reply_to')) . $rn;
228 if (count($rfc_header->bcc
) && $useSendmail)
230 $header[] = 'Bcc: '. encodeHeader($rfc822_header->getAddr_s('bcc')) . $rn;
232 /* Identify SquirrelMail */
233 $header[] = "X-Mailer: SquirrelMail (version $version)" . $rn;
234 /* Do the MIME-stuff */
235 $header[] = "MIME-Version: 1.0" . $rn;
236 $contenttype = 'Content-Type: '. $rfc822_header->contenttype
->type0
.'/'.
237 $rfc822_header->contenttype
->type1
;
238 if (count($rfc822_header->contenttype
->properties
))
240 foreach ($rfc822_header->contenttype
->properties
as $k => $v)
242 $contenttype .= ';'. "\r\n " .$k.'='.$v; /* FOLDING */
245 $header[] = $contenttype . $rn;
246 if ($rfc822_header->dnt
)
248 $dnt = $rfc822_header->getAddr_s('dnt');
250 $header[] = 'X-Confirm-Reading-To: '.$dnt;
252 $header[] = 'Disposition-Notification-To: '.$dnt;
254 if ($rfc822_header->priority
)
256 $prio = $rfc822_header->priority
;
257 $header[] = 'X-Priority: '.$prio;
261 $header[] = 'Importance: High';
262 $header[] = 'X-MSMail-Priority: High';
265 $header[] = 'Importance: Normal';
266 $header[] = 'X-MSMail-Priority: Normal';
269 $header[] = 'Importance: Low';
270 $header[] = 'X-MSMail-Priority: Low';
276 /* Insert headers from the $more_headers array */
277 if(count($more_headers))
279 reset($more_headers);
280 foreach ($more_headers as $k => $v)
282 $header[] = $k.': '.$v;
285 $cnt = count($header);
287 for ($i = 0 ; $i < $cnt ; $i++
)
289 $hdr_s .= foldLine($header[$i], 78, ' ');
292 $header .= $rn; /* One blank line to separate header and body */
298 * function for cleanly folding of headerlines
300 function foldLine($line, $length, $pre) {
301 $cnt = strlen($line);
305 $fold_string = $pre.' '."\r\n";
306 for ($i=0;$i<($cnt-$length);$i++
)
309 /* first try to fold at delimiters */
310 for ($j=($i+
$length); $j>$i; $j--)
328 /* not succeed yet so we try at spaces and = */
329 for ($j=($i+
$length); $j>$i; $j--)
348 /* clean folding didn't work */
349 $fold_pos = $i+
$length;
351 $line = substr_replace($line,$line{$fold_pos}.$fold_string,$fold_pos,1);
352 $cnt +
= strlen($fold_string);
353 $i = $j +
strlen($fold_string);
360 function mimeBoundary () {
361 static $mimeBoundaryString;
363 if ( !isset( $mimeBoundaryString ) ||
364 $mimeBoundaryString == '') {
365 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
366 mt_rand( 10000, 99999 );
369 return $mimeBoundaryString;
372 /* Time offset for correct timezone */
373 function timezone () {
376 $diff_second = date('Z');
378 $diff_second = - $diff_second;
380 if ($diff_second > 0) {
387 $diff_second = abs($diff_second);
389 $diff_hour = floor ($diff_second / 3600);
390 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
392 $zonename = '('.strftime('%Z').')';
393 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,