00001 <?php
00002
00003
00083 function drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE) {
00084 $default_from = variable_get('site_mail', ini_get('sendmail_from'));
00085
00086
00087 $message = array(
00088 'id' => $module . '_' . $key,
00089 'to' => $to,
00090 'from' => isset($from) ? $from : $default_from,
00091 'language' => $language,
00092 'params' => $params,
00093 'subject' => '',
00094 'body' => array()
00095 );
00096
00097
00098 $headers = array(
00099 'MIME-Version' => '1.0',
00100 'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
00101 'Content-Transfer-Encoding' => '8Bit',
00102 'X-Mailer' => 'Drupal'
00103 );
00104 if ($default_from) {
00105
00106
00107
00108 $headers['From'] = $headers['Reply-To'] = $headers['Sender'] = $headers['Return-Path'] = $headers['Errors-To'] = $default_from;
00109 }
00110 if ($from) {
00111 $headers['From'] = $headers['Reply-To'] = $from;
00112 }
00113 $message['headers'] = $headers;
00114
00115
00116
00117
00118 if (drupal_function_exists($function = $module . '_mail')) {
00119 $function($key, $message, $params);
00120 }
00121
00122
00123 drupal_alter('mail', $message);
00124
00125
00126 $message['body'] = is_array($message['body']) ? drupal_wrap_mail(implode("\n\n", $message['body'])) : drupal_wrap_mail($message['body']);
00127
00128
00129 if ($send) {
00130 $message['result'] = drupal_mail_send($message);
00131
00132
00133 if (!$message['result']) {
00134 watchdog('mail', 'Error sending e-mail (from %from to %to).', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_ERROR);
00135 drupal_set_message(t('Unable to send e-mail. Please contact the site admin, if the problem persists.'), 'error');
00136 }
00137 }
00138
00139 return $message;
00140 }
00141
00173 function drupal_mail_send($message) {
00174
00175 if (variable_get('smtp_library', '') && file_exists(variable_get('smtp_library', ''))) {
00176 include_once './' . variable_get('smtp_library', '');
00177 return drupal_mail_wrapper($message);
00178 }
00179 else {
00180 $mimeheaders = array();
00181 foreach ($message['headers'] as $name => $value) {
00182 $mimeheaders[] = $name . ': ' . mime_header_encode($value);
00183 }
00184 return mail(
00185 $message['to'],
00186 mime_header_encode($message['subject']),
00187
00188
00189 str_replace("\r", '', $message['body']),
00190
00191
00192 join("\n", $mimeheaders)
00193 );
00194 }
00195 }
00196
00211 function drupal_wrap_mail($text, $indent = '') {
00212
00213 $text = str_replace("\r", '', $text);
00214
00215 $clean_indent = _drupal_html_to_text_clean($indent);
00216 $soft = strpos($clean_indent, ' ') === FALSE;
00217
00218 if (strpos($text, "\n") !== FALSE) {
00219
00220 $text = preg_replace('/ +\n/m', "\n", $text);
00221
00222 $lines = explode("\n", $text);
00223 array_walk($lines, '_drupal_wrap_mail_line', array('soft' => $soft, 'length' => strlen($indent)));
00224 $text = implode("\n", $lines);
00225 }
00226 else {
00227
00228 _drupal_wrap_mail_line($text, 0, array('soft' => $soft, 'length' => strlen($indent)));
00229 }
00230
00231 $text = preg_replace('/^ +\n/m', "\n", $text);
00232
00233 $text = preg_replace('/^(>| |From)/m', ' $1', $text);
00234
00235 $text = $indent . substr(preg_replace('/^/m', $clean_indent, $text), strlen($indent));
00236
00237 return $text;
00238 }
00239
00261 function drupal_html_to_text($string, $allowed_tags = NULL) {
00262
00263 static $supported_tags;
00264 if (empty($supported_tags)) {
00265 $supported_tags = array('a', 'em', 'i', 'strong', 'b', 'br', 'p', 'blockquote', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr');
00266 }
00267
00268
00269 $allowed_tags = isset($allowed_tags) ? array_intersect($supported_tags, $allowed_tags) : $supported_tags;
00270
00271
00272 $string = _filter_htmlcorrector(filter_xss($string, $allowed_tags));
00273
00274
00275 $string = preg_replace('!</?(em|i)((?> +)[^>]*)?>!i', '/', $string);
00276 $string = preg_replace('!</?(strong|b)((?> +)[^>]*)?>!i', '*', $string);
00277
00278
00279
00280
00281 _drupal_html_to_mail_urls(NULL, TRUE);
00282 $pattern = '@(<a[^>]+?href="([^"]*)"[^>]*?>(.+?)</a>)@i';
00283 $string = preg_replace_callback($pattern, '_drupal_html_to_mail_urls', $string);
00284 $urls = _drupal_html_to_mail_urls();
00285 $footnotes = '';
00286 if (count($urls)) {
00287 $footnotes .= "\n";
00288 for ($i = 0, $max = count($urls); $i < $max; $i++) {
00289 $footnotes .= '[' . ($i + 1) . '] ' . $urls[$i] . "\n";
00290 }
00291 }
00292
00293
00294 $split = preg_split('/<([^>]+?)>/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
00295
00296
00297
00298 $tag = FALSE;
00299 $casing = NULL;
00300 $output = '';
00301 $indent = array();
00302 $lists = array();
00303 foreach ($split as $value) {
00304 $chunk = NULL;
00305
00306
00307 if ($tag) {
00308 list($tagname) = explode(' ', strtolower($value), 2);
00309 switch ($tagname) {
00310
00311 case 'ul':
00312 array_unshift($lists, '*');
00313 break;
00314 case 'ol':
00315 array_unshift($lists, 1);
00316 break;
00317 case '/ul':
00318 case '/ol':
00319 array_shift($lists);
00320 $chunk = '';
00321 break;
00322
00323
00324 case 'blockquote':
00325
00326 $indent[] = count($lists) ? ' "' : '>';
00327 break;
00328 case 'li':
00329 $indent[] = is_numeric($lists[0]) ? ' ' . $lists[0]++ . ') ' : ' * ';
00330 break;
00331 case 'dd':
00332 $indent[] = ' ';
00333 break;
00334 case 'h3':
00335 $indent[] = '.... ';
00336 break;
00337 case 'h4':
00338 $indent[] = '.. ';
00339 break;
00340 case '/blockquote':
00341 if (count($lists)) {
00342
00343 $output = rtrim($output, "> \n") . "\"\n";
00344 $chunk = '';
00345 }
00346
00347 case '/li':
00348 case '/dd':
00349 array_pop($indent);
00350 break;
00351 case '/h3':
00352 case '/h4':
00353 array_pop($indent);
00354 case '/h5':
00355 case '/h6':
00356 $chunk = '';
00357 break;
00358
00359
00360 case 'h1':
00361 $indent[] = '======== ';
00362 $casing = 'drupal_strtoupper';
00363 break;
00364 case 'h2':
00365 $indent[] = '-------- ';
00366 $casing = 'drupal_strtoupper';
00367 break;
00368 case '/h1':
00369 case '/h2':
00370 $casing = NULL;
00371
00372 $output = _drupal_html_to_text_pad($output, ($tagname == '/h1') ? '=' : '-', ' ');
00373 array_pop($indent);
00374 $chunk = '';
00375 break;
00376
00377
00378 case 'hr':
00379
00380 $output .= drupal_wrap_mail('', implode('', $indent)) . "\n";
00381 $output = _drupal_html_to_text_pad($output, '-');
00382 break;
00383
00384
00385 case '/p':
00386 case '/dl':
00387 $chunk = '';
00388 break;
00389 }
00390 }
00391
00392 else {
00393
00394 $value = trim(preg_replace('/\s+/', ' ', decode_entities($value)));
00395 if (strlen($value)) {
00396 $chunk = $value;
00397 }
00398 }
00399
00400
00401 if (isset($chunk)) {
00402
00403 if (isset($casing)) {
00404 $chunk = $casing($chunk);
00405 }
00406
00407 $output .= drupal_wrap_mail($chunk, implode('', $indent)) . "\n";
00408
00409 $indent = array_map('_drupal_html_to_text_clean', $indent);
00410 }
00411
00412 $tag = !$tag;
00413 }
00414
00415 return $output . $footnotes;
00416 }
00417
00423 function _drupal_wrap_mail_line(&$line, $key, $values) {
00424
00425 $line = wordwrap($line, 77 - $values['length'], $values['soft'] ? " \n" : "\n");
00426
00427 $line = wordwrap($line, 996 - $values['length'], $values['soft'] ? " \n" : "\n");
00428 }
00429
00435 function _drupal_html_to_mail_urls($match = NULL, $reset = FALSE) {
00436 global $base_url, $base_path;
00437 static $urls = array(), $regexp;
00438
00439 if ($reset) {
00440
00441 $urls = array();
00442 }
00443 else {
00444 if (empty($regexp)) {
00445 $regexp = '@^' . preg_quote($base_path, '@') . '@';
00446 }
00447 if ($match) {
00448 list(, , $url, $label) = $match;
00449
00450 $urls[] = strpos($url, '://') ? $url : preg_replace($regexp, $base_url . '/', $url);
00451 return $label . ' [' . count($urls) . ']';
00452 }
00453 }
00454 return $urls;
00455 }
00456
00462 function _drupal_html_to_text_clean($indent) {
00463 return preg_replace('/[^>]/', ' ', $indent);
00464 }
00465
00471 function _drupal_html_to_text_pad($text, $pad, $prefix = '') {
00472
00473 $text = substr($text, 0, -1);
00474
00475 if (($p = strrpos($text, "\n")) === FALSE) {
00476 $p = -1;
00477 }
00478 $n = max(0, 79 - (strlen($text) - $p));
00479
00480 return $text . $prefix . str_repeat($pad, $n - strlen($prefix)) . "\n";
00481 }