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