From 6ba1737c22a002a71210fcd15ad36c3c2bc68402 Mon Sep 17 00:00:00 2001 From: brettp Date: Fri, 14 May 2010 16:27:08 +0000 Subject: merge -r5832:5898 from 1.7 to trunk. git-svn-id: http://code.elgg.org/elgg/trunk@6055 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/notification.php | 72 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'engine/lib/notification.php') diff --git a/engine/lib/notification.php b/engine/lib/notification.php index 024881e0f..58e2a10f6 100644 --- a/engine/lib/notification.php +++ b/engine/lib/notification.php @@ -308,6 +308,78 @@ function email_notify_handler(ElggEntity $from, ElggUser $to, $subject, $message return mail($to, $subject, wordwrap($message), $headers); } +/** + * Send an email to any email address + * + * @param string $from Email address or string: "name " + * @param string $to Email address or string: "name " + * @param string $subject The subject of the message + * @param string $body The message body + * @param array $params Optional parameters (none used in this function) + * @return bool + */ +function elgg_send_email($from, $to, $subject, $body, array $params = NULL) { + global $CONFIG; + + if (!$from) { + throw new NotificationException(sprintf(elgg_echo('NotificationException:NoEmailAddress'), 'from')); + } + + if (!$to) { + throw new NotificationException(sprintf(elgg_echo('NotificationException:NoEmailAddress'), 'to')); + } + + // return TRUE/FALSE to stop elgg_send_email() from sending + $mail_params = array( 'to' => $to, + 'from' => $from, + 'subject' => $subject, + 'body' => $body, + 'params' => $params); + $result = trigger_plugin_hook('email', 'system', $mail_params, NULL); + if ($result !== NULL) { + return $result; + } + + $header_eol = "\r\n"; + if (isset($CONFIG->broken_mta) && $CONFIG->broken_mta) { + // Allow non-RFC 2822 mail headers to support some broken MTAs + $header_eol = "\n"; + } + + // Windows is somewhat broken, so we use just address for to and from + if (strtolower(substr(PHP_OS, 0 , 3)) == 'win') { + // strip name from to and from + if (strpos($to, '<')) { + preg_match('/<(.*)>/', $to, $matches); + $to = $matches[1]; + } + if (strpos($from, '<')) { + preg_match('/<(.*)>/', $from, $matches); + $from = $matches[1]; + } + } + + $headers = "From: $from{$header_eol}" + . "Content-Type: text/plain; charset=UTF-8; format=flowed{$header_eol}" + . "MIME-Version: 1.0{$header_eol}" + . "Content-Transfer-Encoding: 8bit{$header_eol}"; + + + // Sanitise subject by stripping line endings + $subject = preg_replace("/(\r\n|\r|\n)/", " ", $subject); + if (is_callable('mb_encode_mimeheader')) { + $subject = mb_encode_mimeheader($subject,"UTF-8", "B"); + } + + // Format message + $message = html_entity_decode($body, ENT_COMPAT, 'UTF-8'); // Decode any html entities + $message = strip_tags($body); // Strip tags from message + $message = preg_replace("/(\r\n|\r)/", "\n", $body); // Convert to unix line endings in body + $message = preg_replace("/^From/", ">From", $body); // Change lines starting with From to >From + + return mail($to, $subject, wordwrap($body), $headers); +} + /** * Correctly initialise notifications and register the email handler. * -- cgit v1.2.3