6 * Copyright (c) 1999-2003 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 = "--".$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
;
75 $body_part_trailing = $last = '';
79 if ($message->body_part
) {
80 $body_part = $message->body_part
;
81 $length +
= $this->clean_crlf($body_part);
83 $this->preWriteToStream($body_part);
84 $this->writeToStream($stream, $body_part);
87 } elseif ($message->att_local_name
) {
88 $filename = $message->att_local_name
;
89 $file = fopen ($filename, 'rb');
90 while ($body_part = fgets($file, 4096)) {
91 $length +
= $this->clean_crlf($body_part);
93 $this->preWriteToStream($body_part);
94 $this->writeToStream($stream, $body_part);
102 if ($message->body_part
) {
103 $body_part = $message->body_part
;
104 $length +
= $this->clean_crlf($body_part);
106 $this->writeToStream($stream, $body_part);
108 } elseif ($message->att_local_name
) {
109 $filename = $message->att_local_name
;
110 $file = fopen ($filename, 'rb');
112 while ($tmp = fread($file, 570)) {
113 $body_part = chunk_split(base64_encode($tmp));
114 $length +
= $this->clean_crlf($body_part);
116 $this->writeToStream($stream, $body_part);
123 $body_part_trailing = '';
124 if ($last && substr($last,-1) != "\n") {
125 $body_part_trailing = "\r\n";
127 if ($body_part_trailing) {
128 $length +
= strlen($body_part_trailing);
130 $this->preWriteToStream($body_part_trailing);
131 $this->writeToStream($stream, $body_part_trailing);
136 function clean_crlf(&$s) {
137 $s = str_replace("\r\n", "\n", $s);
138 $s = str_replace("\r", "\n", $s);
139 $s = str_replace("\n", "\r\n", $s);
143 function strip_crlf(&$s) {
144 $s = str_replace("\r\n ", '', $s);
145 $s = str_replace("\r", '', $s);
146 $s = str_replace("\n", '', $s);
149 function preWriteToStream(&$s) {
152 function writeToStream($stream, $data) {
153 fputs($stream, $data);
156 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
164 function prepareMIME_Header($message, $boundary) {
165 $mime_header = $message->mime_header
;
169 $contenttype = 'Content-Type: '. $mime_header->type0
.'/'.
171 if (count($message->entities
)) {
172 $contenttype .= ";\r\n " . 'boundary="'.$boundary.'"';
174 if (isset($mime_header->parameters
['name'])) {
175 $contenttype .= '; name="'.
176 encodeHeader($mime_header->parameters
['name']). '"';
178 if (isset($mime_header->parameters
['charset'])) {
179 $charset = $mime_header->parameters
['charset'];
180 $contenttype .= '; charset="'.
181 encodeHeader($charset). '"';
185 $header[] = $contenttype . $rn;
186 if ($mime_header->description
) {
187 $header[] .= 'Content-Description: ' . $mime_header->description
. $rn;
189 if ($mime_header->encoding
) {
190 $encoding = $mime_header->encoding
;
191 $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding
. $rn;
193 if ($mime_header->type0
== 'text' ||
$mime_header->type0
== 'message') {
194 $header[] .= 'Content-Transfer-Encoding: 8bit' . $rn;
196 $header[] .= 'Content-Transfer-Encoding: base64' . $rn;
199 if ($mime_header->id
) {
200 $header[] .= 'Content-ID: ' . $mime_header->id
. $rn;
202 if ($mime_header->disposition
) {
203 $disposition = $mime_header->disposition
;
204 $contentdisp = 'Content-Disposition: ' . $disposition->name
;
205 if ($disposition->getProperty('filename')) {
206 $contentdisp .= '; filename="'.
207 encodeHeader($disposition->getProperty('filename')). '"';
209 $header[] = $contentdisp . $rn;
211 if ($mime_header->md5
) {
212 $header[] .= 'Content-MD5: ' . $mime_header->md5
. $rn;
214 if ($mime_header->language
) {
215 $header[] .= 'Content-Language: ' . $mime_header->language
. $rn;
218 $cnt = count($header);
220 for ($i = 0 ; $i < $cnt ; $i++
) {
221 $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
224 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
228 function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
229 global $domain, $version, $username;
231 /* if server var SERVER_NAME not available, use $domain */
232 if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER
)) {
233 $SERVER_NAME = $domain;
236 sqGetGlobalVar('REMOTE_ADDR', $REMOTE_ADDR, SQ_SERVER
);
237 sqGetGlobalVar('REMOTE_PORT', $REMOTE_PORT, SQ_SERVER
);
238 sqGetGlobalVar('REMOTE_HOST', $REMOTE_HOST, SQ_SERVER
);
239 sqGetGlobalVar('HTTP_VIA', $HTTP_VIA, SQ_SERVER
);
240 sqGetGlobalVar('HTTP_X_FORWARDED_FOR', $HTTP_X_FORWARDED_FOR, SQ_SERVER
);
244 /* This creates an RFC 822 date */
245 $date = date('D, j M Y H:i:s ', mktime()) . $this->timezone();
246 /* Create a message-id */
247 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
248 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
249 /* Make an RFC822 Received: line */
250 if (isset($REMOTE_HOST)) {
251 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
253 $received_from = $REMOTE_ADDR;
255 if (isset($HTTP_VIA) ||
isset ($HTTP_X_FORWARDED_FOR)) {
256 if (!isset($HTTP_X_FORWARDED_FOR) ||
$HTTP_X_FORWARDED_FOR == '') {
257 $HTTP_X_FORWARDED_FOR = 'unknown';
259 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
262 $header[] = "Received: from $received_from" . $rn;
263 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
264 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
265 $header[] = " $date" . $rn;
266 /* Insert the rest of the header fields */
267 $header[] = 'Message-ID: '. $message_id . $rn;
268 if ($reply_rfc822_header->message_id
) {
269 $rep_message_id = $reply_rfc822_header->message_id
;
270 // $this->strip_crlf($message_id);
271 $header[] = 'In-Reply-To: '.$rep_message_id . $rn;
272 $references = $this->calculate_references($reply_rfc822_header);
273 $header[] = 'References: '.$references . $rn;
275 $header[] = "Date: $date" . $rn;
276 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject
) . $rn;
277 $header[] = 'From: '. $rfc822_header->getAddr_s('from',',',true) . $rn;
278 /* RFC2822 if from contains more then 1 address */
279 if (count($rfc822_header->from
) > 1) {
280 $header[] = 'Sender: '. $rfc822_header->getAddr_s('sender',',',true) . $rn;
282 if (count($rfc822_header->to
)) {
283 $header[] = 'To: '. $rfc822_header->getAddr_s('to',',',true) . $rn;
285 if (count($rfc822_header->cc
)) {
286 $header[] = 'Cc: '. $rfc822_header->getAddr_s('cc',',',true) . $rn;
288 if (count($rfc822_header->reply_to
)) {
289 $header[] = 'Reply-To: '. $rfc822_header->getAddr_s('reply_to',',',true) . $rn;
291 /* Sendmail should return true. Default = false */
292 $bcc = $this->getBcc();
293 if (count($rfc822_header->bcc
)) {
294 $s = 'Bcc: '. $rfc822_header->getAddr_s('bcc',',',true) . $rn;
296 $s = $this->foldLine($s, 78, str_pad('',4));
297 $raw_length +
= strlen($s);
302 /* Identify SquirrelMail */
303 $header[] = 'User-Agent: SquirrelMail/' . $version . $rn;
304 /* Do the MIME-stuff */
305 $header[] = 'MIME-Version: 1.0' . $rn;
306 $contenttype = 'Content-Type: '. $rfc822_header->content_type
->type0
.'/'.
307 $rfc822_header->content_type
->type1
;
308 if (count($rfc822_header->content_type
->properties
)) {
309 foreach ($rfc822_header->content_type
->properties
as $k => $v) {
311 $contenttype .= ';' .$k.'='.$v;
315 $header[] = $contenttype . $rn;
316 if ($rfc822_header->dnt
) {
317 $dnt = $rfc822_header->getAddr_s('dnt');
319 $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
321 $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
323 if ($rfc822_header->priority
) {
324 $prio = $rfc822_header->priority
;
325 $header[] = 'X-Priority: '.$prio. $rn;
327 case 1: $header[] = 'Importance: High'. $rn; break;
328 case 3: $header[] = 'Importance: Normal'. $rn; break;
329 case 5: $header[] = 'Importance: Low'. $rn; break;
333 /* Insert headers from the $more_headers array */
334 if(count($rfc822_header->more_headers
)) {
335 reset($rfc822_header->more_headers
);
336 foreach ($rfc822_header->more_headers
as $k => $v) {
337 $header[] = $k.': '.$v .$rn;
340 $cnt = count($header);
342 for ($i = 0 ; $i < $cnt ; $i++
) {
343 $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4));
345 // $debug = "Debug: <123456789012345678901234567890123456789012345678901234567890123456789>\r\n";
346 // $this->foldLine($debug, 78, str_pad('',4));
348 $header .= $rn; /* One blank line to separate header and body */
349 $raw_length +
= strlen($header);
354 * function for cleanly folding of headerlines
356 function foldLine($line, $length, $pre='') {
357 $line = substr($line,0, -2);
358 $length -= 2; /* do not fold between \r and \n */
359 $cnt = strlen($line);
360 if ($cnt > $length) { /* try folding */
361 $fold_string = "\r\n " . $pre;
363 $aFoldLine = array();
364 while (strlen($line) > $length) {
366 /* handle encoded parts */
367 if (preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',$line,$regs)) {
368 $fold_tmp = $regs[1];
369 $iPosEnc = strpos($line,$fold_tmp);
370 $iLengthEnc = strlen($fold_tmp);
371 if ($iPosEnc < $length && (($iPosEnc +
$iLengthEnc) > $length)) {
373 /* fold just before the start of the encoded string */
374 $aFoldLine[] = substr($line,0,$iPosEnc);
375 $line = substr($line,$iPosEnc);
378 $length -= strlen($fold_string);
380 if ($iLengthEnc > $length) { /* place the encoded
381 string on a separate line and do not fold inside it*/
382 /* minimize foldstring */
383 $fold_string = "\r\n ";
384 $aFoldLine[] = substr($line,0,$iLengthEnc);
385 $line = substr($line,$iLengthEnc);
387 } else { /* the encoded string fits into the foldlength */
389 $iPosEncEnd = $iPosEnc+
$iLengthEnc;
390 $sLineRem = substr($line,$iPosEncEnd,$length - $iPosEncEnd);
391 if (preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',$slineRem) ||
!preg_match('/[=,;\s]/',$sLineRem)) {
392 /*impossible to fold clean in the next part -> fold after the enc string */
393 $aFoldLine[] = substr($line,0,$iPosEncEnd+
1);
394 $line = substr($line,$iPosEncEnd+
1);
398 $length -= strlen($fold_string);
404 $line_tmp = substr($line,0,$length);
406 /* try to fold at logical places */
409 case ($iFoldPos = strrpos($line_tmp,',')): break;
410 case ($iFoldPos = strrpos($line_tmp,';')): break;
411 case ($iFoldPos = strrpos($line_tmp,' ')): break;
412 case ($iFoldPos = strrpos($line_tmp,'=')): break;
416 if (!$iFoldPos) { /* clean folding didn't work */
419 $aFoldLine[] = substr($line,0,$iFoldPos+
1);
420 $line = substr($line,$iFoldPos+
1);
423 $length -= strlen($fold_string);
427 /*$reconstruct the line */
429 $aFoldLine[] = $line;
431 $line = implode($fold_string,$aFoldLine);
437 function mimeBoundary () {
438 static $mimeBoundaryString;
440 if ( !isset( $mimeBoundaryString ) ||
441 $mimeBoundaryString == '') {
442 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
443 mt_rand( 10000, 99999 );
445 return $mimeBoundaryString;
448 /* Time offset for correct timezone */
449 function timezone () {
452 $diff_second = date('Z');
454 $diff_second = - $diff_second;
456 if ($diff_second > 0) {
461 $diff_second = abs($diff_second);
462 $diff_hour = floor ($diff_second / 3600);
463 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
464 $zonename = '('.strftime('%Z').')';
465 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
470 function calculate_references($hdr) {
471 $refer = $hdr->references
;
472 $message_id = $hdr->message_id
;
473 $in_reply_to = $hdr->in_reply_to
;
474 if (strlen($refer) > 2) {
475 $refer .= ' ' . $message_id;
478 $refer .= $in_reply_to . ' ' . $message_id;
480 $refer .= $message_id;