c05cbc52f175408bd1d9c0c1bddd80c5c03ae4c4
[squirrelmail.git] / src / compose.php
1 <?php
2
3 /**
4 * compose.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code sends a mail.
10 *
11 * There are 4 modes of operation:
12 * - Start new mail
13 * - Add an attachment
14 * - Send mail
15 * - Save As Draft
16 *
17 * $Id$
18 */
19
20 /* Path for SquirrelMail required files. */
21 define('SM_PATH','../');
22
23 /* SquirrelMail required files. */
24 require_once(SM_PATH . 'include/validate.php');
25 require_once(SM_PATH . 'functions/imap.php');
26 require_once(SM_PATH . 'functions/date.php');
27 require_once(SM_PATH . 'functions/mime.php');
28 require_once(SM_PATH . 'functions/plugin.php');
29 require_once(SM_PATH . 'functions/display_messages.php');
30 require_once(SM_PATH . 'class/deliver/Deliver.class.php');
31
32 /* --------------------- Get globals ------------------------------------- */
33 $username = $_SESSION['username'];
34 $onetimepad = $_SESSION['onetimepad'];
35 $base_uri = $_SESSION['base_uri'];
36 $delimiter = $_SESSION['delimiter'];
37
38 if (isset($_POST['return'])) {
39 $html_addr_search_done = 'Use Addresses';
40 }
41 if ( isset($_SESSION['composesession']) ) {
42 $composesession = $_SESSION['composesession'];
43 }
44 sqextractGlobalVar('action');
45 sqextractGlobalVar('session');
46 sqextractGlobalVar('mailbox');
47 sqextractGlobalVar('identity');
48 sqextractGlobalVar('send_to');
49 sqextractGlobalVar('send_to_cc');
50 sqextractGlobalVar('send_to_bcc');
51 sqextractGlobalVar('subject');
52 sqextractGlobalVar('body');
53 sqextractGlobalVar('mailprio');
54 sqextractGlobalVar('request_mdn');
55 sqextractGlobalVar('request_dr');
56 sqextractGlobalVar('html_addr_search');
57 sqextractGlobalVar('mail_sent');
58 sqextractGlobalVar('passed_id');
59 sqextractGlobalVar('passed_ent_id');
60 sqextractGlobalVar('send');
61
62 if ( isset($_POST['sigappend']) ) {
63 $sigappend = $_POST['sigappend'];
64 }
65 /* From addressbook search */
66 if ( isset($_POST['from_htmladdr_search']) ) {
67 $from_htmladdr_search = $_POST['from_htmladdr_search'];
68 }
69 if ( isset($_POST['addr_search_done']) ) {
70 $html_addr_search_done = $_POST['addr_search_done'];
71 }
72 if ( isset($_POST['send_to_search']) ) {
73 $send_to_search = &$_POST['send_to_search'];
74 }
75
76 /* Attachments */
77 sqextractGlobalVar('attach');
78 if ( isset($_POST['do_delete']) ) {
79 $do_delete = $_POST['do_delete'];
80 }
81 if ( isset($_POST['delete']) ) {
82 $delete = &$_POST['delete'];
83 }
84 if ( isset($_SESSION['compose_messages']) ) {
85 $compose_messages = &$_SESSION['compose_messages'];
86 }
87
88
89 /* Forward message as attachment */
90 if ( isset($_GET['attachedmessages']) ) {
91 $attachedmessages = $_GET['attachedmessages'];
92 }
93
94 /* Drafts */
95 sqextractGlobalVar('draft');
96 sqextractGlobalVar('draft_id');
97 sqextractGlobalVar('ent_num');
98 sqextractGlobalVar('saved_draft');
99 sqextractGlobalVar('delete_draft');
100
101 $key = $_COOKIE['key'];
102
103 /* --------------------- Specific Functions ------------------------------ */
104
105 function replyAllString($header) {
106 global $include_self_reply_all, $username, $data_dir;
107 $excl_arr = array();
108 /**
109 * 1) Remove the addresses we'll be sending the message 'to'
110 */
111 $url_replytoall_avoid_addrs = '';
112 if (isset($header->replyto)) {
113 $excl_ar = $header->getAddr_a('replyto');
114 }
115 /**
116 * 2) Remove our identities from the CC list (they still can be in the
117 * TO list) only if $include_self_reply_all is turned off
118 */
119 if (!$include_self_reply_all) {
120 $email_address = trim(getPref($data_dir, $username, 'email_address'));
121 $excl_ar[$email_address] = '';
122
123 $idents = getPref($data_dir, $username, 'identities');
124 if ($idents != '' && $idents > 1) {
125 for ($i = 1; $i < $idents; $i ++) {
126 $cur_email_address = getPref($data_dir, $username,
127 'email_address' . $i);
128 $cur_email_address = strtolower($cur_email_address);
129 $excl_ar[$cur_email_address] = '';
130 }
131 }
132 }
133
134 /**
135 * 3) get the addresses.
136 */
137 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
138
139 /**
140 * 4) generate the string.
141 */
142 $url_replytoallcc = '';
143 foreach( $url_replytoall_ar as $email => $personal) {
144 if ($personal) {
145 $url_replytoallcc .= ", \"$personal\" <$email>";
146 } else {
147 $url_replytoallcc .= ', '. $email;
148 }
149 }
150 $url_replytoallcc = substr($url_replytoallcc,2);
151 return $url_replytoallcc;
152 }
153
154 function getforwardHeader($orig_header) {
155 global $editor_size;
156
157 $display = array( _("Subject") => strlen(_("Subject")),
158 _("From") => strlen(_("From")),
159 _("Date") => strlen(_("Date")),
160 _("To") => strlen(_("To")),
161 _("Cc") => strlen(_("Cc")) );
162 $maxsize = max($display);
163 $indent = str_pad('',$maxsize+2);
164 foreach($display as $key => $val) {
165 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
166 }
167 $bodyTop = str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH) .
168 "\n". $display[_("Subject")] .
169 decodeHeader($orig_header->subject) . "\n" .
170 $display[_("From")] .
171 decodeHeader($orig_header->getAddr_s('from',"\n$indent")) . "\n" .
172 $display[_("Date")] .
173 getLongDateString( $orig_header->date ). "\n" .
174 $display[_("To")] .
175 decodeHeader($orig_header->getAddr_s('to',"\n$indent")) ."\n";
176 if ($orig_header->cc != array() && $orig_header->cc !='') {
177 $bodyTop .= $display[_("Cc")] . decodeHeader($orig_header->getAddr_s('cc',"\n$indent")) . "\n";
178 }
179 $bodyTop .= str_pad('', $editor_size -2 , '-') .
180 "\n";
181 return $bodyTop;
182 }
183 /* ----------------------------------------------------------------------- */
184
185 /* OLD: should be adapted to composeMessage */
186
187 /*
188 * If the session is expired during a post this restores the compose session
189 * vars.
190 */
191 //$session_expired = false;
192 sqsession_unregister('session_expired_post');
193 if (false && sqsession_is_registered('session_expired_post')) {
194 global $session_expired_post, $session_expired;
195 /*
196 * extra check for username so we don't display previous post data from
197 * another user during this session.
198 */
199 if ($session_expired_post['username'] != $username) {
200 sqsession_unregister('session_expired_post');
201 sqsession_unregister('session_expired');
202 } else {
203 foreach ($session_expired_post as $postvar => $val) {
204 if (isset($val)) {
205 $$postvar = $val;
206 } else {
207 $$postvar = '';
208 }
209 }
210 if (isset($send)) {
211 unset($send);
212 }
213 $session_expired = true;
214 }
215 sqsession_unregister('session_expired_post');
216 sqsession_unregister('session_expired');
217 if (!isset($mailbox)) {
218 $mailbox = '';
219 }
220
221 if ($compose_new_win == '1') {
222 compose_Header($color, $mailbox);
223 } else {
224 displayPageHeader($color, $mailbox);
225 }
226 showInputForm($session, false);
227 exit();
228 }
229 if (!isset($composesession)) {
230 $composesession = 0;
231 sqsession_register(0,'composesession');
232 }
233
234 if (!isset($session) || (isset($newmessage) && $newmessage)) {
235 sqsession_unregister('composesession');
236 $session = "$composesession" +1;
237 $composesession = $session;
238 sqsession_register($composesession,'composesession');
239 }
240
241 if (!isset($compose_messages)) {
242 $compose_messages = array();
243 }
244 if (!isset($compose_messages[$session]) || ($compose_messages[$session] == NULL)) {
245 /* if (!array_key_exists($session, $compose_messages)) { /* We can only do this in PHP >= 4.1 */
246 $composeMessage = new Message();
247 $rfc822_header = new Rfc822Header();
248 $composeMessage->rfc822_header = $rfc822_header;
249 $composeMessage->reply_rfc822_header = '';
250 $compose_messages[$session] = $composeMessage;
251 sqsession_register($compose_messages,'compose_messages');
252 } else {
253 $composeMessage=$compose_messages[$session];
254 }
255
256 if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) {
257 $mailbox = 'INBOX';
258 }
259
260 if ($draft) {
261 /*
262 * Set $default_charset to correspond with the user's selection
263 * of language interface.
264 */
265 set_my_charset();
266 $composeMessage=$compose_messages[$session];
267 if (! sendMessage($composeMessage, true)) {
268 showInputForm($session);
269 exit();
270 } else {
271 $draft_message = _("Draft Email Saved");
272 /* If this is a resumed draft, then delete the original */
273 if(isset($delete_draft)) {
274 Header("Location: delete_message.php?mailbox=" . urlencode($draft_folder) .
275 "&message=$delete_draft&sort=$sort&startMessage=1&saved_draft=yes");
276 exit();
277 }
278 else {
279 if ($compose_new_win == '1') {
280 Header("Location: compose.php?saved_draft=yes&session=$composesession");
281 exit();
282 }
283 else {
284 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
285 "&startMessage=1&note=".urlencode($draft_message));
286 exit();
287 }
288 }
289 }
290 }
291
292 if ($send) {
293 if (isset($_FILES['attachfile']) &&
294 $_FILES['attachfile']['tmp_name'] &&
295 $_FILES['attachfile']['tmp_name'] != 'none') {
296 $AttachFailure = saveAttachedFiles($session);
297 }
298 if (checkInput(false) && !isset($AttachFailure)) {
299 $urlMailbox = urlencode (trim($mailbox));
300 if (! isset($passed_id)) {
301 $passed_id = 0;
302 }
303 /*
304 * Set $default_charset to correspond with the user's selection
305 * of language interface.
306 */
307 set_my_charset();
308 /*
309 * This is to change all newlines to \n
310 * We'll change them to \r\n later (in the sendMessage function)
311 */
312 $body = str_replace("\r\n", "\n", $body);
313 $body = str_replace("\r", "\n", $body);
314
315 /*
316 * Rewrap $body so that no line is bigger than $editor_size
317 * This should only really kick in the sqWordWrap function
318 * if the browser doesn't support "VIRTUAL" as the wrap type.
319 */
320 $body = explode("\n", $body);
321 $newBody = '';
322 foreach ($body as $line) {
323 if( $line <> '-- ' ) {
324 $line = rtrim($line);
325 }
326 if (strlen($line) <= $editor_size + 1) {
327 $newBody .= $line . "\n";
328 } else {
329 sqWordWrap($line, $editor_size);
330 $newBody .= $line . "\n";
331 }
332 }
333 $body = $newBody;
334 do_hook('compose_send');
335 $composeMessage=$compose_messages[$session];
336
337 $Result = sendMessage($composeMessage);
338 if (! $Result) {
339 showInputForm($session);
340 exit();
341 }
342 if ( isset($delete_draft)) {
343 Header("Location: delete_message.php?mailbox=" . urlencode( $draft_folder ).
344 "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes");
345 exit();
346 }
347 if ($compose_new_win == '1') {
348 Header("Location: compose.php?mail_sent=yes");
349 }
350 else {
351 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
352 "&startMessage=1");
353 }
354 } else {
355 if ($compose_new_win == '1') {
356 compose_Header($color, $mailbox);
357 }
358 else {
359 displayPageHeader($color, $mailbox);
360 }
361 if (isset($AttachFailure)) {
362 plain_error_message(_("Could not move/copy file. File not attached"),
363 $color);
364 }
365 checkInput(true);
366 showInputForm($session);
367 /* sqimap_logout($imapConnection); */
368 }
369 } elseif (isset($html_addr_search_done)) {
370 if ($compose_new_win == '1') {
371 compose_Header($color, $mailbox);
372 }
373 else {
374 displayPageHeader($color, $mailbox);
375 }
376
377 if (isset($send_to_search) && is_array($send_to_search)) {
378 foreach ($send_to_search as $k => $v) {
379 if (substr($k, 0, 1) == 'T') {
380 if ($send_to) {
381 $send_to .= ', ';
382 }
383 $send_to .= $v;
384 }
385 elseif (substr($k, 0, 1) == 'C') {
386 if ($send_to_cc) {
387 $send_to_cc .= ', ';
388 }
389 $send_to_cc .= $v;
390 }
391 elseif (substr($k, 0, 1) == 'B') {
392 if ($send_to_bcc) {
393 $send_to_bcc .= ', ';
394 }
395 $send_to_bcc .= $v;
396 }
397 }
398 }
399 showInputForm($session);
400 } elseif (isset($html_addr_search)) {
401 if (isset($_FILES['attachfile']) &&
402 $_FILES['attachfile']['tmp_name'] &&
403 $_FILES['attachfile']['tmp_name'] != 'none') {
404 if(saveAttachedFiles($session)) {
405 plain_error_message(_("Could not move/copy file. File not attached"), $color);
406 }
407 }
408 /*
409 * I am using an include so as to elminiate an extra unnecessary
410 * click. If you can think of a better way, please implement it.
411 */
412 include_once('./addrbook_search_html.php');
413 } elseif (isset($attach)) {
414 if (saveAttachedFiles($session)) {
415 plain_error_message(_("Could not move/copy file. File not attached"), $color);
416 }
417 if ($compose_new_win == '1') {
418 compose_Header($color, $mailbox);
419 }
420 else {
421 displayPageHeader($color, $mailbox);
422 }
423 showInputForm($session);
424 }
425 elseif (isset($sigappend)) {
426 $idents = getPref($data_dir, $username, 'identities', 0);
427 if ($idents > 1) {
428 if ($identity == 'default') {
429 $no = 'g';
430 } else {
431 $no = $identity;
432 }
433 $signature = getSig($data_dir, $username, $no);
434 }
435 $body .= "\n\n".($prefix_sig==true? "-- \n":'').$signature;
436 if ($compose_new_win == '1') {
437 compose_Header($color, $mailbox);
438 } else {
439 displayPageHeader($color, $mailbox);
440 }
441 showInputForm($session);
442 } elseif (isset($do_delete)) {
443 if ($compose_new_win == '1') {
444 compose_Header($color, $mailbox);
445 }
446 else {
447 displayPageHeader($color, $mailbox);
448 }
449
450 if (isset($delete) && is_array($delete)) {
451 $composeMessage = $compose_messages[$session];
452 foreach($delete as $index) {
453 $attached_file = $composeMessage->entities[$index]->att_local_name;
454 unlink ($attached_file);
455 unset ($composeMessage->entities[$index]);
456 }
457 $new_entities = array();
458 foreach ($composeMessage->entities as $entity) {
459 $new_entities[] = $entity;
460 }
461 $composeMessage->entities = $new_entities;
462 $compose_messages[$session] = $composeMessage;
463 sqsession_register($compose_messages, 'compose_messages');
464 // setPref($data_dir, $username, 'attachments', serialize($attachments));
465 }
466 showInputForm($session);
467 } else {
468 /*
469 * This handles the default case as well as the error case
470 * (they had the same code) --> if (isset($smtpErrors))
471 */
472
473 if ($compose_new_win == '1') {
474 compose_Header($color, $mailbox);
475 } else {
476 displayPageHeader($color, $mailbox);
477 }
478
479 $newmail = true;
480
481 if (!isset($passed_ent_id)) {
482 $passed_ent_id = '';
483 }
484 if (!isset($passed_id)) {
485 $passed_id = '';
486 }
487 if (!isset($mailbox)) {
488 $mailbox = '';
489 }
490 if (!isset($action)) {
491 $action = '';
492 }
493
494 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
495
496 /* in case the origin is not read_body.php */
497 if (isset($send_to)) {
498 $values['send_to'] = $send_to;
499 }
500 if (isset($send_to_cc)) {
501 $values['send_to_cc'] = $send_to_cc;
502 }
503 if (isset($send_to_bcc)) {
504 $values['send_to_bcc'] = $send_to_bcc;
505 }
506 showInputForm($session, $values);
507 }
508
509 exit();
510
511 /**************** Only function definitions go below *************/
512
513
514 /* This function is used when not sending or adding attachments */
515 function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
516 global $editor_size, $default_use_priority, $body,
517 $use_signature, $composesession, $data_dir, $username,
518 $username, $key, $imapServerAddress, $imapPort, $compose_messages,
519 $composeMessage;
520
521 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
522 $mailprio = 3;
523
524 if ($passed_id) {
525 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
526 $imapPort, 0);
527
528 sqimap_mailbox_select($imapConnection, $mailbox);
529 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
530
531 $body = '';
532 if ($passed_ent_id) {
533 /* redefine the messsage in case of message/rfc822 */
534 $message = $message->getEntity($passed_ent_id);
535 /* message is an entity which contains the envelope and type0=message
536 * and type1=rfc822. The actual entities are childs from
537 * $message->entities[0]. That's where the encoding and is located
538 */
539
540 $entities = $message->entities[0]->findDisplayEntity
541 (array(), $alt_order = array('text/plain'));
542 if (!count($entities)) {
543 $entities = $message->entities[0]->findDisplayEntity
544 (array(), $alt_order = array('text/plain','html/plain'));
545 }
546 $orig_header = $message->rfc822_header; /* here is the envelope located */
547 /* redefine the message for picking up the attachments */
548 $message = $message->entities[0];
549
550 } else {
551 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
552 if (!count($entities)) {
553 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
554 }
555 $orig_header = $message->rfc822_header;
556 }
557
558 $encoding = $message->header->encoding;
559 $type0 = $message->type0;
560 $type1 = $message->type1;
561 foreach ($entities as $ent) {
562 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
563 $body_part_entity = $message->getEntity($ent);
564 $bodypart = decodeBody($unencoded_bodypart,
565 $body_part_entity->header->encoding);
566 if ($type1 == 'html') {
567 $bodypart = strip_tags($bodypart);
568 }
569 $body .= $bodypart;
570 }
571 if ($default_use_priority) {
572 $mailprio = substr($orig_header->priority,0,1);
573 if (!$mailprio) {
574 $mailprio = 3;
575 }
576 } else {
577 $mailprio = '';
578 }
579 //ClearAttachments($session);
580
581 $identity = '';
582 $idents = getPref($data_dir, $username, 'identities');
583 $from_o = $orig_header->from;
584 if (is_object($from_o)) {
585 $orig_from = $from_o->getAddress();
586 } else {
587 $orig_from = '';
588 }
589 if (!empty($idents) && $idents > 1) {
590 for ($i = 1; $i < $idents; $i++) {
591 $enc_from_name = '"'.
592 getPref($data_dir,
593 $username,
594 'full_name' . $i) .
595 '" <' . getPref($data_dir, $username,
596 'email_address' . $i) . '>';
597 if ($enc_from_name == $orig_from) {
598 $identity = $i;
599 break;
600 }
601 }
602 }
603
604 switch ($action) {
605 case ('draft'):
606 $use_signature = FALSE;
607 $send_to = decodeHeader($orig_header->getAddr_s('to'));
608 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
609 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
610 $subject = decodeHeader($orig_header->subject);
611
612 $body_ary = explode("\n", $body);
613 $cnt = count($body_ary) ;
614 $body = '';
615 for ($i=0; $i < $cnt; $i++) {
616 if (!ereg("^[>\\s]*$", $body_ary[$i]) || !$body_ary[$i]) {
617 sqWordWrap($body_ary[$i], $editor_size );
618 $body .= $body_ary[$i] . "\n";
619 }
620 unset($body_ary[$i]);
621 }
622 sqUnWordWrap($body);
623 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
624 break;
625 case ('edit_as_new'):
626 $send_to = decodeHeader($orig_header->getAddr_s('to'));
627 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
628 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
629 $subject = decodeHeader($orig_header->subject);
630 $mailprio = $orig_header->priority;
631 $orig_from = '';
632 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
633 sqUnWordWrap($body);
634 break;
635 case ('forward'):
636 $send_to = '';
637 $subject = decodeHeader($orig_header->subject);
638 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
639 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
640 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
641 $subject = '[Fwd: ' . $subject . ']';
642 }
643 $body = getforwardHeader($orig_header) . $body;
644 sqUnWordWrap($body);
645 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
646 $body = "\n" . $body;
647 break;
648 case ('forward_as_attachment'):
649 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
650 $body = '';
651 break;
652 case ('reply_all'):
653 $send_to_cc = replyAllString($orig_header);
654 case ('reply'):
655 $send_to = $orig_header->reply_to;
656 if (is_object($send_to)) {
657 $send_to = decodeHeader($send_to->getAddr_s('reply_to'));
658 } else {
659 $send_to = decodeHeader($orig_header->getAddr_s('from'));
660 }
661 $subject = decodeHeader($orig_header->subject);
662 $subject = str_replace('"', "'", $subject);
663 $subject = trim($subject);
664 if (substr(strtolower($subject), 0, 3) != 're:') {
665 $subject = 'Re: ' . $subject;
666 }
667 /* this corrects some wrapping/quoting problems on replies */
668 $rewrap_body = explode("\n", $body);
669
670 $from = (is_array($orig_header->from)) ?
671 $orig_header->from[0] : $orig_header->from;
672 $body = getReplyCitation($from->getAddress(false));
673
674 $cnt = count($rewrap_body);
675 for ($i=0;$i<$cnt;$i++) {
676 // sqWordWrap($rewrap_body[$i], ($editor_size - 2));
677 if (preg_match("/^(>+)/", $rewrap_body[$i], $matches)) {
678 $gt = $matches[1];
679 $body .= '>' . str_replace("\n", "\n$gt ", $rewrap_body[$i]) ."\n";
680 } else {
681 $body .= '> ' . $rewrap_body[$i] . "\n";
682 }
683 unset($rewrap_body[$i]);
684 }
685 $composeMessage->reply_rfc822_header = $orig_header;
686 break;
687 default:
688 break;
689 }
690 $compose_messages[$session] = $composeMessage;
691 sqsession_register($compose_messages, 'compose_messages');
692
693 sqimap_logout($imapConnection);
694 }
695 $ret = array( 'send_to' => $send_to,
696 'send_to_cc' => $send_to_cc,
697 'send_to_bcc' => $send_to_bcc,
698 'subject' => $subject,
699 'mailprio' => $mailprio,
700 'body' => $body,
701 'identity' => $identity );
702
703 return ($ret);
704 } /* function newMail() */
705
706 function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
707 global $attachment_dir, $username, $data_dir, $squirrelmail_language;
708 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
709 if (!count($message->entities) ||
710 ($message->type0 == 'message' && $message->type1 == 'rfc822')) {
711 if ( !in_array($message->entity_id, $entities) && $message->entity_id) {
712 switch ($message->type0) {
713 case 'message':
714 if ($message->type1 == 'rfc822') {
715 $filename = decodeHeader($message->rfc822_header->subject.'.eml');
716 if ($filename == "") {
717 $filename = "untitled-".$message->entity_id.'.eml';
718 }
719 } else {
720 $filename = decodeHeader($message->getFilename());
721 }
722 break;
723 default:
724 $filename = decodeHeader($message->getFilename());
725 break;
726 }
727 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
728 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
729 $filename = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $filename);
730 }
731
732 $localfilename = GenerateRandomString(32, '', 7);
733 $full_localfilename = "$hashed_attachment_dir/$localfilename";
734 while (file_exists($full_localfilename)) {
735 $localfilename = GenerateRandomString(32, '', 7);
736 $full_localfilename = "$hashed_attachment_dir/$localfilename";
737 }
738 $message->att_local_name = $full_localfilename;
739 if (!$message->mime_header) { /* temporary hack */
740 $message->mime_header = $message->header;
741 }
742
743 $composeMessage->addEntity($message);
744
745 /* Write Attachment to file */
746 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
747 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
748 $passed_id, $message->entity_id),
749 $message->header->encoding));
750 fclose ($fp);
751 }
752 } else {
753 for ($i=0, $entCount=count($message->entities); $i<$entCount;$i++) {
754 $composeMessage=getAttachments($message->entities[$i], $composeMessage, $passed_id, $entities, $imapConnection);
755 }
756 }
757 // setPref($data_dir, $username, 'attachments', serialize($attachments));
758 return $composeMessage;
759 }
760
761 function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
762 $passed_ent_id='', $imapConnection) {
763 global $attachments, $attachment_dir, $username, $data_dir, $uid_support;
764 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
765 if (!$passed_ent_id) {
766 $body_a = sqimap_run_command($imapConnection,
767 'FETCH '.$passed_id.' RFC822',
768 TRUE, $response, $readmessage,
769 $uid_support);
770 } else {
771 $body_a = sqimap_run_command($imapConnection,
772 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
773 TRUE, $response, $readmessage, $uid_support);
774 $message = $message->parent;
775 }
776 if ($response = 'OK') {
777 $subject = encodeHeader($message->rfc822_header->subject);
778 array_shift($body_a);
779 $body = implode('', $body_a) . "\r\n";
780
781 $localfilename = GenerateRandomString(32, 'FILE', 7);
782 $full_localfilename = "$hashed_attachment_dir/$localfilename";
783
784 $fp = fopen( $full_localfilename, 'w');
785 fwrite ($fp, $body);
786 fclose($fp);
787 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
788 $full_localfilename);
789 }
790 return $composeMessage;
791 }
792
793 function showInputForm ($session, $values=false) {
794 global $send_to, $send_to_cc, $body,
795 $passed_body, $color, $use_signature, $signature, $prefix_sig,
796 $editor_size, $attachments, $subject, $newmail,
797 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
798 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
799 $username, $data_dir, $identity, $draft_id, $delete_draft,
800 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
801 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
802 $username, $compose_messages;
803
804 $composeMessage = $compose_messages[$session];
805
806 $subject = decodeHeader($subject, false);
807 if ($values) {
808 $send_to = $values['send_to'];
809 $send_to_cc = $values['send_to_cc'];
810 $send_to_bcc = $values['send_to_bcc'];
811 $subject = $values['subject'];
812 $mailprio = $values['mailprio'];
813 $body = $values['body'];
814 $identity = $values['identity'];
815 }
816
817 if ($use_javascript_addr_book) {
818 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
819 'function open_abook() { ' . "\n" .
820 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
821 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
822 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
823 ' nwin.opener = document.windows;' . "\n" .
824 "}\n" .
825 '// --></SCRIPT>' . "\n\n";
826 }
827
828 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
829 'ENCTYPE="multipart/form-data"';
830 do_hook("compose_form");
831
832 echo ">\n";
833
834 if ($action == 'draft') {
835 echo '<input type="hidden" name="delete_draft" value="' . $passed_id . "\">\n";
836 }
837 if (isset($delete_draft)) {
838 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
839 }
840 if (isset($session)) {
841 echo '<input type="hidden" name="session" value="' . $session . "\">\n";
842 }
843
844 if (isset($passed_id)) {
845 echo '<input type="hidden" name="passed_id" value="' . $passed_id . "\">\n";
846 }
847
848 if ($saved_draft == 'yes') {
849 echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
850 }
851 if ($mail_sent == 'yes') {
852 echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
853 }
854 echo '<TABLE ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
855 if ($compose_new_win == '1') {
856 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
857 ' <TR><TD></TD><TD ALIGN="RIGHT"><INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
858 }
859 if ($location_of_buttons == 'top') {
860 showComposeButtonRow();
861 }
862
863 $idents = getPref($data_dir, $username, 'identities', 0);
864 if ($idents > 1) {
865 echo ' <TR>' . "\n" .
866 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
867 "\n" .
868 _("From:") .
869 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
870 '<select name=identity>' . "\n" .
871 '<option value=default>' .
872 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
873 $em = getPref($data_dir, $username, 'email_address');
874 if ($em != '') {
875 echo htmlspecialchars(' <' . $em . '>') . "\n";
876 }
877 for ($i = 1; $i < $idents; $i ++) {
878 echo '<option value="' . $i . '"';
879 if (isset($identity) && $identity == $i) {
880 echo ' SELECTED';
881 }
882 echo '>' . htmlspecialchars(getPref($data_dir, $username,
883 'full_name' . $i));
884 $em = getPref($data_dir, $username, 'email_address' . $i);
885 if ($em != '') {
886 echo htmlspecialchars(' <' . $em . '>') . "\n";
887 }
888 echo '</option>';
889 }
890 echo '</select>' . "\n" .
891 ' </TD>' . "\n" .
892 ' </TR>' . "\n";
893 }
894 echo ' <TR>' . "\n" .
895 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' . "\n" .
896 _("To:") .
897 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
898 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
899 htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
900 ' </TD>' . "\n" .
901 ' </TR>' . "\n" .
902 ' <TR>' . "\n" .
903 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
904 _("CC:") .
905 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
906 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
907 htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
908 ' </TD>' . "\n" .
909 ' </TR>' . "\n" .
910 ' <TR>' . "\n" .
911 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
912 _("BCC:") .
913 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
914 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
915 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
916 '</TD></TR>' . "\n" .
917 ' <TR>' . "\n" .
918 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
919 _("Subject:") .
920 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
921 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
922 htmlspecialchars($subject) . '">';
923 echo '</td></tr>' . "\n\n";
924
925 if ($location_of_buttons == 'between') {
926 showComposeButtonRow();
927 }
928
929 if ($compose_new_win == '1') {
930 echo ' <TR>' . "\n" .
931 ' <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
932 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
933 $editor_size . '" WRAP="VIRTUAL">';
934 }
935 else {
936 echo ' <TR>' . "\n" .
937 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
938 ' &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS="' .
939 $editor_size . '" WRAP="VIRTUAL">';
940 }
941 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
942 if ($sig_first == '1') {
943 if ($charset == 'iso-2022-jp') {
944 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
945 } else {
946 echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
947 }
948 echo "\n\n".htmlspecialchars($body);
949 }
950 else {
951 echo "\n\n".htmlspecialchars($body);
952 if ($charset == 'iso-2022-jp') {
953 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
954 }else{
955 echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
956 }
957 }
958 }
959 else {
960 echo htmlspecialchars($body);
961 }
962 echo '</TEXTAREA><BR>' . "\n" .
963 ' </TD>' . "\n" .
964 ' </TR>' . "\n";
965
966 if ($location_of_buttons == 'bottom') {
967 showComposeButtonRow();
968 } else {
969 echo ' <TR><TD COLSPAN=2 ALIGN=RIGHT>' .
970 ' <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '">' .
971 ' &nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>' .
972 ' </TD></TR>' . "\n";
973 }
974
975 /* This code is for attachments */
976 echo '<table width="100%" cellpadding="0" cellspacing="4" align="center" border="0">' .
977 ' <tr><td>' .
978 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">' .
979 ' <tr><td>' .
980 ' <table width="100%" cellpadding="3" cellspacing="0" align="center" border="0">' .
981 ' <TR>' . "\n" .
982 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
983 _("Attach:") .
984 ' </TD>' . "\n" .
985 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
986 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
987 ' &nbsp;&nbsp;<input type="submit" name="attach"' .
988 ' value="' . _("Add") .'">' . "\n" .
989 ' </TD>' . "\n" .
990 ' </TR>' . "\n";
991
992
993 $s_a = array();
994 if ($composeMessage->entities) {
995 foreach ($composeMessage->entities as $key => $attachment) {
996 $attached_file = $attachment->att_local_name;
997 if ($attachment->att_local_name || $attachment->body_part) {
998 $attached_filename = decodeHeader($attachment->mime_header->getParameter('name'));
999 $type = $attachment->mime_header->type0.'/'.
1000 $attachment->mime_header->type1;
1001 $s_a[] = '<input type="checkbox" name="delete[]" value="' .
1002 $key . "\">\n" . $attached_filename . ' - ' . $type .
1003 ' ('.show_readable_size( filesize( $attached_file ) )
1004 . ')<br>'."\n";
1005 }
1006 }
1007 }
1008 if (count($s_a)) {
1009 foreach ($s_a as $s) {
1010 echo '<tr><td align=left colspan="2" bgcolor="' . $color[0] . '">'.$s.'</td></tr>';
1011 }
1012 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1013 _("Delete selected attachments") . "\">\n" .
1014 '</td></tr>';
1015 }
1016 echo ' </table></td></tr>' .
1017 ' </table>' .
1018 ' </td></tr>';
1019
1020 /* End of attachment code */
1021 if ($compose_new_win == '1') {
1022 echo '</TABLE>'."\n";
1023 }
1024 echo '</TABLE>' . "\n" .
1025 '<input type="hidden" name="username" value="'. $username . "\">\n" .
1026 '<input type=hidden name=action value=' . $action . ">\n" .
1027 '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
1028 "\">\n";
1029 echo '</FORM>';
1030 do_hook('compose_bottom');
1031 echo '</BODY></HTML>' . "\n";
1032 }
1033
1034
1035 function showComposeButtonRow() {
1036 global $use_javascript_addr_book, $save_as_draft,
1037 $default_use_priority, $mailprio, $default_use_mdn,
1038 $request_mdn, $request_dr,
1039 $data_dir, $username;
1040
1041 echo " <TR><TD>\n</TD><TD>\n";
1042 if ($default_use_priority) {
1043 if(!isset($mailprio)) {
1044 $mailprio = "3";
1045 }
1046 echo _("Priority") .': <select name="mailprio">'.
1047 '<option value="1"'.($mailprio=='1'?' selected':'').'>'. _("High") .'</option>'.
1048 '<option value="3"'.($mailprio=='3'?' selected':'').'>'. _("Normal") .'</option>'.
1049 '<option value="5"'.($mailprio=='5'?' selected':'').'>'. _("Low").'</option>'.
1050 "</select>";
1051 }
1052 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1053 if ($default_use_mdn) {
1054 if ($mdn_user_support) {
1055 echo "\n\t". _("Receipt") .': '.
1056 '<input type="checkbox" name="request_mdn" value=1'.
1057 ($request_mdn=='1'?' checked':'') .'>'. _("On Read").
1058 ' <input type="checkbox" name="request_dr" value=1'.
1059 ($request_dr=='1'?' checked':'') .'>'. _("On Delivery");
1060 }
1061 }
1062
1063 echo " </td></tr>\n <TR><td>\n </td><td>\n" .
1064 "\n <INPUT TYPE=SUBMIT NAME=\"sigappend\" VALUE=\"". _("Signature") . "\">\n";
1065 if ($use_javascript_addr_book) {
1066 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
1067 " <input type=button value=\\\""._("Addresses").
1068 "\\\" onclick='javascript:open_abook();'>\");".
1069 " // --></SCRIPT><NOSCRIPT>\n".
1070 " <input type=submit name=\"html_addr_search\" value=\"".
1071 _("Addresses")."\">".
1072 " </NOSCRIPT>\n";
1073 } else {
1074 echo " <input type=submit name=\"html_addr_search\" value=\"".
1075 _("Addresses")."\">";
1076 }
1077
1078 if ($save_as_draft) {
1079 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
1080 }
1081
1082 echo '<INPUT TYPE=submit NAME=send VALUE="'. _("Send") . "\">\n";
1083 do_hook('compose_button_row');
1084
1085 echo " </TD></TR>\n\n";
1086 }
1087
1088 function checkInput ($show) {
1089 /*
1090 * I implemented the $show variable because the error messages
1091 * were getting sent before the page header. So, I check once
1092 * using $show=false, and then when i'm ready to display the error
1093 * message, show=true
1094 */
1095 global $body, $send_to, $send_to_bcc, $subject, $color;
1096
1097 if ($send_to == '' && $send_to_bcc == '') {
1098 if ($show) {
1099 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
1100 }
1101 return false;
1102 }
1103 return true;
1104 } /* function checkInput() */
1105
1106
1107 /* True if FAILURE */
1108 function saveAttachedFiles($session) {
1109 global $_FILES, $attachment_dir, $attachments, $username,
1110 $data_dir, $compose_messages;
1111
1112 /* get out of here if no file was attached at all */
1113 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1114 return true;
1115 }
1116
1117 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1118 $localfilename = GenerateRandomString(32, '', 7);
1119 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1120 while (file_exists($full_localfilename)) {
1121 $localfilename = GenerateRandomString(32, '', 7);
1122 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1123 }
1124
1125 if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1126 if (function_exists("move_uploaded_file")) {
1127 if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
1128 return true;
1129 }
1130 } else {
1131 if (!@copy($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1132 return true;
1133 }
1134 }
1135 }
1136 $message = $compose_messages[$session];
1137 $type = strtolower($_FILES['attachfile']['type']);
1138 $name = $_FILES['attachfile']['name'];
1139 $message->initAttachment($type, $name, $full_localfilename);
1140 $compose_messages[$session] = $message;
1141 }
1142
1143 function ClearAttachments($composeMessage) {
1144 if ($composeMessage->att_local_name) {
1145 $attached_file = $composeMessage->att_local_name;
1146 if (file_exists($attached_file)) {
1147 unlink($attached_file);
1148 }
1149 }
1150 for ($i=0, $entCount=count($composeMessage->entities);$i< $entCount; ++$i) {
1151 ClearAttachments($composeMessage->entities[$i]);
1152 }
1153 }
1154
1155
1156
1157 function getReplyCitation($orig_from) {
1158 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
1159
1160 /* First, return an empty string when no citation style selected. */
1161 if (($reply_citation_style == '') || ($reply_citation_style == 'none')) {
1162 return '';
1163 }
1164
1165 /* Make sure our final value isn't an empty string. */
1166 if ($orig_from == '') {
1167 return '';
1168 }
1169
1170 /* Otherwise, try to select the desired citation style. */
1171 switch ($reply_citation_style) {
1172 case 'author_said':
1173 $start = '';
1174 $end = ' ' . _("said") . ':';
1175 break;
1176 case 'quote_who':
1177 $start = '<' . _("quote") . ' ' . _("who") . '="';
1178 $end = '">';
1179 break;
1180 case 'user-defined':
1181 $start = $reply_citation_start .
1182 ($reply_citation_start == '' ? '' : ' ');
1183 $end = $reply_citation_end;
1184 break;
1185 default:
1186 return '';
1187 }
1188
1189 /* Build and return the citation string. */
1190 return ($start . $orig_from . $end . "\n");
1191 }
1192
1193 /* temporary function to make use of the deliver class.
1194 In the future the responsable backend should be automaticly loaded
1195 and conf.pl should show a list of available backends.
1196 The message also should be constructed by the message class.
1197 */
1198
1199 function sendMessage($composeMessage, $draft=false) {
1200 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1201 $username, $popuser, $usernamedata, $identity, $data_dir,
1202 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1203 $domain, $action;
1204 global $imapServerAddress, $imapPort, $sent_folder, $key;
1205
1206 $rfc822_header = $composeMessage->rfc822_header;
1207 $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain);
1208 $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain);
1209 $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain);
1210 $rfc822_header->priority = $mailprio;
1211 $rfc822_header->subject = $subject;
1212 $special_encoding='';
1213 if (strtolower($default_charset) == 'iso-2022-jp') {
1214 if (mb_detect_encoding($body) == 'ASCII') {
1215 $special_encoding = '8bit';
1216 } else {
1217 $body = mb_convert_encoding($body, 'JIS');
1218 $special_encoding = '7bit';
1219 }
1220 }
1221 $composeMessage->setBody($body);
1222
1223 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1224 $popuser = $usernamedata[1];
1225 $domain = $usernamedata[2];
1226 unset($usernamedata);
1227 } else {
1228 $popuser = $username;
1229 }
1230 $reply_to = '';
1231 if (isset($identity) && $identity != 'default') {
1232 $from_mail = getPref($data_dir, $username,'email_address' . $identity);
1233 $full_name = getPref($data_dir, $username,'full_name' . $identity);
1234 $reply_to = getPref($data_dir, $username,'reply_to' . $identity);
1235 } else {
1236 $from_mail = getPref($data_dir, $username, 'email_address');
1237 $full_name = getPref($data_dir, $username, 'full_name');
1238 $reply_to = getPref($data_dir, $username,'reply_to');
1239 }
1240 if (!$from_mail) {
1241 $from_mail = "$popuser@$domain";
1242 $full_name = '';
1243 }
1244 $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
1245 if ($full_name) {
1246 $from = $rfc822_header->from[0];
1247 if (!$from->host) $from->host = $domain;
1248 $from_addr = $full_name .' <'.$from->mailbox.'@'.$from->host.'>';
1249 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
1250 }
1251 if ($reply_to) {
1252 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
1253 }
1254 /* Receipt: On Read */
1255 if (isset($request_mdn) && $request_mdn) {
1256 $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true);
1257 }
1258 /* Receipt: On Delivery */
1259 if (isset($request_dr) && $request_dr) {
1260 $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail;
1261 }
1262 /* multipart messages */
1263 if (count($composeMessage->entities)) {
1264 $message_body = new Message();
1265 $message_body->body_part = $composeMessage->body_part;
1266 $composeMessage->body_part = '';
1267 $mime_header = new MessageHeader;
1268 $mime_header->type0 = 'text';
1269 $mime_header->type1 = 'plain';
1270 if ($special_encoding) {
1271 $mime_header->encoding = $special_encoding;
1272 } else {
1273 $mime_header->encoding = 'us-ascii';
1274 }
1275 if ($default_charset) {
1276 $mime_header->parameters['charset'] = $default_charset;
1277 }
1278 $message_body->mime_header = $mime_header;
1279 array_unshift($composeMessage->entities, $message_body);
1280 $content_type = new ContentType('multipart/mixed');
1281 } else {
1282 $content_type = new ContentType('text/plain');
1283 }
1284 if ($default_charset) {
1285 $content_type->properties['charset']=$default_charset;
1286 }
1287
1288 $rfc822_header->content_type = $content_type;
1289 $composeMessage->rfc822_header = $rfc822_header;
1290
1291 if (!$useSendmail && !$draft) {
1292 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
1293 $deliver = new Deliver_SMTP();
1294 global $smtpServerAddress, $smtpPort, $use_authenticated_smtp, $pop_before_smtp;
1295 if ($use_authenticated_smtp) {
1296 global $key, $onetimepad;
1297 $user = $username;
1298 $pass = OneTimePadDecrypt($key, $onetimepad);
1299 } else {
1300 $user = '';
1301 $pass = '';
1302 }
1303 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
1304 $stream = $deliver->initStream($composeMessage,$domain,0,
1305 $smtpServerAddress, $smtpPort, $authPop);
1306 } elseif (!$draft) {
1307 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
1308 global $sendmail_path;
1309 $deliver = new Deliver_SendMail();
1310 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1311 } elseif ($draft) {
1312 global $draft_folder;
1313 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1314 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1315 $imapPort, 0);
1316 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1317 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1318 $imap_deliver = new Deliver_IMAP();
1319 $length = $imap_deliver->mail($composeMessage);
1320 sqimap_append ($imap_stream, $draft_folder, $length);
1321 $imap_deliver->mail($composeMessage, $imap_stream);
1322 sqimap_append_done ($imap_stream);
1323 sqimap_logout($imap_stream);
1324 unset ($imap_deliver);
1325 return $length;
1326 } else {
1327 $msg = '<br>Error: '._("Draft folder")." $draft_folder" . ' does not exist.';
1328 plain_error_message($msg, $color);
1329 return false;
1330 }
1331 }
1332 $succes = false;
1333 if ($stream) {
1334 $length = $deliver->mail($composeMessage, $stream);
1335 $succes = $deliver->finalizeStream($stream);
1336 }
1337 if (!$succes) {
1338 $msg = $deliver->dlv_msg . '<br>Server replied: '.$deliver->dlv_ret_nr;
1339 plain_error_message($msg, $color);
1340 } else {
1341 unset ($deliver);
1342 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1343 $imapPort, 0);
1344 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
1345 sqimap_append ($imap_stream, $sent_folder, $length);
1346 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1347 $imap_deliver = new Deliver_IMAP();
1348 $imap_deliver->mail($composeMessage, $imap_stream);
1349 sqimap_append_done ($imap_stream);
1350 unset ($imap_deliver);
1351 }
1352 global $passed_id, $mailbox, $action;
1353 ClearAttachments($composeMessage);
1354 if ($action == 'reply' || $action == 'reply_all') {
1355 sqimap_mailbox_select ($imap_stream, $mailbox);
1356 sqimap_messages_flag ($imap_stream, $passed_id, $passed_id, 'Answered', true);
1357 }
1358 sqimap_logout($imap_stream);
1359 }
1360 return $succes;
1361 }
1362
1363 ?>