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.'"';
26 $reply_rfc822_header = (isset($message->reply_rfc822_header
)
27 ?
$message->reply_rfc822_header
: '');
28 $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header, $raw_length);
31 $this->preWriteToStream($header);
32 $this->writeToStream($stream, $header);
34 $this->writeBody($message, $stream, $raw_length, $boundary);
38 function writeBody($message, $stream, &$length_raw, $boundary='') {
39 if ($boundary && !$message->rfc822_header
) {
40 $s = '--'.$boundary."\r\n";
41 $s .= $this->prepareMIME_Header($message, $boundary);
42 $length_raw +
= strlen($s);
44 $this->preWriteToStream($s);
45 $this->writeToStream($stream, $s);
48 $this->writeBodyPart($message, $stream, $length_raw);
49 $boundary_depth = substr_count($message->entity_id
,'.');
50 if ($boundary_depth) {
51 $boundary .= '_part'.$boundary_depth;
54 for ($i=0, $entCount=count($message->entities
);$i<$entCount;$i++
) {
55 $msg = $this->writeBody($message->entities
[$i], $stream, $length_raw, $boundary);
56 if ($i == $entCount-1) $last = true;
58 if ($boundary && $last) {
59 $s = "\r\n--".$boundary."--\r\n\r\n";
60 $length_raw +
= strlen($s);
62 $this->preWriteToStream($s);
63 $this->writeToStream($stream, $s);
68 function writeBodyPart($message, $stream, &$length) {
69 if ($message->mime_header
) {
70 $type0 = $message->mime_header
->type0
;
72 $type0 = $message->rfc822_header
->content_type
->type0
;
77 if ($message->body_part
) {
78 $body_part = $message->body_part
;
79 $length +
= $this->clean_crlf($body_part);
81 $this->preWriteToStream($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 = fgets($file, 4096)) {
88 $length +
= $this->clean_crlf($tmp);
90 $this->preWriteToStream($tmp);
91 $this->writeToStream($stream, $tmp);
98 if ($message->body_part
) {
99 $body_part = $message->body_part
;
100 $length +
= $this->clean_crlf($body_part);
102 $this->writeToStream($stream, $body_part);
104 } elseif ($message->att_local_name
) {
105 $filename = $message->att_local_name
;
106 $file = fopen ($filename, 'rb');
107 while ($tmp = fread($file, 570)) {
108 $encoded = chunk_split(base64_encode($tmp));
111 $this->writeToStream($stream, $encoded);
118 $body_part_trailing = "\r\n";
119 $length +
= strlen($body_part_trailing);
121 $this->preWriteToStream($body_part_trailing);
122 $this->writeToStream($stream, $body_part_trailing);
126 function clean_crlf(&$s) {
127 $s = str_replace("\r\n", "\n", $s);
128 $s = str_replace("\r", "\n", $s);
129 $s = str_replace("\n", "\r\n", $s);
133 function preWriteToStream(&$s) {
136 function writeToStream($stream, $data) {
137 fputs($stream, $data);
140 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
148 function prepareMIME_Header($message, $boundary) {
149 $mime_header = $message->mime_header
;
153 $contenttype = 'Content-Type: '. $mime_header->type0
.'/'.
155 if (count($message->entities
)) {
156 $contenttype .= ";\r\n " . 'boundary="'.$boundary.'"';
158 if (isset($mime_header->parameters
['name'])) {
159 $contenttype .= '; name="'.
160 encodeHeader($mime_header->parameters
['name']). '"';
162 if (isset($mime_header->parameters
['charset'])) {
163 $charset = $mime_header->parameters
['charset'];
164 $contenttype .= '; charset="'.
165 encodeHeader($charset). '"';
169 $header[] = $contenttype . $rn;
170 if ($mime_header->description
) {
171 $header[] .= 'Content-Description: ' . $mime_header->description
. $rn;
173 if ($mime_header->encoding
) {
174 $encoding = $mime_header->encoding
;
175 $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding
. $rn;
177 if ($mime_header->type0
== 'text' ||
$mime_header->type0
== 'message') {
178 $header[] .= 'Content-Transfer-Encoding: 8bit' . $rn;
180 $header[] .= 'Content-Transfer-Encoding: base64' . $rn;
183 if ($mime_header->id
) {
184 $header[] .= 'Content-ID: ' . $mime_header->id
. $rn;
186 if ($mime_header->disposition
) {
187 $disposition = $mime_header->disposition
;
188 $contentdisp = 'Content-Disposition: ' . $disposition->name
;
189 if ($disposition->getProperty('filename')) {
190 $contentdisp .= '; filename="'.
191 encodeHeader($disposition->getProperty('filename')). '"';
193 $header[] = $contentdisp . $rn;
195 if ($mime_header->md5
) {
196 $header[] .= 'Content-MD5: ' . $mime_header->md5
. $rn;
198 if ($mime_header->language
) {
199 $header[] .= 'Content-Language: ' . $mime_header->language
. $rn;
202 $cnt = count($header);
204 for ($i = 0 ; $i < $cnt ; $i++
) {
205 $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
208 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
212 function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
213 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
214 global $version, $useSendmail, $username;
215 global $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
218 /* This creates an RFC 822 date */
219 $date = date("D, j M Y H:i:s ", mktime()) . $this->timezone();
220 /* Create a message-id */
221 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
222 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
223 /* Make an RFC822 Received: line */
224 if (isset($REMOTE_HOST)) {
225 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
227 $received_from = $REMOTE_ADDR;
229 if (isset($HTTP_VIA) ||
isset ($HTTP_X_FORWARDED_FOR)) {
230 if ($HTTP_X_FORWARDED_FOR == '') {
231 $HTTP_X_FORWARDED_FOR = 'unknown';
233 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
236 $header[] = "Received: from $received_from" . $rn;
237 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
238 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
239 $header[] = " $date" . $rn;
240 /* Insert the rest of the header fields */
241 $header[] = 'Message-ID: '. $message_id . $rn;
242 if ($reply_rfc822_header->message_id
) {
243 $header[] = 'In-Reply-To: '.$reply_rfc822_header->message_id
. $rn;
244 $references = $this->calculate_references($reply_rfc822_header);
245 $header[] = 'References: '.$references . $rn;
247 $header[] = "Date: $date" . $rn;
248 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject
) . $rn;
249 $header[] = 'From: '. encodeHeader($rfc822_header->getAddr_s('from')) . $rn;
250 /* RFC2822 if from contains more then 1 address */
251 if (count($rfc822_header->from
) > 1) {
252 $header[] = 'Sender: '. encodeHeader($rfc822_header->getAddr_s('sender')) . $rn;
254 if (count($rfc822_header->to
)) {
255 $header[] = 'To: '. encodeHeader($rfc822_header->getAddr_s('to')) . $rn;
257 if (count($rfc822_header->cc
)) {
258 $header[] = 'Cc: '. encodeHeader($rfc822_header->getAddr_s('cc')) . $rn;
260 if (count($rfc822_header->reply_to
)) {
261 $header[] = 'Reply-To: '. encodeHeader($rfc822_header->getAddr_s('reply_to')) . $rn;
263 /* Sendmail should return true. Default = false */
264 $bcc = $this->getBcc();
265 if (count($rfc822_header->bcc
)) {
266 $s = 'Bcc: '. encodeHeader($rfc822_header->getAddr_s('bcc')) . $rn;
268 $s = $this->foldLine($s, 78, str_pad('',4));
269 $raw_length +
= strlen($s);
274 /* Identify SquirrelMail */
275 $header[] = "X-Mailer: SquirrelMail (version $version)" . $rn;
276 /* Do the MIME-stuff */
277 $header[] = "MIME-Version: 1.0" . $rn;
278 $contenttype = 'Content-Type: '. $rfc822_header->content_type
->type0
.'/'.
279 $rfc822_header->content_type
->type1
;
280 if (count($rfc822_header->content_type
->properties
)) {
281 foreach ($rfc822_header->content_type
->properties
as $k => $v) {
283 $contenttype .= ';' .$k.'='.$v;
287 $header[] = $contenttype . $rn;
288 if ($rfc822_header->dnt
) {
289 $dnt = $rfc822_header->getAddr_s('dnt');
291 $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
293 $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
295 if ($rfc822_header->priority
) {
296 $prio = $rfc822_header->priority
;
297 $header[] = 'X-Priority: '.$prio. $rn;
299 case 1: $header[] = 'Importance: High'. $rn; break;
300 case 3: $header[] = 'Importance: Normal'. $rn; break;
301 case 5: $header[] = 'Importance: Low'. $rn; break;
305 /* Insert headers from the $more_headers array */
306 if(count($rfc822_header->more_headers
)) {
307 reset($rfc822_header->more_headers
);
308 foreach ($rfc822_header->more_headers
as $k => $v) {
309 $header[] = $k.': '.$v .$rn;
312 $cnt = count($header);
314 for ($i = 0 ; $i < $cnt ; $i++
) {
315 $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4));
318 $header .= $rn; /* One blank line to separate header and body */
319 $raw_length +
= strlen($header);
324 * function for cleanly folding of headerlines
326 function foldLine($line, $length, $pre='') {
327 $cnt = strlen($line);
329 if ($cnt > $length) {
330 $fold_string = "\r\n " . $pre;
331 $length -=strlen($fold_string);
332 for ($i=0;$i<($cnt-$length);$i++
) {
334 /* first try to fold at delimiters */
335 for ($j=($i+
$length); $j>$i; --$j) {
338 case (';'): $fold_pos = $i = $j; break;
342 if (!$fold_pos) { /* not succeed yet so we try at spaces & = */
343 for ($j=($i+
$length); $j>$i; $j--) {
346 case ('='): $fold_pos = $i = $j; break;
351 if (!$fold_pos) { /* clean folding didn't work */
352 $i = $j = $fold_pos = $i+
$length;
354 $line = substr_replace($line,$line{$fold_pos}.$fold_string,
356 $cnt +
= strlen($fold_string);
357 $i = $j +
strlen($fold_string);
364 function mimeBoundary () {
365 static $mimeBoundaryString;
367 if ( !isset( $mimeBoundaryString ) ||
368 $mimeBoundaryString == '') {
369 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
370 mt_rand( 10000, 99999 );
372 return $mimeBoundaryString;
375 /* Time offset for correct timezone */
376 function timezone () {
379 $diff_second = date('Z');
381 $diff_second = - $diff_second;
383 if ($diff_second > 0) {
388 $diff_second = abs($diff_second);
389 $diff_hour = floor ($diff_second / 3600);
390 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
391 $zonename = '('.strftime('%Z').')';
392 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
397 function calculate_references($hdr) {
398 $refer = $hdr->references
;
399 if (strlen($refer) > 2) {
400 $refer .= ' ' . $hdr->message_id
;
402 if ($hdr->in_reply_to
) {
403 $refer .= $hdr->in_reply_to
. ' ' . $hdr->message_id
;
405 $refer .= $hdr->message_id
;