thomascube
2007-11-25 d5342aabcfeddb959cc286befe6de5bf35fe9d76
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/sendmail.inc                                       |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
f11541 8  | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland                 |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 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
968bdc 24 //require_once('lib/smtp.inc');
T 25 require_once('include/rcube_smtp.inc');
a0109c 26 require_once('lib/html2text.inc');
ab6f80 27 require_once('lib/rc_mail_mime.inc');
4e17e6 28
T 29
30 if (!isset($_SESSION['compose']['id']))
31   {
10a699 32   rcmail_overwrite_action('list');
4e17e6 33   return;
T 34   }
35
36
37 /****** message sending functions ********/
38
39
40 function rcmail_get_identity($id)
41   {
f11541 42   global $DB, $OUTPUT;
4e17e6 43   
T 44   // get identity record
d7cb77 45   $sql_result = $DB->query("SELECT *, email AS mailto
S 46                             FROM ".get_table_name('identities')."
47                             WHERE  identity_id=?
48                             AND    user_id=?
1cded8 49                             AND    del<>1",
d7cb77 50                             $id,$_SESSION['user_id']);
4e17e6 51                                    
T 52   if ($DB->num_rows($sql_result))
53     {
54     $sql_arr = $DB->fetch_assoc($sql_result);
55     $out = $sql_arr;
abb32e 56     $name = strpos($sql_arr['name'], ",") ? '"'.$sql_arr['name'].'"' : $sql_arr['name'];
13c1af 57     $out['string'] = sprintf('%s <%s>',
f11541 58                              rcube_charset_convert($name, RCMAIL_CHARSET, $OUTPUT->get_charset()),
13c1af 59                              $sql_arr['mailto']);
4e17e6 60     return $out;
T 61     }
62
63   return FALSE;  
64   }
65
a0109c 66 /**
S 67  * go from this:
68  * <img src=".../tiny_mce/plugins/emotions/images/smiley-cool.gif" border="0" alt="Cool" title="Cool" />
69  *
70  * to this:
71  *
72  * <IMG src="cid:smiley-cool.gif"/>
73  * ...
74  * ------part...
75  * Content-Type: image/gif
76  * Content-Transfer-Encoding: base64
77  * Content-ID: <smiley-cool.gif>
78  */
79 function rcmail_attach_emoticons(&$mime_message)
80 {
81   global $CONFIG, $INSTALL_PATH;
82
83   $htmlContents = $mime_message->getHtmlBody();
84
85   // remove any null-byte characters before parsing
86   $body = preg_replace('/\x00/', '', $htmlContents);
87   
88   $last_img_pos = 0;
89
90   $searchstr = 'program/js/tiny_mce/plugins/emotions/images/';
91
ed6592 92   // keep track of added images, so they're only added once
S 93   $included_images = array();
94
a0109c 95   // find emoticon image tags
S 96   while ($pos = strpos($body, $searchstr, $last_img_pos))
97     {
98     $pos2 = strpos($body, '"', $pos);
99     $body_pre = substr($body, 0, $pos);
ed6592 100     $image_name = substr($body,
S 101                          $pos + strlen($searchstr),
102                          $pos2 - ($pos + strlen($searchstr)));
ee883a 103     // sanitize image name so resulting attachment doesn't leave images dir
T 104     $image_name = preg_replace('/[^a-zA-Z0-9_\.\-]/i','',$image_name);
a0109c 105
4e6eb1 106     $body_post = substr($body, $pos2);
S 107
ed6592 108     if (! in_array($image_name, $included_images))
a0109c 109       {
ed6592 110       // add the image to the MIME message
S 111       $img_file = $INSTALL_PATH . '/' . $searchstr . $image_name;
112       if(! $mime_message->addHTMLImage($img_file, 'image/gif', '', true, '_' . $image_name))
f11541 113         $OUTPUT->show_message("emoticonerror", 'error');
T 114
ed6592 115       array_push($included_images, $image_name);
S 116       }
a0109c 117
4e6eb1 118     $body = $body_pre . 'cid:_' . $image_name . $body_post;
S 119
a0109c 120     $last_img_pos = $pos2;
S 121     }
122    
123   $mime_message->setHTMLBody($body);
124 }
41fa0b 125
b068a0 126 if (strlen($_POST['_draft_saveid']) > 3)
T 127   $olddraftmessageid = get_input_value('_draft_saveid', RCUBE_INPUT_POST);
128
8cb245 129 $message_id = sprintf('<%s@%s>', md5(uniqid('rcmail'.rand(),true)), rcmail_mail_domain($_SESSION['imap_host']));
b068a0 130 $savedraft = !empty($_POST['_draft']) ? TRUE : FALSE;
41fa0b 131
T 132 // remove all scripts and act as called in frame
133 $OUTPUT->reset();
f11541 134 $OUTPUT->framed = TRUE;
41fa0b 135
b068a0 136
4e17e6 137 /****** check submission and compose message ********/
10a699 138
T 139
dc0969 140 if (!$savedraft && empty($_POST['_to']) && empty($_POST['_subject']) && $_POST['_message'])
10a699 141   {
f11541 142   $OUTPUT->show_message("sendingfailed", 'error');
T 143   $OUTPUT->send('iframe');
10a699 144   return;
T 145   }
4e17e6 146
T 147
5bc8cb 148 // set default charset
13c1af 149 $input_charset = $OUTPUT->get_charset();
c03095 150 $message_charset = isset($_POST['_charset']) ? $_POST['_charset'] : $input_charset;
T 151
5a6ad2 152 $mailto_regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m', '/;/');
T 153 $mailto_replace = array(', ', ', ', '', ',');
4e17e6 154
abb32e 155 // replace new lines and strip ending ', '
ea7c46 156 $mailto = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_to', RCUBE_INPUT_POST, TRUE, $message_charset));
4e17e6 157
T 158 // decode address strings
159 $to_address_arr = $IMAP->decode_address_list($mailto);
b068a0 160 $identity_arr = rcmail_get_identity(get_input_value('_from', RCUBE_INPUT_POST));
4e17e6 161
T 162 $from = $identity_arr['mailto'];
163 $first_to = is_array($to_address_arr[0]) ? $to_address_arr[0]['mailto'] : $mailto;
164
b068a0 165 if (empty($identity_arr['string']))
T 166   $identity_arr['string'] = $from;
4e17e6 167
T 168 // compose headers array
421f5e 169 $headers = array('Date' => date('D, j M Y H:i:s O'),
f11541 170                  'From' => rcube_charset_convert($identity_arr['string'], RCMAIL_CHARSET, $message_charset),
5a6ad2 171                  'To'   => $mailto);
4e17e6 172
T 173 // additional recipients
ea7c46 174 if (!empty($_POST['_cc']))
T 175   $headers['Cc'] = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_cc', RCUBE_INPUT_POST, TRUE, $message_charset));
4e17e6 176
ea7c46 177 if (!empty($_POST['_bcc']))
T 178   $headers['Bcc'] = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_bcc', RCUBE_INPUT_POST, TRUE, $message_charset));
4e17e6 179   
ea7c46 180 if (!empty($identity_arr['bcc']))
4e17e6 181   $headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc'];
T 182
183 // add subject
ea7c46 184 $headers['Subject'] = trim(get_input_value('_subject', RCUBE_INPUT_POST, FALSE, $message_charset));
4e17e6 185
ea7c46 186 if (!empty($identity_arr['organization']))
4e17e6 187   $headers['Organization'] = $identity_arr['organization'];
T 188
7984ec 189 if (!empty($_POST['_replyto']))
T 190   $headers['Reply-To'] = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_replyto', RCUBE_INPUT_POST, TRUE, $message_charset));
191 else if (!empty($identity_arr['reply-to']))
4e17e6 192   $headers['Reply-To'] = $identity_arr['reply-to'];
T 193
f88d41 194 if (!empty($_SESSION['compose']['reply_msgid']))
4e17e6 195   $headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid'];
T 196
f88d41 197 if (!empty($_SESSION['compose']['references']))
T 198   $headers['References'] = $_SESSION['compose']['references'];
4e17e6 199
ea7c46 200 if (!empty($_POST['_priority']))
4e17e6 201   {
c57996 202   $priority = intval($_POST['_priority']);
3287e8 203   $a_priorities = array(1=>'highest', 2=>'high', 4=>'low', 5=>'lowest');
4e17e6 204   if ($str_priority = $a_priorities[$priority])
T 205     $headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority));
206   }
207
620439 208 if (!empty($_POST['_receipt']))
T 209   {
210   $headers['Return-Receipt-To'] = $identity_arr['string'];
211   $headers['Disposition-Notification-To'] = $identity_arr['string'];
212   }
4e17e6 213
T 214 // additional headers
215 $headers['Message-ID'] = $message_id;
216 $headers['X-Sender'] = $from;
217
ea7c46 218 if (!empty($CONFIG['useragent']))
4e17e6 219   $headers['User-Agent'] = $CONFIG['useragent'];
T 220
c03095 221 // fetch message body
ea7c46 222 $message_body = get_input_value('_message', RCUBE_INPUT_POST, TRUE, $message_charset);
4e17e6 223
4b0f65 224 // append generic footer to all messages
63f9d3 225 if (!$savedraft && !empty($CONFIG['generic_message_footer']) && ($footer = file_get_contents(realpath($CONFIG['generic_message_footer']))))
T 226   $message_body .= "\r\n" . rcube_charset_convert($footer, 'UTF-8', $message_charset);
4b0f65 227
4647e1 228 // try to autodetect operating system and use the correct line endings
4b0f65 229 // use the configured delimiter for headers
4647e1 230 if (!empty($CONFIG['mail_header_delimiter']))
T 231   $header_delm = $CONFIG['mail_header_delimiter'];
232 else if (strtolower(substr(PHP_OS, 0, 3)=='win')) 
233   $header_delm = "\r\n";
234 else if (strtolower(substr(PHP_OS, 0, 3)=='mac'))
235   $header_delm = "\r\n";
236 else    
237   $header_delm = "\n";
4b0f65 238
a0109c 239
S 240 $isHtmlVal = strtolower(get_input_value('_is_html', RCUBE_INPUT_POST));
241 $isHtml = ($isHtmlVal == "1");
242
ab6f80 243 // create extended PEAR::Mail_mime instance
T 244 $MAIL_MIME = new rc_mail_mime($header_delm);
a0109c 245
S 246 // For HTML-formatted messages, construct the MIME message with both
247 // the HTML part and the plain-text part
248
249 if ($isHtml)
250   {
251   $MAIL_MIME->setHTMLBody($message_body);
252
253   // add a plain text version of the e-mail as an alternative part.
254   $h2t = new html2text($message_body);
a23884 255   $plainTextPart = wordwrap($h2t->get_text(), 998, "\r\n", true);
a0109c 256   $MAIL_MIME->setTXTBody($plainTextPart);
S 257
258   // look for "emoticon" images from TinyMCE and copy into message as attachments
259   rcmail_attach_emoticons($MAIL_MIME);
260   }
261 else
262   {
a23884 263   $message_body = wordwrap($message_body, 75, "\r\n");
T 264   $message_body = wordwrap($message_body, 998, "\r\n", true);
a0109c 265   $MAIL_MIME->setTXTBody($message_body, FALSE, TRUE);
S 266   }
4e17e6 267
T 268
269 // add stored attachments, if any
270 if (is_array($_SESSION['compose']['attachments']))
271   foreach ($_SESSION['compose']['attachments'] as $attachment)
ab6f80 272     $MAIL_MIME->addAttachment($attachment['path'], $attachment['mimetype'], $attachment['name'], true, 'base64', 'attachment', $message_charset);
4e17e6 273
T 274 // add submitted attachments
275 if (is_array($_FILES['_attachments']['tmp_name']))
276   foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath)
ab6f80 277     $MAIL_MIME->addAttachment($filepath, $files['type'][$i], $files['name'][$i], true, 'base64', 'attachment', $message_charset);
4e17e6 278
1cded8 279
f88d41 280 // chose transfer encoding
T 281 $charset_7bit = array('ASCII', 'ISO-2022-JP', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-15');
282 $transfer_encoding = in_array(strtoupper($message_charset), $charset_7bit) ? '7bit' : '8bit';
283
a95e0e 284 // encoding settings for mail composing
a23884 285 $message_param = array(
T 286   'text_encoding' => $transfer_encoding,
287   'html_encoding' => 'quoted-printable',
288   'head_encoding' => 'quoted-printable',
289   'head_charset'  => $message_charset,
290   'html_charset'  => $message_charset,
291   'text_charset'  => $message_charset,
292 );
4e17e6 293
T 294 // compose message body and get headers
b517af 295 $msg_body = $MAIL_MIME->get($message_param);
T 296 // unset to save memory.
297 unset($MAIL_MIME->_parts);
4e17e6 298
5a6ad2 299 // encoding subject header with mb_encode provides better results with asian characters
13c1af 300 if ($MBSTRING && function_exists("mb_encode_mimeheader"))
b517af 301 {
24fe97 302   mb_internal_encoding($message_charset);
5a6ad2 303   $mb_subject = mb_encode_mimeheader($headers['Subject'], $message_charset, 'Q');
f11541 304   mb_internal_encoding(RCMAIL_CHARSET);
b517af 305 }
4e17e6 306
1966c5 307 // Begin SMTP Delivery Block 
S 308 if (!$savedraft) {
4e17e6 309
1966c5 310   // send thru SMTP server using custom SMTP library
S 311   if ($CONFIG['smtp_server'])
312     {
313     // generate list of recipients
314     $a_recipients = array($mailto);
315   
316     if (strlen($headers['Cc']))
317       $a_recipients[] = $headers['Cc'];
318     if (strlen($headers['Bcc']))
319       $a_recipients[] = $headers['Bcc'];
320   
321     // clean Bcc from header for recipients
322     $send_headers = $headers;
323     unset($send_headers['Bcc']);
324   
5a6ad2 325     if (!empty($mb_subject))
T 326       $send_headers['Subject'] = $mb_subject;
1966c5 327   
S 328     // send message
b517af 329     $smtp_response = array();
95609c 330     $sent = smtp_mail($from, $a_recipients, ($foo = $MAIL_MIME->txtHeaders($send_headers)), $msg_body, $smtp_response);
1966c5 331   
S 332     // log error
333     if (!$sent)
f11541 334       raise_error(array('code' => 800, 'type' => 'smtp', 'line' => __LINE__, 'file' => __FILE__,
b517af 335                         'message' => "SMTP error: ".join("\n", $smtp_response)), TRUE, FALSE);
1966c5 336     }
S 337   
338   // send mail using PHP's mail() function
339   else
340     {
341     // unset some headers because they will be added by the mail() function
342     $headers_enc = $MAIL_MIME->headers($headers);
343     $headers_php = $MAIL_MIME->_headers;
344     unset($headers_php['To'], $headers_php['Subject']);
345     
5a6ad2 346     if (!empty($mb_subject))
T 347       $headers_enc['Subject'] = $mb_subject;
348     
1966c5 349     // reset stored headers and overwrite
S 350     $MAIL_MIME->_headers = array();
351     $header_str = $MAIL_MIME->txtHeaders($headers_php);
352   
353     if (ini_get('safe_mode'))
5a6ad2 354       $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str);
1966c5 355     else
5a6ad2 356       $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from");
1966c5 357     }
S 358   
359   
360   // return to compose page if sending failed
968bdc 361   if (!$sent)
4e17e6 362     {
f11541 363     $OUTPUT->show_message("sendingfailed", 'error'); 
T 364     $OUTPUT->send('iframe');
1966c5 365     return;
4e17e6 366     }
13c1af 367   
1966c5 368   
S 369   // set repliead flag
370   if ($_SESSION['compose']['reply_uid'])
371     $IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED');
c03095 372
1966c5 373   } // End of SMTP Delivery Block
41fa0b 374
T 375
4e17e6 376
1966c5 377 // Determine which folder to save message
b068a0 378 if ($savedraft)
T 379   $store_target = 'drafts_mbox';
380 else
381   $store_target = 'sent_mbox';
c03095 382
1966c5 383 if ($CONFIG[$store_target])
4e17e6 384   {
T 385   // create string of complete message headers
386   $header_str = $MAIL_MIME->txtHeaders($headers);
387
388   // check if mailbox exists
1966c5 389   if (!in_array_nocase($CONFIG[$store_target], $IMAP->list_mailboxes()))
S 390     $store_folder = $IMAP->create_mailbox($CONFIG[$store_target], TRUE);
520c36 391   else
1966c5 392     $store_folder = TRUE;
b068a0 393   
T 394   // add headers to message body
395   $msg_body = $header_str."\r\n".$msg_body;
4e17e6 396
T 397   // append message to sent box
1966c5 398   if ($store_folder)
b068a0 399     $saved = $IMAP->save_message($CONFIG[$store_target], $msg_body);
520c36 400
T 401   // raise error if saving failed
402   if (!$saved)
f0f98f 403     {
f11541 404     raise_error(array('code' => 800, 'type' => 'imap', 'file' => __FILE__,
1966c5 405                       'message' => "Could not save message in $CONFIG[$store_target]"), TRUE, FALSE);
41fa0b 406     
f11541 407     $OUTPUT->show_message('errorsaving', 'error');
T 408     $OUTPUT->send('iframe');
f0f98f 409     }
4e17e6 410
b068a0 411   if ($olddraftmessageid)
T 412     {
1966c5 413     // delete previous saved draft
S 414     $a_deleteid = $IMAP->search($CONFIG['drafts_mbox'],'HEADER Message-ID',$olddraftmessageid);
415     $deleted = $IMAP->delete_message($IMAP->get_uid($a_deleteid[0],$CONFIG['drafts_mbox']),$CONFIG['drafts_mbox']);
4e17e6 416
f0f98f 417     // raise error if deletion of old draft failed
1966c5 418     if (!$deleted)
f11541 419       raise_error(array('code' => 800, 'type' => 'imap', 'file' => __FILE__,
1966c5 420                         'message' => "Could not delete message from ".$CONFIG['drafts_mbox']), TRUE, FALSE);
4e17e6 421     }
T 422   }
423
1966c5 424 if ($savedraft)
S 425   {
f11541 426   // display success
T 427   $OUTPUT->show_message('messagesaved', 'confirmation');
f0f98f 428
f11541 429   // update "_draft_saveid" and the "cmp_hash" to prevent "Unsaved changes" warning
T 430   $OUTPUT->command('set_draft_id', str_replace(array('<','>'), "", $message_id));
431   $OUTPUT->command('compose_field_hash', true);
41fa0b 432
f0f98f 433   // start the auto-save timer again
f11541 434   $OUTPUT->command('auto_save_start');
f0f98f 435
f11541 436   $OUTPUT->send('iframe');
1966c5 437   }
S 438 else
439   {
440   if ($CONFIG['smtp_log'])
441     {
f11541 442     $log_entry = sprintf(
T 443       "[%s] User: %d on %s; Message for %s; %s\n",
444       date("d-M-Y H:i:s O", mktime()),
445       $_SESSION['user_id'],
446       $_SERVER['REMOTE_ADDR'],
447       $mailto,
448       !empty($smtp_response) ? join('; ', $smtp_response) : '');
4e17e6 449
1966c5 450     if ($fp = @fopen($CONFIG['log_dir'].'/sendmail', 'a'))
S 451       {
452       fwrite($fp, $log_entry);
453       fclose($fp);
454       }
455     }
41fa0b 456
1966c5 457   rcmail_compose_cleanup();
f11541 458   $OUTPUT->command('sent_successfully', rcube_label('messagesent'));
T 459   $OUTPUT->send('iframe');
1966c5 460   }
4e17e6 461
T 462
1966c5 463 ?>