| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/steps/mail/sendmail.inc | |
|---|
| 6 | | | |
|---|
| 7 | | This file is part of the RoundCube Webmail client | |
|---|
| 8 | | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland | |
|---|
| 9 | | Licensed under the GNU GPL | |
|---|
| 10 | | | |
|---|
| 11 | | PURPOSE: | |
|---|
| 12 | | Compose a new mail message with all headers and attachments | |
|---|
| 13 | | and send it using IlohaMail's SMTP methods or with PHP mail() | |
|---|
| 14 | | | |
|---|
| 15 | +-----------------------------------------------------------------------+ |
|---|
| 16 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 17 | +-----------------------------------------------------------------------+ |
|---|
| 18 | |
|---|
| 19 | $Id$ |
|---|
| 20 | |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | //require_once('lib/smtp.inc'); |
|---|
| 25 | require_once('lib/html2text.inc'); |
|---|
| 26 | require_once('lib/rc_mail_mime.inc'); |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | if (!isset($_SESSION['compose']['id'])) |
|---|
| 30 | { |
|---|
| 31 | rcmail_overwrite_action('list'); |
|---|
| 32 | return; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | /****** message sending functions ********/ |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | // get identity record |
|---|
| 40 | function rcmail_get_identity($id) |
|---|
| 41 | { |
|---|
| 42 | global $USER, $OUTPUT; |
|---|
| 43 | |
|---|
| 44 | if ($sql_arr = $USER->get_identity($id)) |
|---|
| 45 | { |
|---|
| 46 | $out = $sql_arr; |
|---|
| 47 | $out['mailto'] = $sql_arr['email']; |
|---|
| 48 | $name = strpos($sql_arr['name'], ",") ? '"'.$sql_arr['name'].'"' : $sql_arr['name']; |
|---|
| 49 | $out['string'] = sprintf('%s <%s>', |
|---|
| 50 | rcube_charset_convert($name, RCMAIL_CHARSET, $OUTPUT->get_charset()), |
|---|
| 51 | $sql_arr['email']); |
|---|
| 52 | return $out; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | return FALSE; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * go from this: |
|---|
| 60 | * <img src=".../tiny_mce/plugins/emotions/images/smiley-cool.gif" border="0" alt="Cool" title="Cool" /> |
|---|
| 61 | * |
|---|
| 62 | * to this: |
|---|
| 63 | * |
|---|
| 64 | * <IMG src="cid:smiley-cool.gif"/> |
|---|
| 65 | * ... |
|---|
| 66 | * ------part... |
|---|
| 67 | * Content-Type: image/gif |
|---|
| 68 | * Content-Transfer-Encoding: base64 |
|---|
| 69 | * Content-ID: <smiley-cool.gif> |
|---|
| 70 | */ |
|---|
| 71 | function rcmail_attach_emoticons(&$mime_message) |
|---|
| 72 | { |
|---|
| 73 | global $CONFIG, $INSTALL_PATH; |
|---|
| 74 | |
|---|
| 75 | $htmlContents = $mime_message->getHtmlBody(); |
|---|
| 76 | |
|---|
| 77 | // remove any null-byte characters before parsing |
|---|
| 78 | $body = preg_replace('/\x00/', '', $htmlContents); |
|---|
| 79 | |
|---|
| 80 | $last_img_pos = 0; |
|---|
| 81 | |
|---|
| 82 | $searchstr = 'program/js/tiny_mce/plugins/emotions/images/'; |
|---|
| 83 | |
|---|
| 84 | // keep track of added images, so they're only added once |
|---|
| 85 | $included_images = array(); |
|---|
| 86 | |
|---|
| 87 | // find emoticon image tags |
|---|
| 88 | while ($pos = strpos($body, $searchstr, $last_img_pos)) |
|---|
| 89 | { |
|---|
| 90 | $pos2 = strpos($body, '"', $pos); |
|---|
| 91 | $body_pre = substr($body, 0, $pos); |
|---|
| 92 | $image_name = substr($body, |
|---|
| 93 | $pos + strlen($searchstr), |
|---|
| 94 | $pos2 - ($pos + strlen($searchstr))); |
|---|
| 95 | // sanitize image name so resulting attachment doesn't leave images dir |
|---|
| 96 | $image_name = preg_replace('/[^a-zA-Z0-9_\.\-]/i','',$image_name); |
|---|
| 97 | |
|---|
| 98 | $body_post = substr($body, $pos2); |
|---|
| 99 | |
|---|
| 100 | if (! in_array($image_name, $included_images)) |
|---|
| 101 | { |
|---|
| 102 | // add the image to the MIME message |
|---|
| 103 | $img_file = $INSTALL_PATH . '/' . $searchstr . $image_name; |
|---|
| 104 | if(! $mime_message->addHTMLImage($img_file, 'image/gif', '', true, '_' . $image_name)) |
|---|
| 105 | $OUTPUT->show_message("emoticonerror", 'error'); |
|---|
| 106 | |
|---|
| 107 | array_push($included_images, $image_name); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | $body = $body_pre . 'cid:_' . $image_name . $body_post; |
|---|
| 111 | |
|---|
| 112 | $last_img_pos = $pos2; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | $mime_message->setHTMLBody($body); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | if (strlen($_POST['_draft_saveid']) > 3) |
|---|
| 119 | $olddraftmessageid = get_input_value('_draft_saveid', RCUBE_INPUT_POST); |
|---|
| 120 | |
|---|
| 121 | $message_id = sprintf('<%s@%s>', md5(uniqid('rcmail'.rand(),true)), rcmail_mail_domain($_SESSION['imap_host'])); |
|---|
| 122 | $savedraft = !empty($_POST['_draft']) ? TRUE : FALSE; |
|---|
| 123 | |
|---|
| 124 | // remove all scripts and act as called in frame |
|---|
| 125 | $OUTPUT->reset(); |
|---|
| 126 | $OUTPUT->framed = TRUE; |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | /****** check submission and compose message ********/ |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | if (!$savedraft && empty($_POST['_to']) && empty($_POST['_subject']) && $_POST['_message']) |
|---|
| 133 | { |
|---|
| 134 | $OUTPUT->show_message("sendingfailed", 'error'); |
|---|
| 135 | $OUTPUT->send('iframe'); |
|---|
| 136 | return; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | // set default charset |
|---|
| 141 | $input_charset = $OUTPUT->get_charset(); |
|---|
| 142 | $message_charset = isset($_POST['_charset']) ? $_POST['_charset'] : $input_charset; |
|---|
| 143 | |
|---|
| 144 | $mailto_regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m', '/;/'); |
|---|
| 145 | $mailto_replace = array(', ', ', ', '', ','); |
|---|
| 146 | |
|---|
| 147 | // replace new lines and strip ending ', ' |
|---|
| 148 | $mailto = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_to', RCUBE_INPUT_POST, TRUE, $message_charset)); |
|---|
| 149 | |
|---|
| 150 | // decode address strings |
|---|
| 151 | $to_address_arr = $IMAP->decode_address_list($mailto); |
|---|
| 152 | $identity_arr = rcmail_get_identity(get_input_value('_from', RCUBE_INPUT_POST)); |
|---|
| 153 | |
|---|
| 154 | $from = $identity_arr['mailto']; |
|---|
| 155 | $first_to = is_array($to_address_arr[0]) ? $to_address_arr[0]['mailto'] : $mailto; |
|---|
| 156 | |
|---|
| 157 | if (empty($identity_arr['string'])) |
|---|
| 158 | $identity_arr['string'] = $from; |
|---|
| 159 | |
|---|
| 160 | // compose headers array |
|---|
| 161 | $headers = array('Date' => date('r'), |
|---|
| 162 | 'From' => rcube_charset_convert($identity_arr['string'], RCMAIL_CHARSET, $message_charset), |
|---|
| 163 | 'To' => $mailto); |
|---|
| 164 | |
|---|
| 165 | // additional recipients |
|---|
| 166 | if (!empty($_POST['_cc'])) |
|---|
| 167 | $headers['Cc'] = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_cc', RCUBE_INPUT_POST, TRUE, $message_charset)); |
|---|
| 168 | |
|---|
| 169 | if (!empty($_POST['_bcc'])) |
|---|
| 170 | $headers['Bcc'] = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_bcc', RCUBE_INPUT_POST, TRUE, $message_charset)); |
|---|
| 171 | |
|---|
| 172 | if (!empty($identity_arr['bcc'])) |
|---|
| 173 | $headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc']; |
|---|
| 174 | |
|---|
| 175 | // add subject |
|---|
| 176 | $headers['Subject'] = trim(get_input_value('_subject', RCUBE_INPUT_POST, FALSE, $message_charset)); |
|---|
| 177 | |
|---|
| 178 | if (!empty($identity_arr['organization'])) |
|---|
| 179 | $headers['Organization'] = $identity_arr['organization']; |
|---|
| 180 | |
|---|
| 181 | if (!empty($_POST['_replyto'])) |
|---|
| 182 | $headers['Reply-To'] = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_replyto', RCUBE_INPUT_POST, TRUE, $message_charset)); |
|---|
| 183 | else if (!empty($identity_arr['reply-to'])) |
|---|
| 184 | $headers['Reply-To'] = $identity_arr['reply-to']; |
|---|
| 185 | |
|---|
| 186 | if (!empty($_SESSION['compose']['reply_msgid'])) |
|---|
| 187 | $headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid']; |
|---|
| 188 | |
|---|
| 189 | if (!empty($_SESSION['compose']['references'])) |
|---|
| 190 | $headers['References'] = $_SESSION['compose']['references']; |
|---|
| 191 | |
|---|
| 192 | if (!empty($_POST['_priority'])) |
|---|
| 193 | { |
|---|
| 194 | $priority = intval($_POST['_priority']); |
|---|
| 195 | $a_priorities = array(1=>'highest', 2=>'high', 4=>'low', 5=>'lowest'); |
|---|
| 196 | if ($str_priority = $a_priorities[$priority]) |
|---|
| 197 | $headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority)); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | if (!empty($_POST['_receipt'])) |
|---|
| 201 | { |
|---|
| 202 | $headers['Return-Receipt-To'] = $identity_arr['string']; |
|---|
| 203 | $headers['Disposition-Notification-To'] = $identity_arr['string']; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | // additional headers |
|---|
| 207 | $headers['Message-ID'] = $message_id; |
|---|
| 208 | $headers['X-Sender'] = $from; |
|---|
| 209 | |
|---|
| 210 | if (!empty($CONFIG['useragent'])) |
|---|
| 211 | $headers['User-Agent'] = $CONFIG['useragent']; |
|---|
| 212 | |
|---|
| 213 | // fetch message body |
|---|
| 214 | $message_body = get_input_value('_message', RCUBE_INPUT_POST, TRUE, $message_charset); |
|---|
| 215 | |
|---|
| 216 | // append generic footer to all messages |
|---|
| 217 | if (!$savedraft && !empty($CONFIG['generic_message_footer']) && ($footer = file_get_contents(realpath($CONFIG['generic_message_footer'])))) |
|---|
| 218 | $message_body .= "\r\n" . rcube_charset_convert($footer, 'UTF-8', $message_charset); |
|---|
| 219 | |
|---|
| 220 | $isHtmlVal = strtolower(get_input_value('_is_html', RCUBE_INPUT_POST)); |
|---|
| 221 | $isHtml = ($isHtmlVal == "1"); |
|---|
| 222 | |
|---|
| 223 | // create extended PEAR::Mail_mime instance |
|---|
| 224 | $MAIL_MIME = new rc_mail_mime(rcmail_header_delm()); |
|---|
| 225 | |
|---|
| 226 | // For HTML-formatted messages, construct the MIME message with both |
|---|
| 227 | // the HTML part and the plain-text part |
|---|
| 228 | |
|---|
| 229 | if ($isHtml) |
|---|
| 230 | { |
|---|
| 231 | $MAIL_MIME->setHTMLBody($message_body); |
|---|
| 232 | |
|---|
| 233 | // add a plain text version of the e-mail as an alternative part. |
|---|
| 234 | $h2t = new html2text($message_body); |
|---|
| 235 | $plainTextPart = wordwrap($h2t->get_text(), 998, "\r\n", true); |
|---|
| 236 | $MAIL_MIME->setTXTBody($plainTextPart); |
|---|
| 237 | |
|---|
| 238 | // look for "emoticon" images from TinyMCE and copy into message as attachments |
|---|
| 239 | rcmail_attach_emoticons($MAIL_MIME); |
|---|
| 240 | } |
|---|
| 241 | else |
|---|
| 242 | { |
|---|
| 243 | $message_body = wordwrap($message_body, 75, "\r\n"); |
|---|
| 244 | $message_body = wordwrap($message_body, 998, "\r\n", true); |
|---|
| 245 | $MAIL_MIME->setTXTBody($message_body, FALSE, TRUE); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | // add stored attachments, if any |
|---|
| 250 | if (is_array($_SESSION['compose']['attachments'])) |
|---|
| 251 | foreach ($_SESSION['compose']['attachments'] as $attachment) |
|---|
| 252 | $MAIL_MIME->addAttachment($attachment['path'], $attachment['mimetype'], $attachment['name'], true, 'base64', 'attachment', $message_charset); |
|---|
| 253 | |
|---|
| 254 | // add submitted attachments |
|---|
| 255 | if (is_array($_FILES['_attachments']['tmp_name'])) |
|---|
| 256 | foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) |
|---|
| 257 | $MAIL_MIME->addAttachment($filepath, $files['type'][$i], $files['name'][$i], true, 'base64', 'attachment', $message_charset); |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | // chose transfer encoding |
|---|
| 261 | $charset_7bit = array('ASCII', 'ISO-2022-JP', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-15'); |
|---|
| 262 | $transfer_encoding = in_array(strtoupper($message_charset), $charset_7bit) ? '7bit' : '8bit'; |
|---|
| 263 | |
|---|
| 264 | // encoding settings for mail composing |
|---|
| 265 | $MAIL_MIME->setParam(array( |
|---|
| 266 | 'text_encoding' => $transfer_encoding, |
|---|
| 267 | 'html_encoding' => 'quoted-printable', |
|---|
| 268 | 'head_encoding' => 'quoted-printable', |
|---|
| 269 | 'head_charset' => $message_charset, |
|---|
| 270 | 'html_charset' => $message_charset, |
|---|
| 271 | 'text_charset' => $message_charset, |
|---|
| 272 | )); |
|---|
| 273 | |
|---|
| 274 | // encoding subject header with mb_encode provides better results with asian characters |
|---|
| 275 | if ($MBSTRING && function_exists("mb_encode_mimeheader")) |
|---|
| 276 | { |
|---|
| 277 | mb_internal_encoding($message_charset); |
|---|
| 278 | $headers['Subject'] = mb_encode_mimeheader($headers['Subject'], $message_charset, 'Q'); |
|---|
| 279 | mb_internal_encoding(RCMAIL_CHARSET); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | // pass headers to message object |
|---|
| 283 | $MAIL_MIME->headers($headers); |
|---|
| 284 | |
|---|
| 285 | // Begin SMTP Delivery Block |
|---|
| 286 | if (!$savedraft) |
|---|
| 287 | { |
|---|
| 288 | $sent = rcmail_deliver_message($MAIL_MIME, $from, $mailto); |
|---|
| 289 | |
|---|
| 290 | // return to compose page if sending failed |
|---|
| 291 | if (!$sent) |
|---|
| 292 | { |
|---|
| 293 | $OUTPUT->show_message("sendingfailed", 'error'); |
|---|
| 294 | $OUTPUT->send('iframe'); |
|---|
| 295 | return; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | // set repliead flag |
|---|
| 299 | if ($_SESSION['compose']['reply_uid']) |
|---|
| 300 | $IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED'); |
|---|
| 301 | |
|---|
| 302 | } // End of SMTP Delivery Block |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | // Determine which folder to save message |
|---|
| 307 | if ($savedraft) |
|---|
| 308 | $store_target = 'drafts_mbox'; |
|---|
| 309 | else |
|---|
| 310 | $store_target = 'sent_mbox'; |
|---|
| 311 | |
|---|
| 312 | if ($CONFIG[$store_target]) |
|---|
| 313 | { |
|---|
| 314 | // check if mailbox exists |
|---|
| 315 | if (!in_array_nocase($CONFIG[$store_target], $IMAP->list_mailboxes())) |
|---|
| 316 | $store_folder = $IMAP->create_mailbox($CONFIG[$store_target], TRUE); |
|---|
| 317 | else |
|---|
| 318 | $store_folder = TRUE; |
|---|
| 319 | |
|---|
| 320 | // append message to sent box |
|---|
| 321 | if ($store_folder) |
|---|
| 322 | $saved = $IMAP->save_message($CONFIG[$store_target], $MAIL_MIME->getMessage()); |
|---|
| 323 | |
|---|
| 324 | // raise error if saving failed |
|---|
| 325 | if (!$saved) |
|---|
| 326 | { |
|---|
| 327 | raise_error(array('code' => 800, 'type' => 'imap', 'file' => __FILE__, |
|---|
| 328 | 'message' => "Could not save message in $CONFIG[$store_target]"), TRUE, FALSE); |
|---|
| 329 | |
|---|
| 330 | $OUTPUT->show_message('errorsaving', 'error'); |
|---|
| 331 | $OUTPUT->send('iframe'); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | if ($olddraftmessageid) |
|---|
| 335 | { |
|---|
| 336 | // delete previous saved draft |
|---|
| 337 | $a_deleteid = $IMAP->search($CONFIG['drafts_mbox'],'HEADER Message-ID',$olddraftmessageid); |
|---|
| 338 | $deleted = $IMAP->delete_message($IMAP->get_uid($a_deleteid[0],$CONFIG['drafts_mbox']),$CONFIG['drafts_mbox']); |
|---|
| 339 | |
|---|
| 340 | // raise error if deletion of old draft failed |
|---|
| 341 | if (!$deleted) |
|---|
| 342 | raise_error(array('code' => 800, 'type' => 'imap', 'file' => __FILE__, |
|---|
| 343 | 'message' => "Could not delete message from ".$CONFIG['drafts_mbox']), TRUE, FALSE); |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | if ($savedraft) |
|---|
| 348 | { |
|---|
| 349 | // display success |
|---|
| 350 | $OUTPUT->show_message('messagesaved', 'confirmation'); |
|---|
| 351 | |
|---|
| 352 | // update "_draft_saveid" and the "cmp_hash" to prevent "Unsaved changes" warning |
|---|
| 353 | $OUTPUT->command('set_draft_id', str_replace(array('<','>'), "", $message_id)); |
|---|
| 354 | $OUTPUT->command('compose_field_hash', true); |
|---|
| 355 | |
|---|
| 356 | // start the auto-save timer again |
|---|
| 357 | $OUTPUT->command('auto_save_start'); |
|---|
| 358 | |
|---|
| 359 | $OUTPUT->send('iframe'); |
|---|
| 360 | } |
|---|
| 361 | else |
|---|
| 362 | { |
|---|
| 363 | if ($CONFIG['smtp_log']) |
|---|
| 364 | { |
|---|
| 365 | $log_entry = sprintf( |
|---|
| 366 | "[%s] User: %d on %s; Message for %s; %s\n", |
|---|
| 367 | date("d-M-Y H:i:s O", mktime()), |
|---|
| 368 | $_SESSION['user_id'], |
|---|
| 369 | $_SERVER['REMOTE_ADDR'], |
|---|
| 370 | $mailto, |
|---|
| 371 | !empty($smtp_response) ? join('; ', $smtp_response) : ''); |
|---|
| 372 | |
|---|
| 373 | if ($fp = @fopen($CONFIG['log_dir'].'/sendmail', 'a')) |
|---|
| 374 | { |
|---|
| 375 | fwrite($fp, $log_entry); |
|---|
| 376 | fclose($fp); |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | rcmail_compose_cleanup(); |
|---|
| 381 | $OUTPUT->command('sent_successfully', rcube_label('messagesent')); |
|---|
| 382 | $OUTPUT->send('iframe'); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | ?> |
|---|