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