85a7395e5052272f843195cc91db4ee215a1a269
5 function mail($message, $stream) {
7 $rfc822_header = $message->rfc822_header
;
8 if (count($message->entities
)) {
9 $boundary = mimeBoundary();
10 $rfc822_header->contenttype
->properties
['boundary']=$boundary;
14 $header = prepareRFC822_Header($rfc822_header);
15 $raw_length = strlen($header);
17 $this->preWriteToStream($s);
18 $this->writeToStream($stream, $s);
20 writeBody($message, $stream, $length_raw, $boundary);
24 function writeBody($message, $stream, &$length_raw, $boundary='') {
26 $s = '--'.$boundary."\r\n";
27 $s .= $this->prepareMIME_Header($message, $boundary);
28 $length_raw +
= strlen($s);
30 $this->preWriteToStream($s);
31 $this->writeToStream($stream, $s);
34 writeBodyPart($message, $stream, $length_raw);
35 $boundary_depth = substr_count($message->entity_id
,'.');
36 if ($boundary_depth) {
37 $boundary .= '_part'.$boundary_depth;
39 for ($i=0, $entCount=count($message->entities
);$i<$entCount;$i++
) {
40 $msg = writeBody($message->entities
[$i], $stream, $length_raw, $boundary);
43 $s = '--'.$boundary."--\r\n";
44 $length_raw +
= strlen($s);
46 $this->preWriteToStream($s);
47 $this->writeToStream($stream, $s);
52 function writeBodyPart($message, $stream, &$length) {
54 switch ($message->type0
) {
57 if ($message->body_part
) {
58 $body_part = $message->body_part
;
59 $length +
= $this->clean_crlf($body_part);
61 $this->preWriteToStream($body_part);
62 $this->writeToStream($stream, $body_part)
64 } elseif ($message->att_local_name
) {
65 $filename = $message->att_local_name
;
66 $file = fopen ($filename, 'rb');
67 while ($tmp = fgets($file, 4096)) {
68 $length +
= $this->clean_crlf($tmp);
70 $this->preWriteToStream($tmp);
71 $this->writeToStream($stream, $tmp);
78 if ($message->body_part
) {
79 $body_part = $message->body_part
;
80 $length +
= $this->clean_crlf($body_part);
82 $this->writeToStream($stream, $body_part)
84 } elseif ($message->att_local_name
) {
85 $filename = $message->att_local_name
;
86 $file = fopen ($filename, 'rb');
87 while ($tmp = fread($file, 1520)) {
88 $encoded = chunk_split(base64_encode($tmp));
89 $length +
= strlen($encoded);
91 $this->writeToStream($stream, $encoded);
100 function clean_crlf($s) {
101 $s = str_replace("\r\n", "\n", $s);
102 $s = str_replace("\r", "\n", $s);
103 $s = str_replace("\n", "\r\n", $s);
107 function preWriteToStream($
&s
) {
110 function writeToStream($stream, $data) {
113 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
117 function prepareMIME_Header($message, $boundary) {
118 $mime_header = $message->header
;
122 $contenttype = 'Content-Type: '. $mime_header->contenttype
->type0
.'/'.
123 $mime_header->contenttype
->type1
;
124 if (count($message->entities
)) {
125 $contenttype .= ";\r\n " . 'boundary="'.$boundary.'"';
127 if (isset($mime_header->parameters
['name'])) {
128 $contenttype .= ";\r\n " . 'name="'.
129 encodeHeader($mime_header->parameters
['name']). '"';
131 $header[] = $contenttype . $rn;
132 if ($mime_header->description
) {
133 $header[] .= 'Content-Description: ' . $mime_header->description
. $rn;
135 if ($mime_header->encoding
) {
136 $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding
. $rn;
138 if ($mime_header->id
) {
139 $header[] .= 'Content-ID: ' . $mime_header->id
. $rn;
141 if ($mime_header->disposition
) {
142 $contentdisp .= 'Content-Disposition: ' . $mime_header->disposition
;
143 if (isset($mime_header->parameters
['filename'])) {
144 $contentdisp .= ";\r\n " . 'filename="'.
145 encodeHeader($mime_header->parameters
['filename']). '"';
147 $header[] = $contentdisp . $rn;
149 if ($mime_header->md5
) {
150 $header[] .= 'Content-MD5: ' . $mime_header->md5
. $rn;
152 if ($mime_header->language
) {
153 $header[] .= 'Content-Language: ' . $mime_header->language
. $rn;
156 $cnt = count($header);
158 for ($i = 0 ; $i < $cnt ; $i++
) {
159 $hdr_s .= foldLine($header[$i], 78, ' ');
162 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
166 function prepareRFC822_Header($rfc822_header) {
167 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
168 global $version, $useSendmail, $username;
169 global $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
172 /* This creates an RFC 822 date */
173 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
174 /* Create a message-id */
175 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
176 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
177 /* Make an RFC822 Received: line */
178 if (isset($REMOTE_HOST))
180 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
184 $received_from = $REMOTE_ADDR;
186 if (isset($HTTP_VIA) ||
isset ($HTTP_X_FORWARDED_FOR)) {
187 if ($HTTP_X_FORWARDED_FOR == '') {
188 $HTTP_X_FORWARDED_FOR = 'unknown';
190 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
193 $header[] = "Received: from $received_from" . $rn;
194 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
195 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
196 $header[] = " $date" . $rn;
197 /* Insert the rest of the header fields */
198 $header[] = "Message-ID: $message_id" . $rn;
199 $header[] = "Date: $date" . $rn;
200 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject
) . $rn;
201 $header[] = 'From: '. encodeHeader($rfc822_header->getAddr_s('from')) . $rn;
202 if (count($rfc822_header->from
) > 1) /* RFC2822 if from contains
203 more then 1 address */
205 $header[] = 'Sender: '. encodeHeader($rfc822_header->getAddr_s('sender')) . $rn;
207 $header[] = 'To: '. encodeHeader($rfc822_header->getAddr_s('to')) . $rn; // Who it's TO
208 if (count($rfc_header->cc
))
210 $header[] = 'Cc: '. encodeHeader($rfc822_header->getAddr_s('cc')) . $rn;
212 if (count($rfc822_header->$reply_to))
214 $header[] = 'Reply-To: '. encodeHeader($rfc822_header->getAddr_s('reply_to')) . $rn;
216 if (count($rfc_header->bcc
) && $useSendmail)
218 $header[] = 'Bcc: '. encodeHeader($rfc822_header->getAddr_s('bcc')) . $rn;
220 /* Identify SquirrelMail */
221 $header[] = "X-Mailer: SquirrelMail (version $version)" . $rn;
222 /* Do the MIME-stuff */
223 $header[] = "MIME-Version: 1.0" . $rn;
224 $contenttype = 'Content-Type: '. $rfc822_header->contenttype
->type0
.'/'.
225 $rfc822_header->contenttype
->type1
;
226 if (count($rfc822_header->contenttype
->properties
))
228 foreach ($rfc822_header->contenttype
->properties
as $k => $v)
230 $contenttype .= ';'. "\r\n " .$k.'='.$v; /* FOLDING */
233 $header[] = $contenttype . $rn;
234 if ($rfc822_header->dnt
)
236 $dnt = $rfc822_header->getAddr_s('dnt');
238 $header[] = 'X-Confirm-Reading-To: '.$dnt;
240 $header[] = 'Disposition-Notification-To: '.$dnt;
242 if ($rfc822_header->priority
)
244 $prio = $rfc822_header->priority
;
245 $header[] = 'X-Priority: '.$prio;
249 $header[] = 'Importance: High';
250 $header[] = 'X-MSMail-Priority: High';
253 $header[] = 'Importance: Normal';
254 $header[] = 'X-MSMail-Priority: Normal';
257 $header[] = 'Importance: Low';
258 $header[] = 'X-MSMail-Priority: Low';
264 /* Insert headers from the $more_headers array */
265 if(count($more_headers))
267 reset($more_headers);
268 foreach ($more_headers as $k => $v)
270 $header[] = $k.': '.$v;
273 $cnt = count($header);
275 for ($i = 0 ; $i < $cnt ; $i++
)
277 $hdr_s .= foldLine($header[$i], 78, ' ');
280 $header .= $rn; /* One blank line to separate header and body */
286 * function for cleanly folding of headerlines
288 function foldLine($line, $length, $pre) {
289 $cnt = strlen($line);
293 $fold_string = $pre.' '."\r\n";
294 for ($i=0;$i<($cnt-$length);$i++
)
297 /* first try to fold at delimiters */
298 for ($j=($i+
$length); $j>$i; $j--)
316 /* not succeed yet so we try at spaces and = */
317 for ($j=($i+
$length); $j>$i; $j--)
336 /* clean folding didn't work */
337 $fold_pos = $i+
$length;
339 $line = substr_replace($line,$line{$fold_pos}.$fold_string,$fold_pos,1);
340 $cnt +
= strlen($fold_string);
341 $i = $j +
strlen($fold_string);
348 function mimeBoundary () {
349 static $mimeBoundaryString;
351 if ( !isset( $mimeBoundaryString ) ||
352 $mimeBoundaryString == '') {
353 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
354 mt_rand( 10000, 99999 );
357 return $mimeBoundaryString;
360 /* Time offset for correct timezone */
361 function timezone () {
364 $diff_second = date('Z');
366 $diff_second = - $diff_second;
368 if ($diff_second > 0) {
375 $diff_second = abs($diff_second);
377 $diff_hour = floor ($diff_second / 3600);
378 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
380 $zonename = '('.strftime('%Z').')';
381 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,