webmaster@1: mail, user_preferred_language($account), $params); webmaster@1: * } webmaster@1: * } webmaster@1: * webmaster@1: * function example_mail($key, &$message, $params) { webmaster@1: * $language = $message['language']; webmaster@1: * $variables = user_mail_tokens($params['account'], $language); webmaster@1: * switch($key) { webmaster@1: * case 'notice': webmaster@1: * $message['subject'] = t('Notification from !site', $variables, $language->language); webmaster@1: * $message['body'] = t("Dear !username\n\nThere is new content available on the site.", $variables, $language->language); webmaster@1: * break; webmaster@1: * } webmaster@1: * } webmaster@1: * @endcode webmaster@1: * webmaster@1: * @param $module webmaster@1: * A module name to invoke hook_mail() on. The {$module}_mail() hook will be webmaster@1: * called to complete the $message structure which will already contain common webmaster@1: * defaults. webmaster@1: * @param $key webmaster@1: * A key to identify the e-mail sent. The final e-mail id for e-mail altering webmaster@1: * will be {$module}_{$key}. webmaster@1: * @param $to webmaster@1: * The e-mail address or addresses where the message will be sent to. The webmaster@1: * formatting of this string must comply with RFC 2822. Some examples are: webmaster@1: * user@example.com webmaster@1: * user@example.com, anotheruser@example.com webmaster@1: * User webmaster@1: * User , Another User webmaster@1: * @param $language webmaster@1: * Language object to use to compose the e-mail. webmaster@1: * @param $params webmaster@1: * Optional parameters to build the e-mail. webmaster@1: * @param $from webmaster@1: * Sets From, Reply-To, Return-Path and Error-To to this value, if given. webmaster@1: * @param $send webmaster@1: * Send the message directly, without calling drupal_mail_send() manually. webmaster@1: * @return webmaster@1: * The $message array structure containing all details of the webmaster@1: * message. If already sent ($send = TRUE), then the 'result' element webmaster@1: * will contain the success indicator of the e-mail, failure being already webmaster@1: * written to the watchdog. (Success means nothing more than the message being webmaster@1: * accepted at php-level, which still doesn't guarantee it to be delivered.) webmaster@1: */ webmaster@1: function drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE) { webmaster@1: $default_from = variable_get('site_mail', ini_get('sendmail_from')); webmaster@1: webmaster@1: // Bundle up the variables into a structured array for altering. webmaster@1: $message = array( webmaster@1: 'id' => $module .'_'. $key, webmaster@1: 'to' => $to, webmaster@1: 'from' => isset($from) ? $from : $default_from, webmaster@1: 'language' => $language, webmaster@1: 'params' => $params, webmaster@1: 'subject' => '', webmaster@1: 'body' => array() webmaster@1: ); webmaster@1: webmaster@1: // Build the default headers webmaster@1: $headers = array( webmaster@1: 'MIME-Version' => '1.0', webmaster@1: 'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes', webmaster@1: 'Content-Transfer-Encoding' => '8Bit', webmaster@1: 'X-Mailer' => 'Drupal' webmaster@1: ); webmaster@1: if ($default_from) { webmaster@1: // To prevent e-mail from looking like spam, the addresses in the Sender and webmaster@1: // Return-Path headers should have a domain authorized to use the originating webmaster@1: // SMTP server. Errors-To is redundant, but shouldn't hurt. webmaster@1: $headers['From'] = $headers['Reply-To'] = $headers['Sender'] = $headers['Return-Path'] = $headers['Errors-To'] = $default_from; webmaster@1: } webmaster@1: if ($from) { webmaster@1: $headers['From'] = $headers['Reply-To'] = $from; webmaster@1: } webmaster@1: $message['headers'] = $headers; webmaster@1: webmaster@1: // Build the e-mail (get subject and body, allow additional headers) by webmaster@1: // invoking hook_mail() on this module. We cannot use module_invoke() as webmaster@1: // we need to have $message by reference in hook_mail(). webmaster@1: if (function_exists($function = $module .'_mail')) { webmaster@1: $function($key, $message, $params); webmaster@1: } webmaster@1: webmaster@1: // Invoke hook_mail_alter() to allow all modules to alter the resulting e-mail. webmaster@1: drupal_alter('mail', $message); webmaster@1: webmaster@1: // Concatenate and wrap the e-mail body. webmaster@1: $message['body'] = is_array($message['body']) ? drupal_wrap_mail(implode("\n\n", $message['body'])) : drupal_wrap_mail($message['body']); webmaster@1: webmaster@1: // Optionally send e-mail. webmaster@1: if ($send) { webmaster@1: $message['result'] = drupal_mail_send($message); webmaster@1: webmaster@1: // Log errors webmaster@1: if (!$message['result']) { webmaster@1: watchdog('mail', 'Error sending e-mail (from %from to %to).', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_ERROR); webmaster@1: drupal_set_message(t('Unable to send e-mail. Please contact the site admin, if the problem persists.'), 'error'); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: return $message; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Send an e-mail message, using Drupal variables and default settings. webmaster@1: * More information in the webmaster@1: * PHP function reference for mail(). See drupal_mail() for information on webmaster@1: * how $message is composed. webmaster@1: * webmaster@1: * @param $message webmaster@1: * Message array with at least the following elements: webmaster@1: * - id webmaster@1: * A unique identifier of the e-mail type. Examples: 'contact_user_copy', webmaster@1: * 'user_password_reset'. webmaster@1: * - to webmaster@1: * The mail address or addresses where the message will be sent to. The webmaster@1: * formatting of this string must comply with RFC 2822. Some examples are: webmaster@1: * user@example.com webmaster@1: * user@example.com, anotheruser@example.com webmaster@1: * User webmaster@1: * User , Another User webmaster@1: * - subject webmaster@1: * Subject of the e-mail to be sent. This must not contain any newline webmaster@1: * characters, or the mail may not be sent properly. webmaster@1: * - body webmaster@1: * Message to be sent. Accepts both CRLF and LF line-endings. webmaster@1: * E-mail bodies must be wrapped. You can use drupal_wrap_mail() for webmaster@1: * smart plain text wrapping. webmaster@1: * - headers webmaster@1: * Associative array containing all mail headers. webmaster@1: * @return webmaster@1: * Returns TRUE if the mail was successfully accepted for delivery, webmaster@1: * FALSE otherwise. webmaster@1: */ webmaster@1: function drupal_mail_send($message) { webmaster@1: // Allow for a custom mail backend. webmaster@1: if (variable_get('smtp_library', '') && file_exists(variable_get('smtp_library', ''))) { webmaster@1: include_once './'. variable_get('smtp_library', ''); webmaster@1: return drupal_mail_wrapper($message); webmaster@1: } webmaster@1: else { webmaster@1: $mimeheaders = array(); webmaster@1: foreach ($message['headers'] as $name => $value) { webmaster@1: $mimeheaders[] = $name .': '. mime_header_encode($value); webmaster@1: } webmaster@1: return mail( webmaster@1: $message['to'], webmaster@1: mime_header_encode($message['subject']), webmaster@1: // Note: e-mail uses CRLF for line-endings, but PHP's API requires LF. webmaster@1: // They will appear correctly in the actual e-mail that is sent. webmaster@1: str_replace("\r", '', $message['body']), webmaster@1: join("\n", $mimeheaders) webmaster@1: ); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Perform format=flowed soft wrapping for mail (RFC 3676). webmaster@1: * webmaster@1: * We use delsp=yes wrapping, but only break non-spaced languages when webmaster@1: * absolutely necessary to avoid compatibility issues. webmaster@1: * webmaster@1: * We deliberately use LF rather than CRLF, see drupal_mail(). webmaster@1: * webmaster@1: * @param $text webmaster@1: * The plain text to process. webmaster@1: * @param $indent (optional) webmaster@1: * A string to indent the text with. Only '>' characters are repeated on webmaster@1: * subsequent wrapped lines. Others are replaced by spaces. webmaster@1: */ webmaster@1: function drupal_wrap_mail($text, $indent = '') { webmaster@1: // Convert CRLF into LF. webmaster@1: $text = str_replace("\r", '', $text); webmaster@1: // See if soft-wrapping is allowed. webmaster@1: $clean_indent = _drupal_html_to_text_clean($indent); webmaster@1: $soft = strpos($clean_indent, ' ') === FALSE; webmaster@1: // Check if the string has line breaks. webmaster@1: if (strpos($text, "\n") !== FALSE) { webmaster@1: // Remove trailing spaces to make existing breaks hard. webmaster@1: $text = preg_replace('/ +\n/m', "\n", $text); webmaster@1: // Wrap each line at the needed width. webmaster@1: $lines = explode("\n", $text); webmaster@1: array_walk($lines, '_drupal_wrap_mail_line', array('soft' => $soft, 'length' => strlen($indent))); webmaster@1: $text = implode("\n", $lines); webmaster@1: } webmaster@1: else { webmaster@1: // Wrap this line. webmaster@1: _drupal_wrap_mail_line($text, 0, array('soft' => $soft, 'length' => strlen($indent))); webmaster@1: } webmaster@1: // Empty lines with nothing but spaces. webmaster@1: $text = preg_replace('/^ +\n/m', "\n", $text); webmaster@1: // Space-stuff special lines. webmaster@1: $text = preg_replace('/^(>| |From)/m', ' $1', $text); webmaster@1: // Apply indentation. We only include non-'>' indentation on the first line. webmaster@1: $text = $indent . substr(preg_replace('/^/m', $clean_indent, $text), strlen($indent)); webmaster@1: webmaster@1: return $text; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Transform an HTML string into plain text, preserving the structure of the webmaster@1: * markup. Useful for preparing the body of a node to be sent by e-mail. webmaster@1: * webmaster@1: * The output will be suitable for use as 'format=flowed; delsp=yes' text webmaster@1: * (RFC 3676) and can be passed directly to drupal_mail() for sending. webmaster@1: * webmaster@1: * We deliberately use LF rather than CRLF, see drupal_mail(). webmaster@1: * webmaster@1: * This function provides suitable alternatives for the following tags: webmaster@1: *

    1. webmaster@1: *


      webmaster@1: * webmaster@1: * @param $string webmaster@1: * The string to be transformed. webmaster@1: * @param $allowed_tags (optional) webmaster@1: * If supplied, a list of tags that will be transformed. If omitted, all webmaster@1: * all supported tags are transformed. webmaster@1: * @return webmaster@1: * The transformed string. webmaster@1: */ webmaster@1: function drupal_html_to_text($string, $allowed_tags = NULL) { webmaster@1: // Cache list of supported tags. webmaster@1: static $supported_tags; webmaster@1: if (empty($supported_tags)) { webmaster@1: $supported_tags = array('a', 'em', 'i', 'strong', 'b', 'br', 'p', 'blockquote', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr'); webmaster@1: } webmaster@1: webmaster@1: // Make sure only supported tags are kept. webmaster@1: $allowed_tags = isset($allowed_tags) ? array_intersect($supported_tags, $allowed_tags) : $supported_tags; webmaster@1: webmaster@1: // Make sure tags, entities and attributes are well-formed and properly nested. webmaster@1: $string = _filter_htmlcorrector(filter_xss($string, $allowed_tags)); webmaster@1: webmaster@1: // Apply inline styles. webmaster@1: $string = preg_replace('!!i', '/', $string); webmaster@1: $string = preg_replace('!!i', '*', $string); webmaster@1: webmaster@1: // Replace inline
      tags with the text of link and a footnote. webmaster@1: // 'See the Drupal site' becomes webmaster@1: // 'See the Drupal site [1]' with the URL included as a footnote. webmaster@1: _drupal_html_to_mail_urls(NULL, TRUE); webmaster@1: $pattern = '@(]+?href="([^"]*)"[^>]*?>(.+?))@i'; webmaster@1: $string = preg_replace_callback($pattern, '_drupal_html_to_mail_urls', $string); webmaster@1: $urls = _drupal_html_to_mail_urls(); webmaster@1: $footnotes = ''; webmaster@1: if (count($urls)) { webmaster@1: $footnotes .= "\n"; webmaster@1: for ($i = 0, $max = count($urls); $i < $max; $i++) { webmaster@1: $footnotes .= '['. ($i + 1) .'] '. $urls[$i] ."\n"; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // Split tags from text. webmaster@1: $split = preg_split('/<([^>]+?)>/', $string, -1, PREG_SPLIT_DELIM_CAPTURE); webmaster@1: // Note: PHP ensures the array consists of alternating delimiters and literals webmaster@1: // and begins and ends with a literal (inserting $null as required). webmaster@1: webmaster@1: $tag = FALSE; // Odd/even counter (tag or no tag) webmaster@1: $casing = NULL; // Case conversion function webmaster@1: $output = ''; webmaster@1: $indent = array(); // All current indentation string chunks webmaster@1: $lists = array(); // Array of counters for opened lists webmaster@1: foreach ($split as $value) { webmaster@1: $chunk = NULL; // Holds a string ready to be formatted and output. webmaster@1: webmaster@1: // Process HTML tags (but don't output any literally). webmaster@1: if ($tag) { webmaster@1: list($tagname) = explode(' ', strtolower($value), 2); webmaster@1: switch ($tagname) { webmaster@1: // List counters webmaster@1: case 'ul': webmaster@1: array_unshift($lists, '*'); webmaster@1: break; webmaster@1: case 'ol': webmaster@1: array_unshift($lists, 1); webmaster@1: break; webmaster@1: case '/ul': webmaster@1: case '/ol': webmaster@1: array_shift($lists); webmaster@1: $chunk = ''; // Ensure blank new-line. webmaster@1: break; webmaster@1: webmaster@1: // Quotation/list markers, non-fancy headers webmaster@1: case 'blockquote': webmaster@1: // Format=flowed indentation cannot be mixed with lists. webmaster@1: $indent[] = count($lists) ? ' "' : '>'; webmaster@1: break; webmaster@1: case 'li': webmaster@1: $indent[] = is_numeric($lists[0]) ? ' '. $lists[0]++ .') ' : ' * '; webmaster@1: break; webmaster@1: case 'dd': webmaster@1: $indent[] = ' '; webmaster@1: break; webmaster@1: case 'h3': webmaster@1: $indent[] = '.... '; webmaster@1: break; webmaster@1: case 'h4': webmaster@1: $indent[] = '.. '; webmaster@1: break; webmaster@1: case '/blockquote': webmaster@1: if (count($lists)) { webmaster@1: // Append closing quote for inline quotes (immediately). webmaster@1: $output = rtrim($output, "> \n") ."\"\n"; webmaster@1: $chunk = ''; // Ensure blank new-line. webmaster@1: } webmaster@1: // Fall-through webmaster@1: case '/li': webmaster@1: case '/dd': webmaster@1: array_pop($indent); webmaster@1: break; webmaster@1: case '/h3': webmaster@1: case '/h4': webmaster@1: array_pop($indent); webmaster@1: case '/h5': webmaster@1: case '/h6': webmaster@1: $chunk = ''; // Ensure blank new-line. webmaster@1: break; webmaster@1: webmaster@1: // Fancy headers webmaster@1: case 'h1': webmaster@1: $indent[] = '======== '; webmaster@1: $casing = 'drupal_strtoupper'; webmaster@1: break; webmaster@1: case 'h2': webmaster@1: $indent[] = '-------- '; webmaster@1: $casing = 'drupal_strtoupper'; webmaster@1: break; webmaster@1: case '/h1': webmaster@1: case '/h2': webmaster@1: $casing = NULL; webmaster@1: // Pad the line with dashes. webmaster@1: $output = _drupal_html_to_text_pad($output, ($tagname == '/h1') ? '=' : '-', ' '); webmaster@1: array_pop($indent); webmaster@1: $chunk = ''; // Ensure blank new-line. webmaster@1: break; webmaster@1: webmaster@1: // Horizontal rulers webmaster@1: case 'hr': webmaster@1: // Insert immediately. webmaster@1: $output .= drupal_wrap_mail('', implode('', $indent)) ."\n"; webmaster@1: $output = _drupal_html_to_text_pad($output, '-'); webmaster@1: break; webmaster@1: webmaster@1: // Paragraphs and definition lists webmaster@1: case '/p': webmaster@1: case '/dl': webmaster@1: $chunk = ''; // Ensure blank new-line. webmaster@1: break; webmaster@1: } webmaster@1: } webmaster@1: // Process blocks of text. webmaster@1: else { webmaster@1: // Convert inline HTML text to plain text. webmaster@1: $value = trim(preg_replace('/\s+/', ' ', decode_entities($value))); webmaster@1: if (strlen($value)) { webmaster@1: $chunk = $value; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // See if there is something waiting to be output. webmaster@1: if (isset($chunk)) { webmaster@1: // Apply any necessary case conversion. webmaster@1: if (isset($casing)) { webmaster@1: $chunk = $casing($chunk); webmaster@1: } webmaster@1: // Format it and apply the current indentation. webmaster@1: $output .= drupal_wrap_mail($chunk, implode('', $indent)) ."\n"; webmaster@1: // Remove non-quotation markers from indentation. webmaster@1: $indent = array_map('_drupal_html_to_text_clean', $indent); webmaster@1: } webmaster@1: webmaster@1: $tag = !$tag; webmaster@1: } webmaster@1: webmaster@1: return $output . $footnotes; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function for array_walk in drupal_wrap_mail(). webmaster@1: * webmaster@1: * Wraps words on a single line. webmaster@1: */ webmaster@1: function _drupal_wrap_mail_line(&$line, $key, $values) { webmaster@1: // Use soft-breaks only for purely quoted or unindented text. webmaster@1: $line = wordwrap($line, 77 - $values['length'], $values['soft'] ? " \n" : "\n"); webmaster@1: // Break really long words at the maximum width allowed. webmaster@1: $line = wordwrap($line, 996 - $values['length'], $values['soft'] ? " \n" : "\n"); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function for drupal_html_to_text(). webmaster@1: * webmaster@1: * Keeps track of URLs and replaces them with placeholder tokens. webmaster@1: */ webmaster@1: function _drupal_html_to_mail_urls($match = NULL, $reset = FALSE) { webmaster@1: global $base_url, $base_path; webmaster@1: static $urls = array(), $regexp; webmaster@1: webmaster@1: if ($reset) { webmaster@1: // Reset internal URL list. webmaster@1: $urls = array(); webmaster@1: } webmaster@1: else { webmaster@1: if (empty($regexp)) { webmaster@1: $regexp = '@^'. preg_quote($base_path, '@') .'@'; webmaster@1: } webmaster@1: if ($match) { webmaster@1: list(, , $url, $label) = $match; webmaster@1: // Ensure all URLs are absolute. webmaster@1: $urls[] = strpos($url, '://') ? $url : preg_replace($regexp, $base_url .'/', $url); webmaster@1: return $label .' ['. count($urls) .']'; webmaster@1: } webmaster@1: } webmaster@1: return $urls; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function for drupal_wrap_mail() and drupal_html_to_text(). webmaster@1: * webmaster@1: * Replace all non-quotation markers from a given piece of indentation with spaces. webmaster@1: */ webmaster@1: function _drupal_html_to_text_clean($indent) { webmaster@1: return preg_replace('/[^>]/', ' ', $indent); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function for drupal_html_to_text(). webmaster@1: * webmaster@1: * Pad the last line with the given character. webmaster@1: */ webmaster@1: function _drupal_html_to_text_pad($text, $pad, $prefix = '') { webmaster@1: // Remove last line break. webmaster@1: $text = substr($text, 0, -1); webmaster@1: // Calculate needed padding space and add it. webmaster@1: if (($p = strrpos($text, "\n")) === FALSE) { webmaster@1: $p = -1; webmaster@1: } webmaster@1: $n = max(0, 79 - (strlen($text) - $p)); webmaster@1: // Add prefix and padding, and restore linebreak. webmaster@1: return $text . $prefix . str_repeat($pad, $n - strlen($prefix)) ."\n"; webmaster@1: }